@debugmode_ Dhananjay Kumar http://dhananjaykumar.net/ Microsoft-MVP-Connected System LINQ to SharePoint with WCF 4.0 Objective 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 SharePoint 2010 list. We are going to see that in this article. Flow Diagram Now we will follow the below steps to perform all above operations.
17
Embed
LINQ to SharePoint with WCF 4 · 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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
@debugmode_ Dhananjay Kumar http://dhananjaykumar.net/ Microsoft-MVP-Connected System
LINQ to SharePoint with WCF 4.0
Objective
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 SharePoint 2010 list. We are
going to see that in this article.
Flow Diagram
Now we will follow the below steps to perform all above operations.
@debugmode_ Dhananjay Kumar http://dhananjaykumar.net/ Microsoft-MVP-Connected System
Select the Application pool to SharePoint-80
Create Contracts
We need to create DataContract or Data Transfer object. This class will represent the Product Data
context class (We generated this class in previous step) 0
Create Data Contract,
[DataContract] public class ProductDTO { [DataMember] public string ProductName; [DataMember] public string ProductId; [DataMember] public string ProductPrice; }
Add 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.
@debugmode_ Dhananjay Kumar http://dhananjaykumar.net/ Microsoft-MVP-Connected System
Add the namespace
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 Service Contract
We need to create Service contract. There would be four operation contracts each for one operation on
Sharepoint list.
IService1.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Runtime.Serialization; using System.ServiceModel.Description; namespace WcfService1 { [ServiceContract ] public interface IService1 { [OperationContract] List<ProductDTO> GetProduct(); [OperationContract] bool InsertProduct(ProductDTO product); [OperationContract] bool UpdateProduct(ProductDTO product); [OperationContract] bool DeleteProduct(ProductDTO product); }
Implement the Service
Now we need to implement the service to perform operations
@debugmode_ Dhananjay Kumar http://dhananjaykumar.net/ Microsoft-MVP-Connected System
Retrieving all the list items
public List<ProductDTO> GetProduct() { using (ProductDataContext context = new ProductDataContext("http://dhananjay-pc/my/personal/Test1")) { ProductDTO product; List<ProductDTO> lstProducts = new List<ProductDTO>(); var result = from r in context.Test1_Product select r; foreach (var r in result) { product = new ProductDTO { ProductId = r.ProductId, ProductName = r.ProductName, ProductPrice = r.ProductPrice.ToString () }; lstProducts.Add(product); } return lstProducts; } }
In above code
1. Creating instance of ProductDataContext.
2. Using LINQ to SharePoint retrieving all the list items.
3. Creating instance of ProductDTO class with values fetched from SharePoint list
4. Adding the instance of ProductDTO class in List of ProductDTO.
5. Returning list of ProductDTO
Inserting an element in List
public bool InsertProduct(ProductDTO product) { try { using(ProductDataContext context = new ProductDataContext("http://dhananjay-pc/my/personal/Test1")) { EntityList<Test1_ProductItem> products = context.GetList<Test1_ProductItem>("Test1_Product");
Now our service is created. 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, full source code for service implementation is as below ,
Service1.svc.cs
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; using Microsoft.SharePoint.Linq; using Microsoft.SharePoint;
@debugmode_ Dhananjay Kumar http://dhananjaykumar.net/ Microsoft-MVP-Connected System
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.
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using ConsoleApplication1.ServiceReference1; using System.Collections.Generic; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { Service1Client proxy = new Service1Client(); // Insertion ProductDTO productToInsert = new ProductDTO { ProductId = "99", ProductName = "Ball", ProductPrice = "700" }; if (proxy.InsertProduct(productToInsert)) Console.WriteLine("Inserted successfully "); else Console.WriteLine("Opps some problem in Insertion "); // Update ProductDTO productToUpdate = new ProductDTO { ProductId = "99", ProductName = "Ball for New game ", ProductPrice = "7000" }; if (proxy.InsertProduct(productToInsert)) Console.WriteLine("Updated successfully "); else Console.WriteLine("Opps some problem in Updation "); //Delete ProductDTO productToDelete = new ProductDTO { ProductId = "99" }; if (proxy.InsertProduct(productToInsert)) Console.WriteLine("Deleted successfully "); else Console.WriteLine("Opps some problem in Deletion ");