locked
How to find the library (with sub folder) object from the URL of the library in C#? RRS feed

  • Question

  • 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

    http://siteName/(subsite)/(_layouts/15/start.aspx#/(Lists))/libraryName//Forms/AllItems.aspx

    (?RootFolder=%2Fsubsite%2Flibrary1%2F........foldername,sub-foldername.....FolderCTID=..........


    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.

    My trying:

               string path = @"http://siteName/subsite/_layouts/15/start.aspx#/libraryName//Forms/AllItems.aspx?RootFolder=..............";

                List<string> pathComponent = null;

                if (path.Contains("_layouts"))//the path should be contains "_layouts"

                {

                    string[] words = path.Split('/');

                    pathComponent.Add(@"http://" + words[2] + @"/");// The 'http://' is set to default.

                    string subSite = "";

                    int num = 0;

                    if (words[3] != "_layouts") 

                  {

                        for (int i = 3; i < words.Length; i++)

                        {

                            if (words[i] != "_layouts")

                            {

                                subSite += words[i] + @"/";

                            }

                            if(words[i].Contains(".aspx"))

                            {

                                num = i;

                           }

                        }

                    } //else the path is not contain subsite

                    pathComponent.Add(subSite);// site/subsite inculded

                    //Get library/list name

                    string libName = "";

                    if (words[num - 1] == "Forms")

                    {

                        libName = words[num - 2];// it is library

                    }

                    else

                     {

                        libName = words[num - 1];// it is list

                    }

                    pathComponent.Add(libName);// library/ list name inculded

                }

                if (path.Contains("RootFolder"))

                {

    //I have no idea that how to do this.. :(

                }

            }


    Monday, August 29, 2016 3:45 PM