Monday, August 8, 2011

REST web services

WEB SERVICES IN SHREPOINT

Rest Web Service can be used for

1: To get the List Items: This is possible by using REST web service of SharePoint server. We can get all the list items in xml by giving list name as parameter in the URL.

Syntax: http ://< site name>/_vti_bin/listdata.svc/

Ex: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise

2: To get the List in Sorted Order: List items can be sorted by using the following syntax: http:///_vti_bin/listdata.svc/?$orderby=Title desc, Name asc (returns List with Title in descending order and Name in ascending)

Sorting with “$orderby” keyword

Ex: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise?$orderby=Title asc, Discipline desc

3: To Filter list item: List items can be filtered by using the following Syntax: http:///_vti_bin/listdata.svc/?$filter=Title eq 'Title_Name' (returns List with Title = 'Title_Name')

Ex: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise?$filter=Title eq 'soft'

Filtering with “$filter” keyword in URL/Query string.

4: To retrieve items on page wise:

Syntax: http ://< site name>/_vti_bin/listdata.svc/? $skip=[n]&$top=[n]&$orderby=Title

The $skip and $top parameters are ideal for implementing a paging mechanism. The $skip=[n] parameter allows you to skip the first n options, the $top=[n] parameter allows you to return the next n items. Typically, you will use this in conjunction with the $orderby parameter (appended using the & (ampersand) symbol).

Ex: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise? $skip=5&$top=1&$orderby=Title

5: To select specific column:

Syntax: http:///_vti_bin/listdata.svc/?$ select = ,,..

EX: http://sharepoint-webaddress/_vti_bin/listdata.svc/Expertise?$select = Title

Resource: http://www.lcbridge.nl/vision/2010/rest.htm

6: To limit on number of rows returned – In SharePoint, Site Collection Administrator can set the limit of number of rows returned & also programmers can override this value as per their requirements.

Resource: 1: http://msdn.microsoft.com/en-us/library/gg985387.aspx

2: https://willhlaw.wordpress.com/tag/sharepoint-2010/

Consuming REST Service with Linq in Visual Studio Application:

1) To consume a Restful web service in Visual Studio, you can go to the Data menu and click Add New Data Source.
2) In the Add Service Reference dialog, add the Url to a SharePoint 2010 web site.
3) Go to the Data menu again and this time, select Show data sources and select List
4) Add Reference

Using RestDemo.ServiceReference1;

Using System.Net;

Create a new class-level variable to hold the data context

TeamSiteDataContext context = new TeamSiteDataContext (new Uri(http://sp2010a/teamsite/_vti_bin/listdata.svc));

5) Next, in the Form_Load event, add the following two lines.

context.Credentials = CredentialCache.DefaultCredentials;

contactsBindingSource.DataSource = context.Contacts;

The first line will authenticate to SharePoint using your current logged-in account. Of course, other login methods are available. The second references the Contacts entity. The contactsBindingSource is an object automatically created by Visual Studio when you added the Contacts list to your form. The data source for the GridView automatically points to this data source.

We’ve seen above that REST allows you filter and sort. What if you wanted to be more efficient and filter and sort in application? You can and one way you can do this is with LINQ:

var q = from c in context.Contacts

where c.ID > 100

orderby c.FirstName

select new { c.FirstName, c.LastName, c.EMailAddress, c.BusinessPhone };

contactsBindingSource.DataSource = q;

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...