As you may know, SharePoint 2010 can host WCF services in it’s environment. There are number of benefits to using WCF over old way of using SOAP. This is going to be one of at least 2 articles I’m planning to talk about WCF and SharePoint.
In this part I will talk about how you can use ListData.svc out of the box web service in your sample application. In next post I will cover how to create you custom WCF service and consume it through browser calls in XML or JSON.
ListData.svc is an out-of-the-box service allowing you to list any lists available on the current web and access items within those lists using web service calls.
Navigate to (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI) where you will find: ListData.svc. From the browser you can access this service by using relative URL: ~/_vti_bin/ListData.svc.
This means if you have a site collection http://localhost – you would call http://localhost/_vti_bin/ListData.svc; try and make the call. If you see XML back – that’s a good sign. If you got an error
Could not load type ‘System.Data.Services.Providers.IDataServiceUpdateProvider’
this means you first need to apply the following service pack: http://support.microsoft.com/kb/976127.
Try calling a web service and see if you get results back.
Now let’s try to call out list data service using JavaScript. Create a new HTM file and paste the following code in it.
<html> <head>
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$.getJSON("http://localhost/_vti_bin/ListData.svc/Calendar",function(data) {
var count = 0; $.each(data.d.results, function(i,result)
{ var title = result.Title;
html = "<table> \
<tr><td>" + title +"</td></tr> \
</table>";
$('#resultarea').append($(html));
});
});
});
</script>
</head> <body> <div id="resultarea"> </div> </body> </html>
This code assumes you have a list called Calendar on your root site of your site collection. Make sure you create one and add few items into the list. The code will iterate through the list and display titles of all calendar events you have in the list.
This concludes our short demonstration of out-of-the-box WCF services and calling them in JS.
Check back tomorrow to see how you can create your own SharePoint hosted REST services.
Good Luck!