Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
Never worked with WCF Service in SP but sending the URL will definitely get the SPWeb object! –  Arsalan Adam Khatri Nov 30 '12 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 '12 at 12:04
add comment

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.