As we known, SharePoint has its own URL structure. In my project, i have to do things with the SharePoint library/ List by entering the library/ list URL. However, i don't know how to split the URL to site, sub-site, Library/ List Name, and Folder Level..

For example: *(something) => maybe exist


So, how to split the above URL to fit the code below?

        string siteurl = "http://siteName/subsite/";
        using (SPSite site = new SPSite(siteurl))
            using (SPWeb web = site.OpenWeb())

                SPList docLib = web.Lists["libraryName"];
            }// How about folders?

Moreover, how to handle the sub-folder in the URL? For example, i need to do things to the file which inside the folder that if the admin has specified.


i solve it. Please check the feasibility of it :)

First- to find the site~~

List<string> pathComponent = new List<string>();//to store the url component
string path= "siteURLWith Folder";
string[] words = path.Split('/'); 
pathComponent.Add(@"http://" + words[2] + @"/");//now i got http://site/

//so i need to find subsite, library name, folder name/path

Second - to find the other component~~

if (path.Contains("?RootFolder="))
    string folderPath = path.Split(new string[] { "?RootFolder=" }, StringSplitOptions.None)[1];
    // Decode the encoded string.
    folderPath = HttpUtility.UrlDecode(folderPath);
    string finalPath = folderPath.Split(new string[] { "&FolderCTID=" }, StringSplitOptions.None)[0];
    //Done!! i got /subsite/libraryName/folderPath by the above code
    //in here, it is no any folder lever ;)
    //I won't show code because it is very long..i will tell you the steps.
    //if"_layouts" exist
    //The subsite will be between site and _layout. /site/subsite/_layout/...
    //So we can get it with for loop
    //else if the "_layout" is not contained. It will be /site/subsite/libraryName/... more easy, right?
    //to find the library Name, i will write it in if "_layouts" block
    //first we find the .aspx in for loop.
    //then we got it :) because .../libraryName/Form/AllItem.aspx

Your Answer

