Question

Can I split the URL to SPSite part and SPWeb part?

consider the follwing:

http://abc/sites/john/_layouts/15/start.aspx

I can easliy to split the URL to SPSite: http://abc/sites/john/ and SPWeb : ""

However, I am scared that there are much more structure of SharePoint URL which I cannot split by the above way

Update:

if a URL like:

http://abc/a/b/c/d/e/f/g/h/i/k/lib1/Forms/AllItems.aspx

i can find the SPSite part: http://abc/ and the library part: /lib1/Forms/AllItems.aspx. How about the /a/b/c/d/e/f/g/h/i/k/? which of them is site collection? Which of them is subsite?

No correct solution

OTHER TIPS

You can create an SPSite object from the specified url, and open an SPWeb in that site. From there you can retrieve the segments you need.

string url = "http://abc/a/b/c/d/e/f/g/h/i/k/lib1/Forms/AllItems.aspx";
using (SPSite site = new SPSite(url))
{
    string sitePart = site.Url;
    string webRelativeUrl = url.Replace(sitePart, string.Empty);

    using (SPWeb web = site.OpenWeb(webRelativeUrl, false)) {
        ...
    }
}

Notice the false parameter in the OpenWeb() method. This specifies that the provided URL does not have to be an exact web url, but can be the url of a page in the web - which would be the case in your example.

If you are using SP2013. You can get the site and web url using _spPageContextInfo on client side.

  1. for web : _spPageContextInfo.webAbsoluteUrl
  2. for site : _spPageContextInfo.siteAbsoluteUrl

Hope this what you are looking for

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top