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

Thursday, September 22, 2016

Using REST API For Selecting, Filtering, Sorting And Pagination in SharePoint List

 how to work with SharePoint list items, basically performing CRUD operations, using the combination of REST API 
and jQuery Ajax. The REST URI ends with any OData query operators to specify selecting, sorting, or filtering

Selecting and sorting items:

$select :

This ' /_api/web/lists/getbytitle('test')/items' url returns all items with all possible fields or list columns. 
But what if list has more than 20-30 columns?
 It’s not good practice to get all fields. 


Syntax for this is $select=Field1, Field2, Field3 
/_api/web/lists/getbytitle('test')/items?$select=ID,Title,Employee,company

'$orderby' :


The first two specify sorting in ascending order and the third one descending order.
 It's simple, you can use '$orderby' parameter and provide the field name. 
REST service will return sorted list items in response.

Syntax: for this is $orderby=(Column Internal Name order)

Ascending Order:
/_api/web/lists/getbytitle('emp')/items?$select=ID,Title,Employee,company&$orderby= Employee asc 
Descending Order:


/_api/web/lists/getbytitle('emp')/items?$select=ID,Title,Employee,company&$orderby= Employee desc  

Filtering items:

You can filter your list to contain only items which match a simple logical expression using the $filterparameter.

Syntax: for this is $filter=(Column Internal Name operator value).
See below examples,

Filter by Title

/_api/web/lists/getbytitle('emp')/items?$filter=Employee eq ‘parth'

Filter by ID:

/_api/web/lists/getbytitle('emp')/items?$filter=ID eq 2

Filter by Date

/_api/web/lists/getbytitle('emp')/items?$filter=Start_x0020_Date le datetime'2016-03-26T09:59:32Z'

Multiple Filters

/_api/web/lists/getbytitle('emp')/items?$filter=( Modified le datetime'2016-03-26T09:59:32Z') and (ID eq 2)

Title name starts with the letter P

/_api/web/lists/getbytitle('emp')/items?$filter=startswith(Title,‘P’)

Return all items from the 'emp'list modified in May

/_api/web/lists/getbytitle('emp')/items? $filter=month(Modified) eq 5

Wednesday, September 21, 2016

what is difference between Name and Account - /_vti_bin/listdata.svc/UserInformationList,



Result:

<content type="application/xml">
  <m:properties>
    <d:Id m:type="Edm.Int32">1</d:Id>
    <d:ContentTypeID>0x010A00F8C6531A37316E499095FDC0720C4D90</d:ContentTypeID>
    <d:ContentType>Person</d:ContentType>
    <d:Name>peakfinders\administrator</d:Name>
    <d:Modified m:type="Edm.DateTime">2015-04-30T16:50:53</d:Modified>
    <d:Created m:type="Edm.DateTime">2015-04-30T16:50:53</d:Created>
    <d:CreatedById m:type="Edm.Int32">1073741823</d:CreatedById>
    <d:ModifiedById m:type="Edm.Int32">1073741823</d:ModifiedById>
    <d:Owshiddenversion m:type="Edm.Int32">1</d:Owshiddenversion>
    <d:Version>1.0</d:Version>
    <d:Path>/_catalogs/users</d:Path>
    <d:Account>i:0#.w|peakfinders\administrator</d:Account>
    <d:EMail m:null="true" />
    <d:MobileNumber m:null="true" />
    <d:AboutMe m:null="true" />
    <d:SIPAddress m:null="true" />
    <d:IsSiteAdmin m:type="Edm.Boolean">true</d:IsSiteAdmin>
    <d:Deleted m:type="Edm.Boolean">false</d:Deleted>
    <d:Hidden m:type="Edm.Boolean">false</d:Hidden>
    <d:Picture m:null="true" />
    <d:Department m:null="true" />
    <d:JobTitle m:null="true" />
  </m:properties>
</content>


Tuesday, January 19, 2016

How to Copy List item to Another list using sharepoint designer 2013 workflow?

We can use the the REST services to move the items one to another list.
  • Add Dictionary is a new variable 
  • create a dictionary add Build Dictionary action
  • Accept and Content-Type
  •  set the values for both of them to application/json; odata=verbos
  • add key __metadata 
New item created from workflow 
call the web service
_api/web/lists/getbytitle('examples')/items and the HTTP method is POST. 
Set the request to your parameters dictionary. 
To set the header of your request go to :
Right click option

Call HTTP service -> properties -> RequestHeaders and set it to your header dictionary.
 

Tuesday, January 5, 2016

How to delete list item using Restapi in Sharepoint 2013

SharePoint 2013,/_api/web/lists,


function deleteListItem(url, listname, id, success, failure) {

    // getting our item to delete, then executing a delete once it's been returned
    getListItem(url, listname, id, function (data) {
        $.ajax({
            url: data.d.__metadata.uri,
            type: "POST",
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-Http-Method": "DELETE",
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "If-Match": data.d.__metadata.etag
            },
            success: function (data) {
                success(data);
            },
            error: function (data) {
                failure(data);
            }
        });
    });

};

Friday, January 1, 2016

Add list items restapi

function CreateListItemWithDetails(listName, webUrl, newItemTitle, success, failure) {
    var itemType = GetItemTypeForListName(listName);
    var item = {
        "__metadata": { "type": itemType },
        "Title": newItemTitle
    };

    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            success(data);
        },
        error: function (data) {
            failure(data);
        }
    });
}

// Get List Item Type metadata
function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}