Pages

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

Add your web method to your web service. (If you just want to test this out, the default HelloWorld will work)

Run the WSS Web Service Helper against your .asmx file (in your web service project). This will autogenerate your wsdl and disco .aspx files.

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.

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)

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), '"'); %>

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), '"'); %>

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.

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:

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.

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.