I'm currently building a Silverlight web part on SharePoint 2010. The issue I have, maybe is too trivial for me, is that I have a list, lets say its http://www.mysite.com/subsite/anothersubsite/Lists/mylist/AllItems.aspx, I want to retrieve the site url, in this case: http://www.mysite.com/subsite/anothersubsite . The url can vary depending on the depth of the subsite. I've try using the ClientContext.Current but without success.

Any help would be appreciated!


You need to load the required properties. Unlike server side object model, context.web will be largely empty until you explicitly load the data.

context = ClientContext.Current;   
web = context.Web;
context.Load(web, w => w.Title, w => w.Description, w => w.ServerRelativeUrl);   
context.ExecuteQueryAsync(OnSiteLoadSuccess, OnSiteLoadFailure);

So here you can access web.ServerRelativeUrl method in OnSiteLoadSuccess method.

| improve this answer | |
  • I've tried that, but always return the root "/" as ServerRelativeUrl. I think my issue is that the XAP file is hosted in the mysite.com/SomeFolder/MyApp.xap and return always the root based on this address, not from where the webpart is. – Gabriel Mongeon Jan 12 '12 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.