Showing posts with label RestApi SharePoint. Show all posts
Showing posts with label RestApi SharePoint. Show all posts

Thursday, March 18, 2021

How to read more than 5000 items from document library in SharePoint RestApi?

  1. First call a ajax function with parameters "/_api/Web/Lists/GetByTitle(ListName)/Items?$orderby=Id desc&$top=1". Now you will get the latest added "Id".
  2. Now Divide the Id returned by the ajax by 5000. (Id/5000) So that you will get a result of how many times you need to perform the ajax.
  3. Now you can perform the ajax function repeatedly be filtering every 5000 items, with filter like, $filter=Id ge 0 and Id le 5000, $filter=Id ge 5000 and Id le 10000, ........ , etc.
  4. You can have a foreach loop to perform the ajax repeatedly with a dynamic filter, $filter=Id ge (5000*LoopValue) and Id le (5000*(LoopValue+1)).

Also make sure, the ajax to have async:true, to avoid performance issue. You can store the returned data in array and perform further actions, so that you could store more than 5000 data to perform your functions.

 

 var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('DocumentList')/items?$select=customerID&$top=1000";

    var response = response || [];  // this variable is used for storing list items

    function GetListItems(){

        return $.ajax({

            url: url,  

            method: "GET",  

            headers: {  

                "Accept": "application/json; odata=verbose"  

            },

            success: function(data){

                response = response.concat(data.d.results);

                if (data.d.__next) {

                    url = data.d.__next;

                    GetListItems();

                }

                $.each(response, function(index, item) {

                    arrayCustomerID[index] = item.customerID;

                });

            },

            error: function(error){

            }

        });

    }

Wednesday, March 17, 2021

How to check whether the current user has edit permission for a particular list using JSOM in SharePoint 2013

 we first need to get the login name of user and pass it to the get_effectiveBasePermissions method.


To ensure that user has edit permission, we will check the SP.PermissionKind.editListItems enum.


SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {

    getCurrentUserPermission();

});



function getCurrentUserPermission()

{    

    var web,clientContext,currentUser,oList,perMask;


    clientContext = new SP.ClientContext.get_current();

    web = clientContext.get_web();

    currentUser = web.get_currentUser();   

    oList = web.get_lists().getByTitle('Test List');

    clientContext.load(oList,'EffectiveBasePermissions');

    clientContext.load(currentUser); 

    clientContext.load(web);           


    clientContext.executeQueryAsync(function(){

        if (oList.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems)){

            console.log("user has edit permission");

        }else{

             console.log("user doesn't have edit permission");

        }   

    }, function(sender, args){

        console.log('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());

    });

}

How to get permission of a SharePoint list for a user using RESTApi

 Use EffectiveBasePermissions to get permissions of the user on a list. Example:


/_api/web/lists/getbytitle('L2')/EffectiveBasePermissions

Note that this will give the permissions of the logged in user


function checkPermissions() {

    var call = jQuery.ajax({

        url: _spPageContextInfo.webAbsoluteUrl +

            "/_api/Web/effectiveBasePermissions",

        type: "GET",

        dataType: "json",

        headers: {

            Accept: "application/json;odata=verbose"

        }

    });


    call.done(function (data, textStatus, jqXHR) {

        var manageListsPerms = new SP.BasePermissions();

        manageListsPerms.initPropertiesFromJson(data.d.EffectiveBasePermissions);


        var manageLists = manageListsPerms.has(SP.PermissionKind.manageLists);


        var message = jQuery("#message");

        message.text("Manage Lists: " + manageLists);

    });

}

Wednesday, December 2, 2020

Display SharePoint list items using rest api

 <div>

<input type="button" id="btnSubmit" value="Get List Details" />

</div>

<div id="divResults"></div>


<script>

$(function () {

$("#btnSubmit").on("click", function () {

getListData();

});

});


function getListData() {

var fullUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Projection')/items";

$.ajax({

url: fullUrl,

type: "GET",

headers: {

"accept": "application/json;odata=verbose",

"content-type": "application/json;odata=verbose",

},

success: onQuerySucceeded,

error: onQueryFailed

});

}


function onQuerySucceeded(data) {

var listItemInfo = ";

$.each(data.d.results, function (key, value) {

listItemInfo += '<b>Title:</b> ' + value.Title + ' – <b>Billable hours:</b> ' + value.BillableDays + '<br />';

});

$("#divResults").html(listItemInfo);

}

function onQueryFailed() {

alert('Error!');

}

</script>

Wednesday, February 15, 2017

How to get current user group collection through Rest api in SharePoint 2013

<script type="text/javascript">
    $(document).ready(function () { getCurrentUser(); });
    function getCurrentUser() {

        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/CurrentUser",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                getCurrentUserGroupColl(data.d.Id);
            },
            error: function (data) {
                failure(data);
            }
        });

    }
    function getCurrentUserGroupColl(UserID) {
        $.ajax
        ({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + UserID + ")/Groups",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                /* get all group's title of current user. */
                var results = data.d.results; var InnrHtmlgrp = "<ul>";
                for (var i = 0; i < results.length; i++) {
                    lstgrp += "<li>" + results[i].Title + "</li>";
                }
                $("#bindGroup").append(lstgrp + "</ul>");
            }
        });
    }
</script>
 
      <div id="bindGroup"></div>