【问题标题】:Find out the URL for the document library of a SharePoint document找出 SharePoint 文档的文档库的 URL
【发布时间】:2010-12-06 21:02:43
【问题描述】:

如果我知道文档的 URL,我能否找到该文档所在的共享点文档库的 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 模式的解决方案。我们可以使用其中任何一个来实现这一点吗?

谢谢。

【问题讨论】:

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

标签: sharepoint


【解决方案1】:

根据具体情况,我有两种不同的方法。两者都表现得非常好(需要注意),尽管第二种解决方案通常在我们的用例中表现得相当好。

第一个非常简单:

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,然后在网络中找到正确的列表。

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

【讨论】:

    【解决方案2】:

    SPWeb 对象有一个GetFile 方法,它获取完整的文件url。

    SPFile file = web.GetFile(yoururl);
    

    现在很容易通过以下方式访问 SPList 的 url:

    string listUrl = file.Item.ParentList.DefaultViewUrl;
    

    所以,在一个方法中:

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

    确保在您的项目中也引用Microsoft.Sharepoint.dll

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2011-04-30
      • 1970-01-01
      • 2014-02-12
      • 2010-10-05
      相关资源
      最近更新 更多