Introduction
How could we perform CRUD operations on SharePoint 2010 List from a WCF Service? Assume we need to create a WCF Service and that service will perform CRUD operation on a SharePoint 2010 list. We are going to see that in this article.

Now we will follow the below steps to perform all the above operations.
Step 1: Setting up the SharePoint list
We have a custom list
- Name of the list is Test_Product.
- Columns of the list are defined as below.

There are two items in the list:

The URL of the SharePoint site is
http://dhananjay-pc/my/personal/Test1
Now we will access the SharePoint site on above URL.
Step 2 : Creating the WCF Service
Now we will create a WCF Service. WCF Service will have four operation contracts for CRUD operations on list.
To create the WCF service, open visual studio and from the WCF tab select WCF Service application.

Now to make sure that we would be able to use SPLinq or LINQ to SharePoint we need to change the target framework and Platform.
Steps are:
1. Right click and click on Properties of WCF Service.

2. Click on the Build tab and change Platform Target to Any CPU.

3. Click on the Application tab and change the Target framework type to .Net Framework 3.5.

Now we need to create the DataContext class of the SharePoint list such that we can perform LINQ against that class to perform CRUD operation. To create the context class we need to perform the below steps.
1. Open the command prompt and change directory to
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN
Type the command CD C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

2. Now we need to create the class for corresponding list definitions.
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN> spmetal.exe /web:http://dhananjay-pc/my/personal/Test1 /namespace:nwind /code:Product.cs
In the above command we are passing a few parameters to spmetal.exe, they are explained below.
/web:Url: Here we need to provide the URL of the SharePoint site.
/web:http://dhananjay-pc/my/personal/Test1 /
/namespace:nwind: This would be the namespace under which the class of the list will get created. In my case name of the namespace would be nwind.
/code:Product.cs: This is the file name of the generated class. Since we are giving name Product for the file then the generated class will be ProductDataContext.
The classes by default get saved in the same folder as SPMetal.exe. So to see where the class got created we need to navigate to the folder
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN
Now add this class to the project. To do this, right click on the WCF Service project and select Add existing item. Then browse to above path and select Product.cs.
Step 3. Host the WCF Service in IIS
To work with SPLInq in WCF service, we need to host the service in IIS with the same application pool SharePoint is running.
See the below link for step by step explanation on how to host WCF 4.0 Service in IIS 7.5:
http://dhananjaykumar.net/2010/09/07/walkthrough-on-creating-wcf-4-0-service-and-hosting-in-iis-7-5/

Select the Application pool to SharePoint-80
Step 4. Create Contracts
We need to create a DataContract or Data Transfer object. This class will represent the Product Data context class (We generated this class in the previous step).
Now add some references to work with LINQ to SharePoint:
- Microsoft.SharePoint
- Microsoft.SharePoint.Linq
Right click on Reference and select Add Reference. To locate Microsoft.SharePoint and Microsoft.SharePoint.Linq dll browse to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI. All the SharePoint dll are here in this location.
Add the following using directives.
Nwind is the name of the namespace of the data context class we created in previous steps. We need to make sure that we have added Product class to the WCF Service application project as Add an Existing item.
Create the Service Contract
We need to create the Service contract. There would be four operation contracts, one for each operation on the Sharepoint list.
Listing 1: IService1.cs
Implement the Service
Now we need to implement the service to perform the CRUD operations.
Listing 2: Retrieving all the list items
The above code performs the following logic.
- Creating instance of ProductDataContext.
- Using LINQ to SharePoint retrieving all the list items.
- Creating instance of ProductDTO class with values fetched from SharePoint list
- Adding the instance of ProductDTO class in List of ProductDTO.
- Returning list of ProductDTO
Listing 3: Inserting an element in List
In the above code
- Boolean is being returned. If item will get saved successful then
true will be returned by the service else false.
- An instance of ProductDataContext is created and
submitchanges() will be called on instance of ProductDataContext.
- An instance of Test_ProductItem is being created and we are passing values from ProductDTO to create instance of Test_ProductItem. Point we need to note here is that Test_Product is name of the SharePoint list.
- We're calling
InsertOnSubmit() to submit a list to item to get inserted.
Listing 4: Updating an Element in the List
In the above code
- We are fetching list item to be updated on basis of
ProductId of ProductDTO.
- We're Assigning the new values
- We're calling
submitchanges on instance of ProductContext class.
Listing 5: Deleting an Element from the List
In the above code
- We are fetching the list item to be deleted.
- We're calling
DeleteOnSubmit() and passing the item to be deleted.
- We're calling
SubmitChanges() on instance of ProductContext.
Modify the Web.Config
- We will put Binding as basicHttpBinding.
- In ServiceBehavior attribute , we will enable the servicemetadata
- In ServiceBehavior , we will make IncludeserviceException as false.
- We will configure MetadataExchnagePoint in the service
Now in Web.Config the Service configuration will look like so:
Now our service is complete. We will be hosting this service in IIS as we discussed in previous step. From IIS we will browse to test whether service is up and running or not.
For reference, the full source code for the service implementation is reported below.
Listing 6: Service1.svc.cs
Creating a Managed Application (Console client to consume Service)
Now we will create a new project by selecting console application as project type. Right click on the project and add a service reference. Give the URL of the WCF Service hosted in IIS.
We need to call the service to perform the CRUD operation on SharePoint list from the console client.
Listing 7: Program.cs
Calling the service is very simple. We just need to create a proxy of the service and call the methods on the service as normal functions with required parameters.
Figure 10: Output

About Dhananjay Kumar
 |
Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter @debug_mode
This author has published 8 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Make SharePoint 2007 Act Like SharePoint 2010, Updated
read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 18: Custom Linq Provider
read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update Summary
read more
SharePoint Search-as-You-Type with jQuery
read more
WCF defaults stifles loose coupling
read more
LINQ in Action samples in LINQPad
read more
My SharePoint Sessions at the Dutch DevDays
read more
Sharepoint for Developer Series by Kirk Evans
read more
Dev Connections - The Code!
read more
More reminders / gotchas from the trenches with Azure
read more
|
|
Please login to rate or to leave a comment.