I have created and deployed a WCF service in SharePoint and I am sending requests using JQuery. Every thing is working fine except for sub sites.

Lets say I have these two service URLs:

 - http://localhost:1111/_vti_bin...../MethodName
 - http://localhost:1111/subsite/_vti_bin...../MethodName  

When I use these two URLs the SPContext.Current.Site is the same and is correct.
But the SPContext.Current.Web is the same which is incorrect. Using the second URL the SPWeb object must be the sub web and not the root web.

How can I get the sub site as an SPWeb object? Do I need to always pass the web URL as a parameter to the WCF method?

  • Never worked with WCF Service in SP but sending the URL will definitely get the SPWeb object! Nov 30, 2012 at 12:01
  • Yes I know but I am asking why the SPContext.Current.Web doesn't return the sub web and always the root web?
    – user3470
    Nov 30, 2012 at 12:04

2 Answers 2


After searching I have concluded that the SPContext.Current.Web will always return the RootWeb so I need the URL of the specific sub web.

To avoid passing the SPWeb URL to every WCF method where I need an SPWeb object, I can do the following:

I am running my WCF service in ASP.NET comatibility mode so I can use the HttpContext object, and I can use HttpContext.Current.Request.RawUrl. This property returns every thing in the URL string after the domain name. Then I can truncate this string to get the sub web relative URL and pass it to the OpenWeb() method.


At the client side the URL argument of the JQuery AJAX call is:


On the server side if I use HttpContext.Current.Request.RawUrl I get:


Then after I truncate this string I pass the /subsite to the OpenWeb() method.

Any other ideas are greatly appreciated.


I had the same case. But I took the Url from here:

Uri requestUri = System.ServiceModel.OperationContext.Current.RequestContext.RequestMessage.Headers.To;

So I was able to create a SPSite object:

using (SPSite site = new SPSite(requestUri.Scheme + "://" + requestUri.Host)
    //your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.