从SharePoint文档库URL

时间:2018-01-24 14:24:51

标签: c# sharepoint-online

我是Sharepoint的新手,目前正在使用Sharepoint Online。我需要在C#中的sharepoint文档库下检索文件。

我们有几个文档库网址如下:

  1. https://customer1.sharepoint.com/ DocumentFolder /Forms/AllItems.aspx
  2. https://customer2.sharepoint.com/sites/网站名称 / DocumentFolder / 子文件夹 /Forms/AllItems.aspx
  3. 我需要在上面的URL 1中提取 DocumentFolder 名称,并在URL 2的情况下提取 DocumentFolder / SubFolder 名称

    使用Sharepoint REST服务,我想列出 DocumentFolder 下的文件:

    http://site url / _api / web / GetFolderByServerRelativeUrl('/ DocumentFolder ')

    我遇到的挑战是为上述网址提取 DocumentFolder ,因为我找不到任何API /方法调用来提取 DocumentFolder 。我可以在网址上试用正则表达式,但如果有正确的方法来提取 DocumentFolder / SubFolder,这将会很有帮助。

1 个答案:

答案 0 :(得分:0)

我们可以使用带有CAML查询的CSOM C#来实现它,将视图设置为<查看Scope ='RecursiveAll'>,从而从根文件夹及其子文件夹中获取文档。

string targetSiteURL = @"https://xxx.sharepoint.com/sites/sitename";

var login = "xxx@xxx.onmicrosoft.com";
var password = "xxx";

var securePassword = new SecureString();

foreach (char c in password)
{
    securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = onlineCredentials;

List list = ctx.Web.Lists.GetByTitle("Documents");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='RecursiveAll'><Query></Query></View>";
camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();

foreach (var item in listItems)
{
     if (item.FileSystemObjectType == FileSystemObjectType.File)
     {
         // This is a File
     }
     else if (item.FileSystemObjectType == FileSystemObjectType.Folder)
     {
         // This is a  Folder
     }
}

更多信息供您参考:

http://www.sharepointpals.com/post/How-to-Get-All-the-Files-within-a-Folder-using-CAML-Query-in-SharePoint-Office-365-using-C-CSOM