SharePoint Events

  5/06/2014 - Webcast: Advanced Workflow, Forms and Mobility in Office 365 and SharePoint 2013 from Nintex
  5/07/2014 - Webcast: View, Annotate and Secure your Content Anywhere and on Any Device in SharePoint 2013
  5/08/2014 - Webcast: Advanced Records Management with SharePoint
  5/29/2014 - Webcast: Enhance Productivity, Usability and Relevance with Advanced Search in SharePoint 2013

 SharePoint Videos

  Why SharePoint 2013
  Keys to Successful SharePoint Initiatives and User Adoption
  Out of the Box Business Intelligence and Reporting in SharePoint 2013
  Driving the Business Case and User Adoption for SharePoint
  Automate Business Processes with SharePoint 2013 and Business Connectivity Services
  SharePoint and Office 365 Migration Made Easy
  Full Lifecycle Content Management with SharePoint 2013
  Compliant Document Generation and Assembly in SharePoint
  Application of Content Security and Corporate Risk Protection in SharePoint
  How to Redline, Markup, Collaborate and Review Content in SharePoint
  Advanced Content Lifecycle and Records Management in SharePoint
  Advanced Workflow Made Easy with SharePoint 2013 and Nintex
  Full Lifecycle Content Management with SharePoint 2013
  SharePoint 2013 Solutions for the Financial Services Industry
  ProjectReady 2013: SharePoint Solutions built for the AEC
  SharePoint 2013 Solutions for the Legal Industry
  Driving the Business Case and User Adoption for SharePoint
  Investment Considerations for SharePoint 2010 and SharePoint 2013
  SharePoint 2013 and Enterprise Content Management
  SharePoint 2013 Launch
(More Links...)

 Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
1 - 10 Next
How to Create WCF Web Service on SharePoint 2010

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:
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

      The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
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).
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
Then, add the following line:
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
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.
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
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…”
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
Select “ISAPI”, and press Okay. 
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

You should now have an ISAPI folder under the project:

The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

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_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
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:
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
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.
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
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!
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

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:

- System.ServiceModel
- System.Runtime.Serialization
- Microsoft.SharePoint.Client.ServerRuntime.dll  (this can be found in C:\windows\assembly\GAC_MSIL\Microsoft.SharePoint.Client.ServerRuntime\14.0.0.0__[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:

The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

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" ?>

<configuration>

  <system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior name="WCFTest001.Test001ServiceBehavior">

          <serviceMetadata httpGetEnabled="true" />

          <serviceDebug includeExceptionDetailInFaults="false" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <services>

      <service behaviorConfiguration="WCFTest001.Test001ServiceBehavior"

          name="WCFTest001.Test001Service">

        <endpoint address="" binding="basicHttpBinding" contract="WCFTest001.ITest001Service">

          <identity>

            <dns value="localhost" />

          </identity>

        </endpoint>

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

      </service>

    </services>

  </system.serviceModel>

</configuration>

NOTE: make sure to edit the values I’ve set to red.

In the end, the project should look like this:

The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

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.
The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

Then, enable Anonymous authentication:

The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

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:

The_SharePoint_Blog_How_to_Create_WCF_Web_Service_on_SharePoint_2010

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

        

Comments

Wouter Heesakkers

Great artical, thanks for sharing.
I did a web service to in SP2010, but only once until now,but without the web.config and other .svc content.

Why do you enable anonymous access? Can't you just enable anonymous access on the web application, but not on the site collection?

Grtz, Wouter
at 3/24/2011 3:39 PM

Arjun

Thank you for the response!  I never set anonymous access to true in site collections; I only set it to true in web applications through IIS.  If I implied that anonymous access should be set on a site collection, could you please point out where?  Thanks!

-Arjun
at 4/4/2011 10:09 AM

New Balance 2011

You built some superior factors there. I searched this matter and located out that most of the people will concur with your blog.
at 4/11/2011 4:45 AM

Bijay

Hi,
I am getting the below error. Can you help me out.


The type 'WCFTest001.Test001Service, WCFTest001, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1b40828b828a8d86', provided as the Service attribute value in the ServiceHost directive could not be found.

Troubleshoot issues with Microsoft SharePoint Foundation.

Correlation ID: 539c3415-34e9-42c2-af82-d6c45988385f

Date and Time: 4/15/2011 2:21:36 PM
at 4/15/2011 4:54 AM

Dharani

how to create slideshow webpart in sharepoint 2010 using jquery.
Plz let me know.
Its an urgent.Guide me.
at 4/27/2011 3:27 AM

mbt shoes

I like your article very much! It's worth reading.Thank you for sharing it!
at 5/19/2011 11:34 PM

Ian Hayse

thanks for the detailed instructions!

at 7/12/2011 12:06 PM

Jos van Putte

Thanks a lot ! This works quite well.
I had one minor problem activating the service due to the fact that line [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] is missing in the webservice definition.

Great stuff !
at 8/15/2011 8:08 AM

Web

These kind of post are always inspiring and I prefer to read quality content so I'm happy to find many good point here in the post, writing is simply great, thank you for the post.
at 8/18/2011 3:01 AM

Creative Web Solutions

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
at 9/8/2011 6:20 AM

Ramya

hi

when m integrating with silverligth m getting an error cross domain pl help

advance thanks
at 9/14/2011 5:52 AM

Raju

Its a nice documentary ......
its vary good and helpful....


Thanks
Raju
at 10/4/2011 7:39 AM

Bill Ross

Not only is your article well researched and documented, it also appears that it is also very rare but extremely important and useful info. Thanks so much for posting!
at 10/7/2011 6:50 AM

Bob Graham

Thank you for this post.   It helped me get up to speed quickly.
at 10/28/2011 3:33 PM

Gil Roitto

A word of caution:

The web.config will overwrite any existing web.config in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI on the SharePoint server. This means e.g. that other installations which have altered this file will have their changes lost.

So better is to NOT have web.config in the ISAPI mapped folder, but to edit these changes manually into the web.config in the path above.

When you follow these otherwise excellent steps in this guide, your Office Workspace, Office Web Apps etc will stop working. So make sure to backup the web.config in the path above before you work with this guide. This will save you some headaches.
at 3/12/2012 8:07 AM

Pari

Nice article ... It was very useful for me ... Thanks a lot ...
at 4/25/2012 11:43 PM

David Holland

Hi Arjun,

This is a clear article - well written.  I am getting an error when I attempt to run it:

The type 'WCFTest001.Test001Service', provided as the Service attribute value in the ServiceHost directive could not be found.

Is there something else about the IIS or SharePoint I must configure?

David
at 8/31/2012 1:44 PM

Zeeshan Hanif

Hi,

Thanks for the nice tutorial.

I have managed to run the service in the sharepoint. But when i am adding the operationContracts, deploy it and update the service reference in the Client application there i cannot see those newly OperationContracts.

I am banging my head with it. Kindly reply asap so that i can get rid from this situation.

Thank you.
at 11/15/2012 2:06 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy