Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Posts Tagged ‘rest’

Insert using REST API

Posted by Paul on January 23, 2016

In this article we can explore a simple code to Insert an Item using REST API.

Contacts List

Create a Contacts List for our purpose.

image

Script Inclusion

Following are the script files you need to include:

1. JQuery https://jquery.com/download/

2. JSON2 https://www.drupal.org/project/json2

You can copy the following script code with Microsoft & Yandex CDN links.

http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.0.min.js

http://yandex.st/json2/2011-10-19/json2.min.js

Insert Code

Following is the Insert Code to Insert an item.

function test()

{

CreateListItemWithDetails(“Contacts”, _spPageContextInfo.webAbsoluteUrl, “New Contact”, success, failure);

}

function success()

{

alert(“success”);

}

function failure()

{

alert(“failed”);

}

function CreateListItemWithDetails(listName, webUrl, newItemTitle, success, failure) {

var itemType = “SP.Data.” + listName + “ListItem”;

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);

}

});

}

Invoke Code

Following would be the Invoke Code.

<input type=”button” value=”Click Me..” onclick=”javascript:test();”

Testing Code

You can use a new page & Content Editor web part to test the above code. On successful insert you should be getting the message below.

image

You can also verify the new item in the Contacts list.

image

This concludes Insertion using REST API code.

References

http://bit.ly/1R7kB11

Summary

In this post we have explored how to Insert an Item using REST API.

Advertisements

Posted in SharePoint, SharePoint 2013 | Tagged: , , | Leave a Comment »

REST API to check User Permission

Posted by Paul on October 17, 2015

In this article we can explore the REST API to check User Permission for a Group.

Scenario

You are working in a client-side script SharePoint App. You need to check whether the current user belongs to the Approvers group. The twist is that the current user is added to an AD Group and in turn the AD Group was added to SharePoint group.

Solution: Following is the REST code to get the result.

http://sharepoint/_api/web/sitegroups/getbyname(‘Approvers’)/CanCurrentUserViewMembership

Following is the result.

image

image

You can try REST code in browser address bar itself. Later you can wrap a JavaScript or AngularJS around it to attain programmatic checking.

If I enter an invalid group name, the result will be different.

http://sharepoint/_api/web/sitegroups/getbyname(‘InvalidGroup’)/CanCurrentUserViewMembership

image

We can also check Site Collection & Sub Site permission groups using the same way.

Pre-Conditions

The above code is checking does the user have View Membership permission. In order to make this work we need to configure the following group property as true.

By default this property will be true.

image
You can find more property information using References link.

image

References

https://msdn.microsoft.com/en-us/library/office/jj245943.aspx

Summary

In this article we have explored the REST API to check User Permission for a Group.

Posted in SharePoint, SharePoint 2013 | Tagged: , , , , | Leave a Comment »

OData and Numeric Comparison Filters

Posted by Paul on June 21, 2015

In this article we can explore OData using Numeric Comparison filters.

Numeric Comparison Filters

Following are the Numeric Comparison filters available with OData

lt Less Than
le Less Than or Equals To
gt Greater Than
ge Greater Than or Equals To
eq Equal To
ne Not Equal To

Data Preparation

To proceed with the example I encourage you to create a Contacts list with a column named Age of type integer. Please fill some data appropriately.

clip_image002

eq Filter

Following is the example of using Equal filter where the column Age is equal to 30.

http://server/_api/Web/Lists/GetByTitle(‘Contacts’)/Items?$filter=Age eq 30

The result was one list item as per my data.

clip_image004

clip_image006

Please note that uppercase (EQ) won’t work.

ne Filter

Following is the example of using Not Equal filter where the column Age is not equal to 30.

http://server/_api/Web/Lists/GetByTitle(‘Contacts’)/Items?$filter=Age ne 30

Obviously it returned a lot of items.

clip_image008

lt Filter

Following is the example of using Less Than filter.

http://server/_api/Web/Lists/GetByTitle(‘Contacts’)/Items?$filter=Age lt 10

As there are no items with Age less than 10, my result was empty.

clip_image010

Similarly you can play with the remaining Arithmetic operators.

References

