Pages

Sunday, March 6, 2011

SharePoint 2010: Create Custom WCF Service

In SharePoint 2007, creating a custom Web Service was not so easy. However, asp.net web services are obsolete in SharePoint 2010. Rather new and recommended approach is to develop WCF Service. So the question comes up, “How much difficult it is to create a custom WCF service in SharePoint 2010?”. I’m going to answer the question just right in this blog.

 

Install CKS development tools edition

For showing how easily you can develop your own Custom WCF Service in SharePoint 2010, I’m going to use a open source Visual Studio 2010 extension know as Community Kit for SharePoint: Development Tools Edition. This tool will make the WCF service development much easier. It’ll automate tasks that you would have to do manually. There are two version of the extensions: One for SharePoint Foundation and another one is for SharePoint Server. Download the appropriate version and install.

 

Create WCF Service

Once you installed the CKSDev Visual Studio extension, you can open a SharePoint Project. In the SharePoint Project, right click on the project and try to add a new item. In the “Add New Item” dialog, you will find some new items added by CKSDev Visual Studio extension. Please select the option “WCF Service (CKSDev)” for new item as shown below:

image

Figure 1: ‘Add New WCF Service’ option ‘add new item’ dialog

 

Once you add the WCF Service, two files will be added by the dialog. One is the service interface and another is the Service itself.

 

Modify Service Types

As defined in MSDN, there are three different service types. Most of the time you need SOAP service. But if you need REST or ADO.NET Data service you can modify the service types by modifying the service factory as sown in the figure 2. The following table shows the three service types and their service factory name.

Service Type

Service Factory

Description

SOAP service

MultipleBaseAddressBasicHttpBindingServiceHostFactory

Basic HTTP binding must be used, which creates endpoints for a service based on the basic HTTP binding.

REST Service

MultipleBaseAddressWebServiceHostFactory

The service factory creates endpoints with Web bindings.

ADO.NET Data Service

MultipleBaseAddressDataServiceHostFactory

A data service host factory can be used.

When you create service with CKSDev tool, the default service generated is SOAP service. If you want to change the service type, please modify the factory in .svc file as shown below:

image

Figure 2: Service Factory defined in SVC file.

 

Deploy the Service

Once you are done with the service development, you are ready to deploy. But where you want to deploy the service? By default SharePoint service are kept in ISAPI directory. However, CKSDev deploy the service in ISAPI\ProjectNameSpace path as shown below:

image

Figure 3: Service deployment location

Once you define the service deployment location as shown in the figure 3, you can deploy the solution.

 

Access the Custom WCF Service

After Service deploy, you need to use the service in another projects. First try to access the service in browser. But remember you need to access the MEX endpoint either you will not get the service accessible in browser. To access the MEX endpoint, you should add “/MEX” at the end of the service name as shown below:

image

Figure 4: Access WCF Service MEX endpoint.

 

Finally try to add the service reference in a project using Visual Studio’s ‘Add Service Reference’ dialog as shown below:

image

Figure 5: Add Service Reference

 

 

Conclusion

So the steps described in this post are pretty simple:

  • Make sure you have downloaded and installed CKSDev Visual Studio extension.
  • Create a WCF Service (CKSDev) in the project. And if necessary, modify the service type
  • Deploy the solution and if necessary, change the deployment path.
  • Access the service MEX endpoint.

You are done. Pretty simple, I think.

