Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint

REST Operations in SharePoint 2013

0.00/5 (No votes)
19 May 2015CPOL 6.9K  
REST operations in SharePoint 2013

REST operations are essential in any SharePoint development. This series explains the available REST endpoints in SharePoint 2013. The first article shows how we can make 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 to the related JavaScript.

JavaScript
<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.

JavaScript
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:

jsript
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

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)