https://msdn.microsoft.com/en-us/library/gg309461.aspx

Summary

In this article we have explored Numeric Comparison filters of OData on SharePoint 2013 REST services.

Posted in SharePoint, SharePoint 2013 | Tagged: , , , , | Leave a Comment »

OData with REST

Posted by Paul on June 19, 2015

In this post we can get introduced to OData along with REST.

REST

REST stands for Representational State Transfer. It is a stateless communication protocol using HTTP and ensuring HTTP verbs are accurately used for CRUD operations. Please see references for more information.

SharePoint 2013 supports REST services which enabled an easier & better communication from client-side. We can use REST for:

1. Getting site information using URL

2. Getting list information using URL

3. Getting list items using URL

An example of REST URL is:

http://server/_api/Web/Lists/GetByTitle(‘Contacts’)

But, REST have drawbacks on querying and filtering. For example, using the URL format it is difficult to filter items.

OData

OData stands for Open Data Protocol. OData allows Filtering & Interoperability on REST services. We combine OData along with REST methods.

So REST + OData allows gives more flexibility in querying & filtering items.

Selecting Columns using OData

Following OData sample specifies selection of Title column only.

http://server/_api/Web/Lists/GetByTitle(‘Contacts’)/Items?$select=Title

image

FYI: The above query without the Title selection is as following.

http://server/_api/Web/Lists/GetByTitle(‘Contacts’)/Items

image

Although REST is stateless like HTTP, the sessions are stored in client-side and transferred back and forth from web server.

References

http://www.c-sharpcorner.com/UploadFile/40e97e/rest-enable-web-services-in-sharepoint/

https://en.wikipedia.org/wiki/Open_Data_Protocol

https://msdn.microsoft.com/en-us/library/office/fp142385.aspx

Summary

In this post we have explored usage of REST with OData in SharePoint 2013.  In the upcoming posts we can explore more options in OData.

Posted in SharePoint, SharePoint 2013 | Tagged: , , , | Leave a Comment »

More REST Examples

Posted by Paul on June 15, 2015

In this article we can explore more REST and examples.

Get a List by Title

We can get List information using the Title of the list. Make sure you have a list named Contacts.

URL: http://server/_api/Web/Lists/GetByTitle(‘Contacts’)

image

Please note that REST API is provided using the context _api.

Using chrome browser I received the following output.

image

Although IE 11 hesitated to show the output, giving an error message.

image

But in IE too, we can view the output using View Source option.

image

image

By default XML format result is provided by SharePoint REST services. In programmatic way, we can format these results to JSON format.

Getting List Items

We can try getting the list items using REST.

URL: http://sharepoint/_api/Web/Lists/GetByTitle(‘Contacts’)/Items

Using chrome browser I was able to view the following results.

image

However In IE the result summary was shown.

image

image

Please note that REST is trending as a replacement for JSOM in SharePoint world. REST provides much easier, less-coded ways of working with client-side object model.

References

https://msdn.microsoft.com/EN-US/library/office/jj164022.aspx

Summary

In this article we have explored few REST examples using browser URL box.

Posted in SharePoint, SharePoint 2013 | Tagged: , , , , | Leave a Comment »

Time to Move from SPServices to REST

Posted by Paul on May 22, 2015

In one of the SharePoint sessions, I found that the presenter is using SPServices.  Although, I have used SPServices before, I do not recommend using it for new projects, especially while SharePoint 2013 supports REST enabled services.

What is SPServices?

For those who are not aware of SPServices:

  • SPServices is a JQuery library which eases working with SharePoint ASMX web services
  • SPServices started supporting from MOSS 2007 to SharePoint 2010 to SharePoint 2013
  • SPServices is available from https://spservices.codeplex.com/

Following is an example of how SPServices looks like. (Courtesy)

image

What is REST?

A brief about REST enabled services in SharePoint 2013:

  • REST uses GET/POST/PUT/DELETE http-verbs for carrying the get/insert/update/delete operations on site, list, libraries
  • REST decreases the foot-print of application
  • REST supports JSON on top of XML enabling a broader client application integration with SharePoint
  • REST along with OData support provides advantages like filtering, combining etc.

Following is an example of how a REST service look like.

image

