Wednesday, August 5, 2015

SharePoint 2013 List Operations in REST– Retrieve List Fields Basics

You can use the function specified in the first post to call the REST URLs. You can use the appropriate function depending on the App type you are developing.

Get selected fields in SharePoint List using SELECT REST operation.

Getting single value fields

/*
 List Name : listname
 Columns: Title => Text , ID =>  Number
*/
 
var url = "/_api/web/lists/getbytitle('listname')/items?$select=Title,ID";
 
function s(data){
 
    // get the data and convert to JSON Objects
    var d = JSON.parse(data.body).d.results[0]; 
    
    alert(d.ID);
    alert(d.Title);
    
}
 
function f(data){
    alert(data);
}
 
getREST(url,s,f);

Getting Single value Lookup fields



/*
 List Name : listname
 Columns: ID =>  Number , CountryLookup => List LooukUp 
*/
 
var url = "/_api/web/lists/getbytitle('listname')/items?$select=ID,CountryLookupId";
 
function s(data){
 
    // get the data and convert to JSON Objects
    var d = JSON.parse(data.body).d.results[0]; 
    
    alert(d.ID);
    
    // 3 , will return the ID 
    alert(d.CountryLookupId);
}
 
function f(data){
    alert(data);
}
 
getREST(url,s,f);

Getting Multiple value Lookup fields



/*
 List Name : listname
 Columns: ID =>  Number , CountryLookup => List LooukUp multiple
*/
 
var url = "/_api/web/lists/getbytitle('listname')/items?$select=ID,CountryMultiLookupId";
 
function s(data){
 
    // get the data and convert to JSON Objects
    var d = JSON.parse(data.body).d.results[0]; 
    
    alert(d.ID);
    
    // [3,5]
    var mul = data.CountryMultiLookupId.results; 
    // iterate and show the results , $.each is a jQuery function
    $.each(mul,function(i,ele){ alert(ele) });    
}
 
function f(data){
    alert(data);
}
 
getREST(url,s,f);

Tuesday, July 14, 2015

SQL Quick Tip

Concatenating Text and INT

SELECT 'text' + CAST(ID AS VARCHAR(20)) 

Monday, May 18, 2015

REST operations in SharePoint 2013

REST operations are essential in any SharePoint development. This series explain available REST endpoints in SharePoint 2013. First article shows how we can made a REST call in both SharePoint Hosted and Provider Hosted app.

Executing a REST GET call from SharePoint Provider Hosted App

First you need to refer related JavaScript related.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript" src="#SPWeb/_layouts/15/MicrosoftAjax.js"></script>
<script type="text/javascript" src="#SPWeb/_layouts/1033/init.js"></script>
<script type="text/javascript" src="#SPWeb/_layouts/15/sp.core.js"></script>
<script type="text/javascript" src="#SPHost/_layouts/15/SP.RequestExecutor.js"></script>
<script type="text/javascript" src="#SPWeb/_layouts/15/sp.js"></script>


You need to replace the #SPWeb from Appweb Url and #SPHost from Host web Url. App Web Url and Host Web Url is available in Url.



You can find these parameters as QueryString and available in SPAppWebUrl, SPHostUrl.



You need to specify the AppWebUrlUrl in the method below. If you are good at jQuery you can change the method by introducing Deferred Object.




 

function getREST(url,success,fail){
 
  // specify your web app url here
  // EX- https://oapp.sharepoint.com/sites/AWM/ArtWorkManagement
  var AppWebUrlUrl = "";  
  var executor = new SP.RequestExecutor(AppWebUrlUrl);
 
  executor.executeAsync(
       {
           url: AppWebUrlUrl + url,
           method: "GET", 
           contentType: "application/json;odata=verbose",
           headers: { "Accept": "application/json; odata=verbose" },
           success: function (data) {
               if(success){success(data);} // call the function if done
           },
           error: function (data, errorCode, errorMessage) {
               if(fail){fail(data);} // call the function if done
           }
       }
);
}


Executing a REST GET call from SharePoint Hosted App



You need to specify the AppWebUrlUrl in the method below.




 

function getREST(url,success,fail){
 
  // specify your web app url here
  // EX- https://oapp.sharepoint.com/sites/AWM/ArtWorkManagement
  var AppWebUrlUrl = "";  
 
    $.ajax({
            method: "GET",
            url: AppWebUrlUrl + url,
            success: function (data) {
                if(success){success(data);} // call the function if done
 
            },
            error: function (data) {
                if(fail){fail(data);} // call the function if done
            }
        });
 }


Calling the Function




 

function done(data){
  // success
}
 
function error(){
    // Error
}
 
var restUrl = "" // Your URL goes here
getREST(restUrl,done,error);