MOSS 2007,以编程方式下载文档文件夹;无法连接到文档文件夹

时间:2019-12-16 19:16:41

标签: c# .net sharepoint sharepoint-2007

我正在尝试以编程方式从Sharepoint 2007站点上的文档文件夹中下载所有文件。到目前为止,我可以连接到该站点,但是在连接到文件夹并下载它们时遇到了问题。


try{
    using(SPSite site = new SPSite("http://mysharepointserver/sites/subsite")){
        using(SPWeb web = site.OpenWeb()){
            Console.Write("Connected to site");
            SPFolder testFolder = web.Folder["testFolder"];
            //example method downloading folder
            downloadFolder(testFolder);
        }
    }
}
catch(Exception e){
    Log(e.ToString());
}

我的控制台写正常,所以我知道我已正确连接到该站点。 我的日志文件输出:

System.ArgumentException: Value does not fall within the expected range.
   at Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean bThrowException)
   at Microsoft.SharePoint.SPListCollection.get_Item(String strListName)

我还尝试打印以下内容:

using(SPWeb web = site.OpenWeb()){
            Console.Write("Connected to site");
            Console.Write(web.lists);
            SPFolder testFolder = web.Folder["testFolder"];
            //example method downloading folder
            downloadFolder(testFolder);
        }

将以下内容输出到控制台:

Connected to site
Microsoft.SharePoint.SPListCollection

但是我不确定如何浏览SPListCollection来检索我的文件夹“ testFolder”

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

当您连接到共享点站点时,有不同类型的库。包含文档和文件夹的库是DocumentLibrary,而不是ListLibrary。按ID获得项目/库后,将其强制转换为SPDocumentLibrary以检索所需的项目。

使用https://docs.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.spdocumentlibrary?view=sharepoint-server获取DocumentLibrary的不同方法和属性以检索testFolder。

从:https://social.msdn.microsoft.com/Forums/en-US/5ee7fb55-5d90-4d28-8990-bf00479f891f/how-to-get-spdocumentlibrary?forum=sharepointdevelopmentprevious

访问文档库项目的示例
SPSite siteCollection = this.Site;
SPWeb site = this.Web;
// obtain query string values
string ListId = Request.QueryString["ListId"];
string ItemId = Request.QueryString["ItemId"];
// create list object and list item object
SPList list = site.Lists[new Guid(ListId)];
SPListItem item = list.Items.GetItemById(Convert.ToInt32(ItemId));
// query for information about list and list item
string ListTitle = list.Title;
string ItemTitle = item.Title;

if (list is SPDocumentLibrary) {
  SPDocumentLibrary documentLibrary = (SPDocumentLibrary)list;
  string DocumentTemplateUrl = documentLibrary.DocumentTemplateUrl;
  SPFile file = item.File;
  string FileAuthor = file.Author.Name;
  string FileSize = file.TotalLength.ToString("0,###") + " bits";
}