Please see References section for more examples.

Why to move to REST?

Following are the reasons Why we should move to REST from SPServices:

  1. More and more CSOM code are being exposed through REST
  2. SPServices was created before an era of CSOM and supports only XML
  3. In future ASMX web services can be deprecated creating a threat to existing SPServices solutions

Summary

Considering the above factors, I would not recommend new projects to be built using SPServices.  The new SharePoint Solution world is of REST and OData!

References

https://msdn.microsoft.com/en-us/magazine/dn198245.aspx

Posted in SharePoint, SharePoint 2013 | Tagged: , , | Leave a Comment »

Access Data using the REST API

Posted by Paul on March 26, 2015

In this article we can explore accessing data using the REST API in SharePoint 2013.

REST API

SharePoint 2013 introduce the REST API. These API allows SharePoint applications to do Data Operations from the client-side.

REST API usage from client-side is the main advantage of using it. This frees Server CPU cycles a lot.

The main operations we can perform are:

1. List CRUD Operations

2. List creation/updating/deletion operations

SharePoint lists including custom lists can be operated using the REST API. From the client-side a C# or JS application can invoke the REST API for the CRUD operation.

As sample of the REST call compatible with OData specification is given below.

· http://server/site/_api/lists/getbytitle(‘listname’)

Through new SharePoint service packs more and more server-side functionalities are made available through the REST services.

REST Specifications

As per the REST specifications, the HTTP verbs has to be used for the corresponding purpose.

· GET for retrieving data

· PUT to create data

· POST to update data

· DELETE for removing data

Depending on the Platform Infrastructure Polices the availability of DELETE & PUT verbs could be restricted. In this case contact the IT Support to enable them or use HTTP extension methods.

Advantages

To summarize the advantages of using REST API:

1. CRUD operations can be moved to client-side

2. Frees up server cycles & resources usage

3. Less line of code through the REST/OData syntax

4. Usage of HTTP verbs for CRUD operations

References

https://msdn.microsoft.com/en-us/library/office/fp142380.aspx

Summary

In this article we have explored accessing data using the REST API in SharePoint 2013. We can see live examples in the upcoming sections.

Posted in SharePoint, SharePoint 2013 | Tagged: , | Leave a Comment »

SharePoint & REST

Posted by Paul on January 10, 2015

SharePoint 2013 supports REST enabled web services.

REST

REST is Representational State Transfer. REST is an architectural style and uses HTTP protocol.

REST provides more interoperability than SOAP (web services).

REST is supposed to be the right way of using HTTP verbs for CRUD operation. (Create – POST, Read – GET, Update – Put, Delete – DELETE) as per protocol definition in RFC 2616.

image

Example

Following are few examples of REST.

image

Advantages

Following are the advantages of going with REST:

1. REST web requests are easier to work with

2. No overheads of WSDL creation

3. No overheads of tying up with Service Reference

4. Direct Calls for fetch, insert, update, delete, find operations

5. Better Interoperability supporting different clients on different platforms, programming models

SharePoint & REST

SharePoint 2013 supports REST. This feature requires some inner tweaking of the existing web services.

In SharePoint the usage of HTTP Verbs as per REST is given below:

1. POST for Create

2. GET for Read

3. PUT for Update

4. DELETE for Delete

Operations

Following are the subset of operations we can perform using SharePoint & REST:

1. Create a Site Collection

2. Create a List

3. Create a Content Type

4. Add, Edit, Delete items from a List

5. Get an Item

X-HTTP-Method

Sometimes network firewalls block certain HTTP Verbs like PUT and DELETE. In this case we have to rely on X-HTTP-Method which is actually a header.

Syntax is:

XHTTPMethod = “X-HTTP-Method: ”

(“PUT”

/ “MERGE”

/ “PATCH”

/ “DELETE”)

CRLF

REST services of SharePoint are comparable with Client Side Object Models. We can use REST services as an alternative to CSOM or JSOM code.

References

http://msdn.microsoft.com/en-us/library/office/fp142380(v=office.15).aspx

Summary

In this article we have explored SharePoint & REST.

Posted in SharePoint, SharePoint 2013 | Tagged: , | Leave a Comment »