SharePoint 2013 Rest API Samples

in this post i will list some of SharePoint Rest API calls

Get the groups of current user using REST API


<script src="//code.jquery.com/jquery-1.11.2.js" type="text/javascript"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>

<script type="text/javascript">

$(document).ready(function () {

getCurrentUserWithDetails().done(function(data)
{
var groupNames = ['Group Template Admins','Approvers'];
//determine wether current user is a memeber of group(s)
var userGroups = data.d.Groups.results;
var foundGroups = userGroups.filter(function(g){ return groupNames.indexOf(g.LoginName) > -1});
alert (JSON.stringify(foundGroups));

})
.fail(
function(error){
alert(JSON.stringify(error));
});

});

function getCurrentUserWithDetails()
{
var endpointUrl = _spPageContextInfo.webServerRelativeUrl + '/_api/web/currentuser/?$expand=groups';
//OR

//var endpointUrl = 'your site collection address/_api/web/currentuser/?$expand=groups';

return $.ajax({
url: endpointUrl,
method: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose"
}
});
}

</script>


function getCurrentUserWithDetails()
{
    var endpointUrl = _spPageContextInfo.webServerRelativeUrl + '/_api/web/currentuser/?$expand=groups';
    return $.ajax({
            url: endpointUrl,
            method: "GET",
            contentType: "application/json;odata=verbose",
            headers: {  
             "Accept": "application/json;odata=verbose"
            }
        });
}


function getPagedItems(webUrl,listTitle,startItemId,itemsCount)
{
    var endpointUrl = webUrl + "/_api/web/lists/getbytitle('" + listTitle +  "')/items?$skiptoken=" + encodeURIComponent('Paged=TRUE&p_SortBehavior=0&p_ID=' + (startItemId-1) + '&$top=' + itemsCount);
    return executeRequest(endpointUrl,'GET');
}

getPagedItems('https://contoso.sharepoint.com/','Pages',2,2)
.done(function(data){
    if(data.d.results.length == 0){
        console.log('Items not found');
        return;
    }
    for(var i = 0; i < data.d.results.length; i++){
        var item = data.d.results[i];
        console.log(item.Title);
    }  
});


function uploadFileBinary() {
  var executor = new SP.RequestExecutor(appweburl);
  var body = "";
  for (var i = 0; i < 1000; i++) {
    var ch = i % 256;
    body = body + String.fromCharCode(ch);
  }
  var info = {
    url: "_api/web/lists/getByTitle('Shared Documents')/RootFolder/Files/Add(url='a.dat', overwrite=true)",  
    method: "POST",
    binaryStringRequestBody: true,  
    body: body,  
    success: success,  
    error: fail,  
    state: "Update"};
   executor.executeAsync(info);
}

It\'s only fair to share...Digg thisBuffer this pageShare on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Pin on Pinterest0Share on StumbleUpon0Share on Tumblr0

Leave a Reply

Your email address will not be published. Required fields are marked *