Thursday, October 21, 2010

CRUD sample for list items using WCF data services and SPLINQ

The purpose of this post is to show how can we do CRUD operations on SharePoint 2010 lists using WCF data services. Actually SharePoint 2010 exposes list data trough REST (Representational State Transfer) service that you can consume in your projects. I will show now how can we use this in code. For this we will use also LINQ to SharePoint called SPLINQ. One advantage of this over CAML that is strong typed and an error can be discovered at compilation time for example.

We have to be aware that the REST based access is not supported for external lists.

For this example I created a custom list of towns that we will use. To this list I add some columns like Country, Population and Website. I also add some data.

listcolumns

listdefaultdata

Now, knowing that the data services are located in the “_vti_bin” directory and is called “ListData.svc” we can start programming. First we can create a simple console application and add a service reference. Give a name to the namespace. I called this CFGService.

service_reference

Next step is to add all required references to Microsoft.SharePoint.Linq and System.Net.
Now we can start writing the code and we will use the DataContext class that was generated when we added the service reference.

First, creating a new entry in the list. Because the DataContext class is strongly typed it knows about our list. We also need to add the credentials. In my example I added the DefaultCredentials.

We will create a new TownsItem object and this to the context by using AddToTowns method. At the end we have to save all the changes by calling SaveChanges. I also added the url as global variable.

public static string servUrl = "http://myserver:111/sites/Test2/_vti_bin/ListData.svc";





public static void CreateData()



 {



     CFGService.Test2DataContext ctx = new CFGService.Test2DataContext(new Uri(servUrl));



     ctx.Credentials = CredentialCache.DefaultCredentials;



 



     CFGService.TownsItem town = new CFGService.TownsItem();



     town.Name = "London";



     town.Country = "United Kingdom";



     town.Population = 7600000;



     town.Website = "http://www.london.gov.uk";



 



     ctx.AddToTowns(town);



     ctx.SaveChanges();



 }





To read the data existing in the list we have the function ReadAllData. Here we have an IEnumerable of TownsItem. Now we iterate through the collection and display the values.





public static void ReadAllData()



 {



     CFGService.Test2DataContext ctx = new CFGService.Test2DataContext(new Uri(servUrl));



     ctx.Credentials = CredentialCache.DefaultCredentials;



 



     IEnumerable<CFGService.TownsItem> towns = from t in ctx.Towns



                                               orderby t.Name



                                               select t;



     foreach (CFGService.TownsItem item in towns)



     {



         Console.WriteLine(String.Format("{0} - {1} - {2} - {3}", item.Name, item.Country, item.Population, item.Website));



     }



 



     Console.ReadKey();



 }




As alternative we can have this code to display only the name for example:





var cities = from c in ctx.Towns



              select c;



 



 foreach (var city in cities)



 {



     Console.WriteLine(city.Name);                



 }




Next, to update the Item we call the UpdateData method. We get the item with the required name and update the population number. Need to call the UpdateObject at the end in order to register the change on the server.





public static void UpdateData()



{



    CFGService.Test2DataContext ctx = new CFGService.Test2DataContext(new Uri(servUrl));



    ctx.Credentials = CredentialCache.DefaultCredentials;



 



    CFGService.TownsItem item = (from t in ctx.Towns



                                 where t.Name == "London"



                                 select t).Single();



    item.Population = 7700000;



    ctx.UpdateObject(item);



    ctx.SaveChanges();



}




To delete we call the DeleteData method.





public static void DeleteData()



{



    CFGService.Test2DataContext ctx = new CFGService.Test2DataContext(new Uri(servUrl));



    ctx.Credentials = CredentialCache.DefaultCredentials;



 



    CFGService.TownsItem item = (from t in ctx.Towns



                                 where t.Name == "London"



                                 select t).Single();



 



    ctx.DeleteObject(item);



    ctx.SaveChanges();



}




And this is it , a simple example to implement the CRUD operations.

No comments:

Post a Comment