Tuesday, July 7, 2009

Creating custom SharePoint web services

I found there are a lot of instructions on how to create web services but not many that give a best practices approach to doing this inside your solution in a way that is easy to repeat (like when you create new web methods in your custom web service and you don't want to manually re-edit all the wsdl and disco junk).

First, you need to download two things to follow along:

  1. STSDEV
  2. WSS Web Service Helper

Next, follow these steps:

  1. Create a new project in STSDEV, use the empty project C# assembly
  2. Add a new Web Service Application Project to your solution
  3. Add your web method to your web service. (If you just want to test this out, the default HelloWorld will work)
  4. Run the WSS Web Service Helper against your .asmx file (in your web service project). This will autogenerate your wsdl and disco .aspx files.
  5. Copy the .asmx file and the .cs codebehind (your webservice) and the two .aspx files generated by the WSS Web Service Helper and place them into your Rootfiles/ISAPI folder in your STSDev project.
  6. Go to your .asmx file and delete the CodeBehind attribute of the Webservice tag and place the fully qualified assembly string into the Class attribute (Namespace.Class, Assembly, Version, Culture, PublicKey)
  7. Go into the Generated *disco.aspx file and for both of the soap tags replace the address attribute with <% SPEncode.WriteHtmlEncodeWithQuote(Response,SPWeb.OriginalBaseUrl(Request), '"'); %>
  8. Next go to the *wsdl.aspx file and (at the bottom of the file) replace the location attribute of the soap12:address tag with <% SPEncode.WriteHtmlEncodeWithQuote(Response,SPWeb.OriginalBaseUrl(Request), '"'); %>
  9. Also in both the *disco.aspx and *wsdl.aspx files you need to change the <@ assembly tag to this:
    <%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    because the .disco generator puts the wrong version and adds an extra Microsoft.SharePoint to the assembly reference.
  10. Deploy your project and test.
  11. Finally, if you have trouble, go here for troubleshooting, the tips are at the bottom of the article.
When you need to add or update your web methods, do it in the Web Service project and then repeat steps 4-9. Hopefully this method makes it a little less painful for you to develop custom web services in SharePoint.

5 comments:

Steve Ruiz said...

nice tip!

Steve Ruiz said...

nice tip!

Jonathan said...

Thanks for your post, it got me over the hump. I was banging my head on the file format all day!! I was already playing with the disco and wsdl generator, but I noticed it was generating for an older version of SP so I dismissed it without further effort. After reading your post, I followed your simple directions and violla!! I tweaked the generator source so you dont have to make any changes to the disco or wsdl file. Id be happy to share, will post on the original authors page as well.

Paul said...

Jonathan, please do send us a link when the post has been updated. Once you send it I'll do a new post with the simplified instructions that take into account your fixes. If you've got a blog, be sure to send that to us as well, we'd love to give you credit for your work.

Anonymous said...

Really crappy instructions for newbies.