By: Arjun Chakraborty
Today, I’d like to talk about how to create a WCF Web service for SharePoint 2010. As you may know, currently there are no standard templates for creating web services for SharePoint 2010. However, with a little tweaking, we can still create effective web services for SharePoint. Here is how I approached creating a WCF Web Service on SharePoint 2010:
NOTE*: these steps require activating ‘anonymous’ authentication in IIS. Despite anonymous authentication being active, SharePoint will still require you to sign in using your own credentials. It would seem, IIS credentials are independent to SharePoint credentials.
1) First, Create an Empty SharePoint Project on Visual Studio 2010. I’ll call this project WCFTest001. Make sure it is set it to Deploy as a Farm solution:
2) Save the Project. Then, right-click on the project and click on “Unload Project”.
Then, right click on the project again, and click on “Edit WCFTest001.csproj” (or whatever your project’s name is).
Then, add the following line:
Make sure this line is below <SandboxedSolution>False</SandboxedSolution>, which is already in the .csproj file!
Lastly, save the .csproj file, and click on reload project.
This is a very important step, because it sets the necessary namespaces for the service file mentioned in step 4.
3) Now, we need to add the _vti_bin to the SharePoint project. To do so, right click on the SharePoint Project, go to add, click on “SharePoint Mapped Folder…”
Select “ISAPI”, and press Okay.
You should now have an ISAPI folder under the project:
4) Right click on the ISAPI folder, and click on Add, and then New Item…. Select a text file under the General Tab. Name it however you like, but make sure it has .svc as its extension. I’ll call it WCFTest.svc.
Before we populate this file, first we need to create the code behind. To do this, right click on the solution file, and click on Add and then New Project. Add a project of type WCF Service Library. I called mine WCFLib, because we will only be taking 2 files from that project and then removing the project from the solution.
The two files we want are IService1.cs and Service1.cs. Copy those two using the Visual Studio Solution Explorer Pane, and paste them under WCFTest001:
Now, simply remove the WCFLib project from the solution. I would also go ahead and create a simple class under WCFTest001 so I can copy paste the project’s namespace. In this case, I left the class’s name as the default Class1.
Now, about those two files; The IService1 file declares the methods and objects that will be exposed by the web service. The Service1 file actually implements those methods and objects. Go ahead and rename them as you wish. I will rename them as Test001Service.cs and ITest001Service.cs. ALSO, I will change their namespace to that of this project!
At this point, you probably have a ton of errors. This is because we do not yet have all the necessary references. You will need to add the following references to your SharePoint project:
- Microsoft.SharePoint.Client.ServerRuntime.dll (this can be found in C:\windows\assembly\GAC_MSIL\Microsoft.SharePoint.Client.ServerRuntime\126.96.36.199__[something]\Microsoft.SharePoint.Client.ServerRuntime.dll )
At this point, all of your errors should have disappeared.
Take a look at the ITest001Service.cs file for a moment. You will see 4 kids of tags; [ServiceContract], [OperationContract], [CompositeType], and [DataMember]. If you want to expose a web method (And, of course you do. Why else would you make a web service?), then you have to use [ServiceContract], and [OperationContract]. [ServiceContract] designates a web service, and [OperationContract] designates a web method within that web service. If you want to use non-primitive objects as parameters or return values for your web methods, then you must use the [CompositeType], and [DataMember] tags. [CompositeType] designates an Class/Object, and [DataMember] designates a public property to or from which the client application (or whatever) stores or extracts data.
Also, let’s take a look at the Test001Service.cs because there are a couple important additions to make:
Notice the 4 using statements added inside the namespace, and the 2 tags added before the web service class. These are necessary.
Now that we know the cs file name, namespace and class name, we can finish up the .svc file. Open the WCFTest.svc file and paste:
<%@ ServiceHost Debug="true" Language="C#" Service="WCFTest001.Test001Service, $SharePoint.Project.AssemblyFullName$" CodeBehind="Test001Service.cs" %>
Note: you should change the values for the ‘Service’ and ‘CodeBehind’ attributes to fit your own service. The Service simply points to the full namespace and class name for the web service.
Lastly, save the .svc file.
5) Create the web.config file:
Right click on the ISAPI folder in Solution Explorer, and add another text file. This time, make sure to name it “web.config”.
Set the web.config contents to:
<?xml version="1.0" encoding="utf-8" ?>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<endpoint address="" binding="basicHttpBinding" contract="WCFTest001.ITest001Service">
<dns value="localhost" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
NOTE: make sure to edit the values I’ve set to red.
In the end, the project should look like this:
6) One more step before testing. We need to enable ‘Anonymous’ Authentication in IIS for the applicable web applications.
Go to IIS through start -> administrative tools.
Click on the applicable web application under the sites folder, and then click on the Authentication icon.
Then, enable Anonymous authentication:
7) Finally, right click on the project, and click deploy. Once Visual Studio says “Deploy Succeeded”, check out any SharePoint site to see if the web service exists. To do so, use the following url: site_url/_vti_bin/service_Name.svc.
In my case, its http://arjunsp01:10000/_vti_bin/wcftest.svc
When the SharePoint project is deployed, it is deployed as a wsp file into Central Administration. However, there are no features involved. This web service is now available to every site on the farm. However, it can only be used on the web applications where anonymous authentication is enabled.
The result should look something like this:
If you get this result, then the web service is working, and should be consumable by client applications and what not. Next time, I will go over how to create asmx web services for SharePoint, which is slightly easier.
This article only reflects my own experience with creating web services for SharePoint. If you have any complaints, questions or additional notes, please feel free to leave a comment.
Most of my knowledge regarding this matter came from:
· This excellent MSDN article: http://msdn.microsoft.com/en-us/library/ff521581.aspx
· Conversations with Microsoft technical support.
· Personal testing.
By: Arjun Chakraborty