Tuesday, June 19, 2012

How to create , deploy and consume WCF service in SharePoint 2010

In my previous post show how to create An ASMX web service in SharePoint 2010.

Here now am trying to show How to create, deploy and consume WCF service in SharePoint 2010.

1. Create Empty SharePoint Project.

2. Specify SharePoint site location for debugging.

starting solution explorer look like this.

3. Right Click on the project on select "Unload Project".

4. Again Right Click project and Select "Edit" project file.

5. Add <TokenReplacementFileExtensions>svc</TokenReplacementFileExtensions> after <sandboxedsolution> tag.

6.Now save the file and close it. Then Right Click on project file and select "Reload Project".

7. Now add SharePoint Mapped Folder to the project.

8. Select "ISAPI" folder and Click on "OK".

9.Now create Folder in side the ISAPI folder and add service file i.e .svc file to the newly created folder.

10.  Now add web.config file to the subfolder in ISAPI.

11. Now i want to create service file for this i need one interface and one class file. Here am getting these two file from wcf service project. For this am adding one more project to my solution and later i will remove this project.

12. Creating new project using "WCF Service Library".

13. Here am taking two service file from the WCF prject and will be add to the previous project i.e where we are creating wcf service.

14. Adding Service file from the WCF service.

15. Now am removing WCF project from the solution.

16. After removing WCF project Solution Explorer look like this.

17. we can rename to the service class and service interface but am continuing with those name only.
 now am adding references to the project.
  I ) Microsoft.SharePoint.Client.ServerRuntime.dll and location for the dll is: C:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint.Client.ServerRuntime\\Microsoft.SharePoint.Client.ServerRuntime.dll
III) System.ServiceModel

18. By default IService1.cs have two methods and now am adding one more method which will return SharePoint Site Title.

19. Writing method implementation in Service1.cs file and this file looks like this.

20. Upto now methods and their implementations are over.
Now open wcf service file i.e .svc file and write the following single line of code.
<%@ ServiceHost Language="C#" Debug="true" Service="UKWCFService.Service1, $SharePoint.Project.AssemblyFullName$" CodeBehind="Service1.cs" %>

Here UKWCFService is the namespace in Service1.cs and Service1 is the class name in Service1.cs

21. Now open Web.config and add the following code.

<?xml version="1.0" encoding="utf-8" ?>





        <behavior name="UKWCFService.Service1ServiceBehavior">

          <serviceMetadata httpGetEnabled="true" />

          <serviceDebug includeExceptionDetailInFaults="false" />





      <service behaviorConfiguration="UKWCFService.Service1ServiceBehavior"


        <endpoint address="" binding="basicHttpBinding" contract="UKWCFService.IService1">


            <dns value="localhost" />



        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />





and now change the highlighted names.

UKWCFService.Service1ServiceBehavior -- here UKWCFService is namespace in Service1.cs and Service1 is the class name in Service1.cs and ServiceBehavior is same.
UKWCFService.Service1 -- here also UKWCFService is namespace in Service1.cs and Service1 is the class name in Service1.cs

22. Now Save all your file and Deploy the project.

23. Now we can test our deployed wcf service using WCF Test Client(open visual studio tools --> Visual Studio Command Prompt and run the commant wcftestclient)

24. Testing Service method from here

SO My Custom WCF Service Deployed Successfully and Working fine.

Now i want to Consume my custom wcf service in visual webpart and invoke service methods.

1. Create SharePoint Project using Visual Web part as template

 2. Specify the SharePoint location for debugging.

Placing one text box , one button and one label in user control and Designed in a order. 

Now adding Service reference. Right click on project select add service reference.

After adding the service reference solution explorer look like this

Now open usercontrol design and double click on button and write the following code in button click event

 private static string websiteUrl = "http://demoshare:6969";

 BasicHttpBinding binding = new BasicHttpBinding();
 binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
 binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
 EndpointAddress endpoint = new EndpointAddress(websiteUrl + "/_vti_bin/udaywcf/ukreddywcf.svc");
 ConsumeUKReddyWCF.UdayWcfService.Service1Client proxy = new ConsumeUKReddyWCF.UdayWcfService.Service1Client(binding, endpoint);

proxy.ClientCredentials.Windows.AllowedImpersonationLevel =

string str = proxy.GetSiteName();

  if (txtNumber.Text.Trim() != "")
                str += " and " + proxy.GetData(Convert.ToInt32(txtNumber.Text));
  lblMsg.Text = str;

 ConsumeUKReddyWCF.UdayWcfService.Service1Client -- here ConsumeUKRReddyWCF is the namespace and UdayWcfSErvice is the service reference name and Service1Client is the class name.

Now deploy the project.

Here I added webpart to my home page.

Now we can see the Result from the web service. which returning site title name and text box entered value.

WCF service Source Code

WCF Consuming Webpart Source Code

Any Doubt or need clarification please feel to send mail at: s.ukreddy@gmail.com