Monday, May 27, 2013

Create and Consume a simple WCF service to add new list item in SharePoint 2010

Step-1 :
Create a new WCF Application project from Visual Studio and named it "Wcf_ListService"

Code for IService.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Wcf_ListService
{
  // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface     name "IService1" in both code and config file together.
 [ServiceContract]
 public interface IService
    {
     [OperationContract]
     string CreateCustomerList(string siteUrl, clsCustomers objCustomer);
    }


// Use a data contract as illustrated in the sample below to add composite types to service operations.
  [DataContract]
  public class clsCustomers
  {
   string _customerName = "";

    [DataMember]
    public string CustomerName
    {
      get { return _customerName; }
      set { _customerName = value; }
    }
  }
}


Code for Service.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;

namespace Wcf_ListService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service : IService
{ 
public string CreateCustomerList(string siteUrl, clsCustomers objCustomer)
{
 string Message = string.Empty;

  try
   {
     SPWeb myWeb = null;

      //The following code must be executed with Elevated rights.
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {

   //Use SPUserTaken'sSystemAccount property so that call for the WCF service hosted on IIS
   //can be made to the SharePoint site.
     using (SPSite mySiteCollection = new SPSite(siteUrl, SPUserToken.SystemAccount))
      {
       mySiteCollection.AllowUnsafeUpdates = true;
       myWeb = mySiteCollection.OpenWeb();
      }
   });


 if (myWeb != null)
 {
   myWeb .AllowUnsafeUpdates = true;
   SPList list = myWeb .Lists["lstCustomers"];
   SPListItem Item = list.Items.Add();
   Item["Title"] = objCustomer.CustomerName;
   Item.Update();
   myWeb.AllowUnsafeUpdates = false;
   Message = "Customer Added Successfully";
  }
 else
  {
    Message = "Error";
  }

}
catch (Exception ex)
 {
   Message = ex.Message;
 }
return Message;
        }
    }
}


Step-2:
Our wcf service is ready now build the application.

Step-3:

Consuming WCF Service:

1- Create a ConsoleApplication/WindowApplication/WebApplication 
2- Right click on project and click Add Service Reference
3- A window will appear, click on Discover after that you will see your service listed below.
4- Give it a name as "CustomerRef", click on ok.
5- Now your wcf service reference added to your project.


Here is the code I used in my Consumer site. 

CustomerRef.ServiceClient proxy = new CustomerRef.ServiceClient();
CustomerRef.clsCustomers customers = new CustomerRef.clsCustomers()
    {
      CustomerName="mohit"
    };

string Msg = proxy.CreateCustomerList("http://myServer/sites/mySite/", customers);
Response.Write(Msg);


Thanks...........
--------------------------------





No comments:

Post a Comment