Question

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?

Was it helpful?

Solution

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.

Example:

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

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

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

/subsite/_vti_bin...../MethodName

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

Any other ideas are greatly appreciated.

OTHER TIPS

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top