47 comments:

  1. First I would like to appreciate you for the valuable information sharing with us. Thanks for making informative blog. As companies grow, it is difficult to manage the record of files, documents, and their location in the companies. SharePoint Development helps you to store the files and documents and share it on a central site.

    ReplyDelete
  2. Thanks so much for posting all of the awesome info! Looking forward to seeing more posts!Sharepoint 2010 supplied versatile development interface and development tools such as visual studio 2010 or sharepoint designer 2010 which enable the customers to handle the development of web part, workflow and other parts of sharepoint.
    SharePoint Consulting

    ReplyDelete
  3. Hi,

    Can i get full working code for lookup field as you given i am unable to var productStatus = productItem["ProductStatus"].ToString();

    "productItem" intellisence.

    ReplyDelete
  4. Sorry, I don't the codebase ready right now. But the code productItem["ProductStatus"].ToString() may throw null exception if the productstatus is null. So please check null. I hope it'll work.

    ReplyDelete
  5. That last '/mex' makes a big difference. Thanks for the post.

    ReplyDelete
  6. Excellent post, Sohel.

    My custom WCF service will be called from a custom web part. Given that the web part is running on a public site, there is no authentication required.

    Would you know what user my custom web service would running under? I wish to use a Linq2SQL class to read data from an external DB. Is web service running under the SharePoint system account?

    ReplyDelete
  7. I can't say for sure which user the service will run under. But you can check the user name from service by using: WindowsIdentity.GetCurrent().Name if you are using windows authentication. Or you can use Thread.CurrentPrincipal.Identity.Name

    ReplyDelete
  8. Hi Sohel,

    New project window does not display WCF Service(CKSDev) option. Instead I get SharePoint Console Application (CKSDev). Tried both foundation and server editions.

    ReplyDelete
  9. @Praveen, Not 'New project window', try add new project item.

    ReplyDelete
  10. How to access service using jQuery?

    ReplyDelete
  11. When I goto the /mex I get "Endpoint not found."

    ReplyDelete
  12. Hi

    Getting same error "End Point not found"

    ReplyDelete
  13. how to configure the default thello world service as a rest based service?
    kindly reply back to sanjuvmail-obama AT yahoo.co.in

    ReplyDelete
  14. I've followed step-by-step your tutorial but I'm not being able to access the service in the browser.

    I've deployed using VS Build > Deploy, then
    accessed the service url (adding /mex at the end)...

    But all I'm getting is a blank page, no source, nothing. The service is actually being found, because if I change something in the URL, then I get the Sharepoint not found error. The same happens if I retract the project.

    Please, any ideas?

    ReplyDelete
  15. It might be related to the MVC file's service attribute. Please open the svc file located in 14 hive and make sure the service class used in mvc file is correct. By the way, are you using sp2010 and cks dev tool for vs2010?

    ReplyDelete
  16. HI, i've got this working perfectly except for one this...I am getting a authentication challenge when I try to return certain objects.
    detailed here: http://stackoverflow.com/questions/10238234/return-an-array-of-objects-from-a-rest-enabled-wcf-service
    i'm wondering...is it possible to customise my endpoint and behaviour by specifying in the .config in the ISAPI for authentication and display full exception message?

    ReplyDelete
  17. Followed your instructions;however,"Endpoint not found" error is produced. Any solutions?

    ReplyDelete
  18. @John, please make sure your service is deployed properly and you are using 'mex' at the url.

    ReplyDelete
  19. Sohel:

    Thanks, I resolved the issue. I had the incorrect deployment options in the solution. With the correct settings the WS works correctly. Excellent article.

    ReplyDelete
    Replies
    1. What deployment options? Same problem here...

      Delete
  20. Awesome article, Thank you!

    ReplyDelete
  21. what deployment options did you do to solve "endpoint not found"

    ReplyDelete
  22. James:
    Under the service file (.svc) properties check the deployment location - make sure the deployment path is populated:
    "ISAPI\solution name\". The CKS is a wonderful tool,but it does not populate the deployment path.

    ReplyDelete
  23. Thanks for sharing your knowledge
    but where can i find the web.config of this service ???

    ReplyDelete
  24. Hi

    You have added service reference to "sohel-server" and visual studio generates some stub based on the reference, in future if you deploy this service to "sohel-dev-server" how would you update the reference? I don't want to delete and add a new service reference because that will require rebuild of my client application.

    ReplyDelete
  25. @Shafaqat Ali, You need to change the url of the wcf service proxy pro programmatically before calling it. Usually the service endpoint url is kept in config file or some other places.

    ReplyDelete
  26. Hi Sohel,

    I am getting bellow Error

    The content type text/html; charset=utf-8 of the response message does not match the content type of the binding
    (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 227 bytes of the response were: 'A transport-level error has occurred when sending the request to the server. (provider: Shared Memory Provider, error: 0
    - Either a required impersonation level was not provided, or the provided impersonation level is invalid.)'.

    ReplyDelete
  27. Hi Sohel,
    I have deployed web services to my shatepoint intranet (NTLM Security) using cks.dev tools and am using jquery to access them from sharepoint pages. I'd like to secure the services so that only users in a certain A/D group can call them.
    Is this possible with the MultipleBaseAddressWebServiceHostFactory

    ReplyDelete
  28. @Russel, I think you should write some WCF extensions but the challenges might be from those WCF binding extensions u can't access sharepoint group. The possible solution might be put the check in WCF service methods. In these methods check the user's group and deny access, if required.

    ReplyDelete
  29. I created and Publised Custom WCF Service Succesfully, but when I calling it from the Client, by addin proxy class with the url/mex no response from the server not event time out. I also checkec the ISAPI folder WCF service is there in the folder of namespace.
    Please advice.

    ReplyDelete
  30. @Farooqui, the most probable reason might be the WCF is not deployed correctly. Please make sure you have deployed your dll in GAC, has safecontrol in web config etc.

    ReplyDelete
  31. @Sohel, Thank you, but still I m unable to Get the bug, DLL is maked as Safe Control in my farm, it is already in the GAC. It is coming in brower no issue, but when i called any method from client no response. nothing...... plz advice

    ReplyDelete
  32. recycle app pool and its start work. Sohel Thank you for your efforts. Appreciated.

    ReplyDelete
  33. FYI

    I installed SharePoint Foundation and I clicked Insert | New item.

    I don't see an option for WCF Service (CKSDev).

    ReplyDelete
  34. @Rhyous You need to install CKS Development tool as mentioned in the section "Install CKS development tools edition"

    ReplyDelete
  35. Hi Sohel,

    is there any way to run a service without using mex protocol.

    ReplyDelete
  36. Hi Sohel
    I am not able to consume this WCf service using WCFTestclient.
    The error I am getting from the tool is
    Cannot obtain Metadata from http:///_vti_bin/SampleWCFSvc.svc/mex If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http:///_vti_bin/SampleWCFSvc.svc/mex Metadata contains a reference that cannot be resolved: 'http:///_vti_bin/SampleWCFSvc.svc/mex'. The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'. The remote server returned an error: (401) Unauthorized.HTTP GET Error URI: http:///_vti_bin/SampleWCFSvc.svc/mex The document was understood, but it could not be processed. - The WSDL document contains links that could not be resolved. - There was an error downloading 'http:///_vti_bin/SampleWCFSvc.svc/mex?xsd=xsd0'. - The request failed with HTTP status 404: Not Found.

    ReplyDelete
  37. I am also getting the same error.

    ReplyDelete
  38. do we need any manual modifications to web.config regarding endpoints?

    ReplyDelete
  39. Marvelous! Simply Marvelous! Thank YOU!

    ReplyDelete
  40. Hi i create wcf service and deployed in share point 2010, but while accessing jquery get access denied error.
    Could you please help me on that

    ReplyDelete
  41. no web.config ... how to change binding information... or creating multiple binding

    ReplyDelete
  42. Can you pls provide how can i create a RESTful services in sharepoint 2013 on-pRemises using VISUAL STUDENT 2013.
    some help on links or some source code.

    ReplyDelete
  43. Can you pls provide how can i create a RESTful services in sharepoint 2013 on-pRemises using VISUAL STUDENT 2013.
    some help on links or some source code.

    ReplyDelete

Note: Only a member of this blog may post a comment.