10

Let's say that I have created:

  • A SharePoint site collection at http://mysharepoint/.
  • A subsite of that site collection titled Team site at http://mysharepoint/teamsite/.
  • A list titled My List at http://mysharepoint/teamsite/Lists/mylist/.
  • A list view titled My View at 'http://mysharepoint/teamsite/Lists/mylist/Forms/myview.aspx'

In the Managed Client Object model code, let's say that I managed to reference:

  • The subsite as Web teamsite;
  • The list as List mylist;
  • The list view as View myview;

Is there a way to get the corresponding URLs from those three objects? Thanks.

9
  • 1
    i assume you already tried teamsite.Url etc?
    – int32
    Commented Nov 17, 2011 at 11:33
  • Also teamsite will be an SPWeb object, not SPSite.
    – James Love
    Commented Nov 17, 2011 at 11:42
  • @AndreasScharf teamsite.Url property does not exist in Managed Client Object model. I haven't been able to find the property/method that would serve my purpose.
    – Boris
    Commented Nov 17, 2011 at 11:51
  • @JamesLove Thanks for pointing out the mistake. I've updated my question.
    – Boris
    Commented Nov 17, 2011 at 11:52
  • I assume you are refering to the script client object model. Please leave a comment if you needed guidance for the "Microsoft.SharePoint.Client" one ^_^ Commented Nov 17, 2011 at 13:34

2 Answers 2

8

You should be able to use the available property to build the url you require. Just be aware that many of the property must be accessed with a *get_method* sintax.

For the SP.Site use the get_url() "property". For the SP.Web use the get_serverRelativeUrl() property and build up the full url.

For the list... don't know if there is an easier way, but you can resort to the get_serverRelativeUrl() property of the root folder (SP.List.get_rootFolder();). Then build the full url from there.

EDIT: As a reference for those that are using the "Microsoft.SharePoint.Client" namespace. On the Site object you can use the .Url property. Since the Web object does not provide an Url property use the ServerRelativeUrl one and build the full url as needed. Also, you must still compensate for the List object not providing any url property. As above, you can resort using the ServerRelativeUrl of the RootFolder of the list.

2
  • Actually, I was looking for the Microsoft.SharePoint.Client model, but the method names are virtually the same and you resolved my question. Thanks.
    – Boris
    Commented Nov 18, 2011 at 8:41
  • ops, sorry. I had read SPSite as SP.Site, so I automatically switched to javascript thinking mode ^_^''. I will update the answer when I get some time on hand. Commented Nov 18, 2011 at 9:06
2

This is how SP.Web absolute url can be constructed:

var clientContext = new SP.ClientContext.get_current();
var site = clientContext.get_site();
var web = clientContext.get_web();
clientContext.load(site,'ServerRelativeUrl', 'Url');
clientContext.load(web, 'ServerRelativeUrl');
clientContext.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
    var fullWebUrl = site.get_url().replace(site.get_serverRelativeUrl(), 
web.get_serverRelativeUrl());
alert(fullWebUrl);
}), Function.createDelegate(this, function (sender, args) {  }));
1
  • This helped me, but it only works when site.get_serverRelativeUrl() is longer than "/". A more flexible solution is: if (site.get_serverRelativeUrl() == "/") return site.get_url() + web.get_serverRelativeUrl(); else return site.get_url().replace(site.get_serverRelativeUrl(), web.get_serverRelativeUrl()); Commented Apr 20, 2012 at 8:27

Your Answer

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

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