SharePoint 2010 – REST Services
Liam Cleary
Tue, Mar 9th 11:43 AM
div class="ExternalClass92FECD0735D7463596B78B2813437482"
pAs you may all be aware SharePoint 2010 now supports the use of REST Services for retrieval of content from lists and libraries etc. This approach works well as the REST Services data can easily be consumed either inside or outside SharePoint. If we access a list within SharePoint we can see I have a tasks list with two items in it. I can view them normally in the UI as shown below: /p
pimg alt="" src="http://www.helloitsliam.com/Lists/Photos/030910_1616_SharePoint21.png" /p
pIf we now modify the URL that is being used from a href="http://websiteaddress/sitename/Lists/Tasks/AllItems.aspx"http://websiteaddress/sitename/Lists/Tasks/AllItems.aspx/a to be a href="http://websiteaddress/sitename /_vti_bin/ListData.svc/Tasks"http://websiteaddress/sitename /_vti_bin/ListData.svc/Tasks/a we now get a different view of the content. /p
pimg alt="" src="http://www.helloitsliam.com/Lists/Photos/030910_1616_SharePoint22.png" /p
pThis is the RSS feed view of the content; we can subscribe to it etc or even filter the results by tweaking the URL. If we modify the URL to the following instead we get different views of the content. /p
pa href="http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$filter=StatusValue eq 'In Progress'"http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$filter=StatusValue eq 'In Progress'/a /p
pimg alt="" src="http://www.helloitsliam.com/Lists/Photos/030910_1616_SharePoint23.png" /p
pYou can add other filters or even sort by parameters such as the following: /p
pa href="http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$orderby=Title"http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$orderby=Title/a /p
pa href="http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$orderby=Priority"http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$orderby=Priority/a /p
pYou are also able to use predicates such as: ne, gt, ge, lt, le, and, or, not /p
pSo you could create the following URLs: /p
pa href="http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$filter=StatusValue eq 'In Progress' and Title eq 'Test Task'amp;$orderby=Priority"http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$filter=StatusValue eq 'In Progress' and Title eq 'Test Task'amp;$orderby=Priority/a /p
pa href="http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$filter=StatusValue eq 'In Progress' and Title eq 'Test Task'amp;$orderby=Priority"http://websiteaddress/sitename/_vti_bin/ListData.svc/Tasks?$filter=StatusValue eq 'In Progress' or StatusValue eq 'Not Started'amp;$orderby=Priority/a /p
pThis is great but how can we consume then REST Services into our custom components. Well firstly we need to realize that we can call these REST Services in different ways to get different types of content back. The first will return the results as shown above in quot;strongATOM+XML/strongquot; format, the other way is to call them and return quot;strongJSON/strongquot;. This can be done in a couple of ways, firstly we can modify the quot;strongheader/strongquot; value that calls the service or we can use a client side script from jQuery to do this. As an example I have created a simple console application that takes a few parameters and then produces either the default of quot;atomquot; or quot;jsonquot;. The code I am using is as follows: /p
pimg alt="" src="http://www.helloitsliam.com/Lists/Photos/030910_1616_SharePoint24.png" /p
pNothing special here except the line for changing the quot;strongAccept/strongquot; header to be quot;strongapplication/json/strongquot;. To run the application I used the following command line: /p
pMyConsoleAPplication.exe quot;a href="http://websiteaddress/sitename/_vti_bin/listdata.svc/"http://websiteaddress/sitename/_vti_bin/listdata.svc//aTasksquot; quot;usernamequot; quot;passwordquot; quot;domainquot; quot;jsonquot; /p
pMyConsoleApplication.exe quot;a href="http://websiteaddress/sitename/_vti_bin/listdata.svc/Tasks"http://websiteaddress/sitename/_vti_bin/listdata.svc/Tasks/aquot; quot;usernamequot; quot;passwordquot; quot;domainquot; quot;quot; /p
pThe first one will output quot;JSONquot;, the second just normal quot;ATOMquot; XML. /p
pspan style="text-decoration:underline"strongATOM Results /strong/span/p
pimg alt="" src="http://www.helloitsliam.com/Lists/Photos/030910_1616_SharePoint25.png"span style="text-decoration:underline"strong /strong/span/p
pspan style="text-decoration:underline"strongJSON Results /strong/span/p
pimg alt="" src="http://www.helloitsliam.com/Lists/Photos/030910_1616_SharePoint26.png" /p
pThese are different result sets that can be used for different approaches for data consuming. We could even go as far as using something like this: /p
pspan style="font-family:Courier New;font-size:10pt"jQuery.ajaxSetup({ beforeSend: function(xhr) { xhr.setRequestHeader(span style="color:#a31515"quot;Acceptquot;/span, span style="color:#a31515"quot;application/jsonquot;/span); }}); /span/p
pspan style="font-family:Courier New;font-size:10pt"jQuery.get(span style="color:#a31515"'http://websiteaddress/sitesname/_vti_bin/listdata.svc/Tasks'/span, function(data) { /span/p
pspan style="font-family:Courier New;font-size:10pt"alert(data); /span/p
pspan style="font-family:Courier New;font-size:10pt"});/span /p
pWe could even use the following jQuery method instead: /p
pspan style="font-family:Courier New;font-size:10pt"$(document).ready(function() { /span/p
p /p
pspan style="font-family:Courier New;font-size:10pt"$.getJSON(span style="color:#a31515"quot;http://websiteaddress/sitesname/_vti_bin/listdata.svc/Tasksquot;/span,function(data) { /span/p
pspan style="font-family:Courier New;font-size:10pt"$.each(data.d.results, function(i,result) { /span/p
pspan style="font-family:Courier New;font-size:10pt"span style="color:green"// Do something with the JSON/span /span/p
pspan style="font-family:Courier New;font-size:10pt"}); /span/p
pspan style="font-family:Courier New;font-size:10pt"}); /span/p
pspan style="font-family:Courier New;font-size:10pt"});/span /p
pAs you can see there are multiple ways of getting this data and then consuming it within our applications and components. This for me is a massive bonus for aggregation and consuming of content from SharePoint. /p
p /p/div
pa href="http://feedads.g.doubleclick.net/~a/IaLHqcAp7qEcCcl3J-QrYjIXzsY/0/da"img src="http://feedads.g.doubleclick.net/~a/IaLHqcAp7qEcCcl3J-QrYjIXzsY/0/di" border="0" ismap="true"/img/abr/
a href="http://feedads.g.doubleclick.net/~a/IaLHqcAp7qEcCcl3J-QrYjIXzsY/1/da"img src="http://feedads.g.doubleclick.net/~a/IaLHqcAp7qEcCcl3J-QrYjIXzsY/1/di" border="0" ismap="true"/img/a/pdiv class="feedflare"
a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:yIl2AUoC8zA"img src="http://feeds.feedburner.com/~ff/helloitsliam?d=yIl2AUoC8zA" border="0"/img/a a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:dnMXMwOfBR0"img src="http://feeds.feedburner.com/~ff/helloitsliam?d=dnMXMwOfBR0" border="0"/img/a a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:F7zBnMyn0Lo"img src="http://feeds.feedburner.com/~ff/helloitsliam?i=-yACUN7Vpg0:hycHoqSqedA:F7zBnMyn0Lo" border="0"/img/a a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:7Q72WNTAKBA"img src="http://feeds.feedburner.com/~ff/helloitsliam?d=7Q72WNTAKBA" border="0"/img/a a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:V_sGLiPBpWU"img src="http://feeds.feedburner.com/~ff/helloitsliam?i=-yACUN7Vpg0:hycHoqSqedA:V_sGLiPBpWU" border="0"/img/a a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:qj6IDK7rITs"img src="http://feeds.feedburner.com/~ff/helloitsliam?d=qj6IDK7rITs" border="0"/img/a a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:KwTdNBX3Jqk"img src="http://feeds.feedburner.com/~ff/helloitsliam?i=-yACUN7Vpg0:hycHoqSqedA:KwTdNBX3Jqk" border="0"/img/a a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:l6gmwiTKsz0"img src="http://feeds.feedburner.com/~ff/helloitsliam?d=l6gmwiTKsz0" border="0"/img/a a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:gIN9vFwOqvQ"img src="http://feeds.feedburner.com/~ff/helloitsliam?i=-yACUN7Vpg0:hycHoqSqedA:gIN9vFwOqvQ" border="0"/img/a a href="http://feeds.feedburner.com/~ff/helloitsliam?a=-yACUN7Vpg0:hycHoqSqedA:TzevzKxY174"img src="http://feeds.feedburner.com/~ff/helloitsliam?d=TzevzKxY174" border="0"/img/a
/divimg src="http://feeds.feedburner.com/~r/helloitsliam/~4/mZt5H43tLss" height="1" width="1"/
Open Post:
http://feedproxy.google.com/~r/helloitsliam/~3/mZt5H43tLss/sharepoint-2010-–-rest-services.aspx
Open with Mowser:
http://mowser.com/web/http://feedproxy.google.com/~r/helloitsliam/~3/mZt5H43tLss/sharepoint-2010-–-rest-services.aspx