找出SharePoint文档的文档库的URL

时间:2009-10-14 23:46:05

标签: sharepoint

如果我知道文档的URL,我是否可以找到文档所在的sharepoint文档库的URL。以下是SharePoint站点的两个示例URL。第一个文档存在于文档库的根目录下。第二个文档位于文档库中的文件夹“folder1”下。感谢是否知道文档库的URL(http:///sites/site1/DocLib/Forms/AllItems.aspx)。

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


感谢您的回复。我正在寻找一个MOSS OOTB Web服务或基于URL模式的解决方案。我们可以使用其中任何一种来实现这一目标吗?

感谢。

2 个答案:

答案 0 :(得分:1)

SPWeb对象有一个GetFile方法,它接受完整的文件URL。

SPFile file = web.GetFile(yoururl);

现在可以通过以下方式轻松访问SPList的网址:

string listUrl = file.Item.ParentList.DefaultViewUrl;

所以,在一起的方法中:

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

答案 1 :(得分:0)

根据具体情况,我有两种不同的方式。虽然第二种解决方案通常对我们的用例表现相当不错,但它们的表现都不是很好(重要的是要注意)。

第一个非常简单:

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;
}

第二个更复杂一点。它确实需要您先删除URL的文件部分,然后将其传递给方法以获取正确的SPWeb,然后在Web中找到正确的列表。

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;
}
相关问题