How to identify SPSite and SPWeb in URL?
-
09-12-2020 - |
문제
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?
올바른 솔루션이 없습니다
다른 팁
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.
- for web :
_spPageContextInfo.webAbsoluteUrl
- for site :
_spPageContextInfo.siteAbsoluteUrl
Hope this what you are looking for