Geeks With Blogs
Adrian Hara Working through the .NET maze

If you ever need to deploy a WCF service to SharePoint 2007, there's plenty of information about how to do it on the web. Including how to make it work with Integrated Authentication or Forms Authentication. And it's all great and works.

However, I had to make the service "run" under the context of a particular web (which was actually a subweb). By this I mean that if I have a root site and a subsite and my WCF service calls SPContext.Current.Web I'd want it to return the subsite, not the root site.

Well, I deployed the service in a subfolder of the "_layouts" folder and it worked fine. Except that SPContext.Current.Web always returned the root site, not the subsite. Surprisingly (or maybe due to my lack of google search skills  ), there was little to no information available on the net on how to do this. All I found was something along the lines of "if deployed in layouts or vti_bin folder, a service runs under the context of the specific web". Pretty generic for me. So after some headbanging and some more searching, I finally found out the problem. And it's so obvious and simple that I'm really surprised there's very little information around about this, so I thought I'd note it here... for posterity like

In the service client, instead of having the service reference like so:

http://domain/_layouts/somefolder/MyService.svc

have it like so

http://domain/SUBSITENAME/_layouts/somefolder/MyService.svc

That's all there is to it, just include the subsite (or subweb) name in the url, as the services in the _layouts folder are available from every subweb, and then the SPContext.Current.Web will return the correct web.

Thanks to Serge van den Oever, on whose blog I found out about this!

Posted on Thursday, May 21, 2009 11:30 AM | Back to top


Comments on this post: Running a WCF service in the context of a particular SharePoint 2007 web (or subweb)

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Adrian Hara | Powered by: GeeksWithBlogs.net