Find out the document library URL of a SharePoint document

If I know the url of the document, can I find the url for the sharepoint document library in which the document is present. Following are two example URLs for a SharePoint site. The first document is present at the root of the document library. The second document is present in the folder "folder1" in the document library. Appreciate if you recognize the URL of the document library (http: ///sites/site1/DocLib/Forms/AllItems.aspx) anyway.

Http: ///sites/site1/DocLib/a.doc Http: ///sites/site1/DocLib/folder1/a.doc


Thank you for your responses. I am looking for a solution with MOSS OOTB web service or URL pattern based. Can we use any of them to achieve this?

Thank.

+2


source to share


2 answers


The SPWeb object has a method GetFile

that accepts the full URL of the file.

SPFile file = web.GetFile(yoururl);

      

It is now easy to get to the SPList URL using the following:

string listUrl = file.Item.ParentList.DefaultViewUrl;

      



So, in a method together:

public string GetListUrlFromFileUrl(string fullFileUrl)
{
  using (SPSite site = new SPSite(fullFileUrl))
  {
    using(SPWeb myWeb = site.OpenWeb())
    {
      SPFile file = myWeb.GetFile(fullFileUrl);
      return file.Item.ParentList.DefaultViewUrl;
    }
  }
}

      

Don't forget to link to Microsoft.Sharepoint.dll

in your project.

+1


source


There are two different ways that I do this, depending on the situation. None of them work very well (important to note), although the second solution usually works well for our use cases.

The first is very simple:

private SPList GetListForFile(string fileUrl)
{
    using (SPSite site = new SPSite(fileUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPFile file = web.GetFile(fileUrl);
            if (file.Exists)
            {
                return file.Item.ParentList;
            }
        }
    }
    return null;
}

      



The second is a little more complicated. This requires you to first chop off the file portion of the url and then pass it to the method to get the correct SPWeb and then find the correct list online.

private SPList GetListForFile(string fileUrl)
{
    using(SPWeb web = OpenWeb(GetFolderUrl(fileUrl)))
    {
        string listName = fileUrl.Replace(web.ServerRelativeUrl, "");
        listName = listName.Substring(0, listName.IndexOf('/'));
        return web.Lists[listName];
    }
}

private string GetFolderUrl(string fileUrl)
{
    return Regex.Replace(fileUrl, @"/[^/]+?\.[A-Z0-9_]{1,6}$", "",
        RegexOptions.IgnoreCase | RegexOptions.Singleline);
}

private SPWeb OpenWeb(string folderUrl)
{
    SPWeb web = null;
    while(web == null)
    {
        web = Site.OpenWeb(folderUrl);
        if (!web.Exists)
        {
            web.Dispose();
            web = null;
        }
        folderUrl = folderUrl.Substring(0, folderUrl.LastIndexOf("/"));
        if (folderUrl.Length == 0)
        {
            folderUrl = "/";
        }
    }
    return web;
}

      

0


source







All Articles