如何使用 SharePoint CSOM 加快文件读取速度?
How to speed up file reading using SharePoint CSOM?
我已经使用 SharePoint CSOM 创建了一个函数,以使用以下代码提取文档集合(列表)中的所有文件,但是它需要 5 分钟以上才能获得 3671 个文件的列表。
是否可以加快速度,如果可以,怎么做?我见过其他人使用 CAML 查询从文件夹中提取文件,尽管我不是 100% 确定我将如何实现它
static SharePointOnlineCredentials SPCreds = new SharePointOnlineCredentials("*********", new System.Net.NetworkCredential("", "*******").SecurePassword);
static string sharepointURL = "https://********";
static string ListName = "Documents";
static string docuemntsStartWith = "Shared Documents";
static List<string> files = new List<string>();
static void Main(string[] args)
{
using (ClientContext clientContext = new ClientContext(sharepointURL))
{
clientContext.Credentials = SPCreds;
List list = clientContext.Web.Lists.GetByTitle(ListName);
clientContext.Load(list);
getFilesInFolder(clientContext, list.RootFolder);
}
Console.WriteLine("Extracted all " + files.Count + " files");
Console.Read();
foreach(string f in files)
{
Console.WriteLine(f);
}
Console.WriteLine("DONE");
Console.Read();
}
public static void getFilesInFolder(ClientContext cc, SP.Folder SPFolder)
{
cc.Load(SPFolder);
cc.Load(SPFolder.Folders);
cc.Load(SPFolder.Files);
cc.ExecuteQuery();
FolderCollection fcol = SPFolder.Folders;
FileCollection filecol = SPFolder.Files;
foreach (SP.File SPF in filecol)
{
files.Add(SPF.ServerRelativeUrl);
}
foreach (SP.Folder SPF in fcol)
{
getFilesInFolder(cc, SPF);
}
}
试试下面使用 CAML 查询的代码:
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class RetrieveListItems
{
static void Main()
{
string siteUrl = "http://gauti.sharepoint.com/sites/SP";
ClientContext clientContext = new ClientContext(siteUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle("NewBook");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
"<Value Type='Number'>19</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>";
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem);
clientContext.ExecuteQuery();
foreach (ListItem oListItem in collListItem)
{
Console.WriteLine("ID: {0} \nTitle: {1} \nBody: {2}", oListItem.Id, oListItem["Title"], oListItem["Body"]);
}
}
}
}
我已经使用 SharePoint CSOM 创建了一个函数,以使用以下代码提取文档集合(列表)中的所有文件,但是它需要 5 分钟以上才能获得 3671 个文件的列表。
是否可以加快速度,如果可以,怎么做?我见过其他人使用 CAML 查询从文件夹中提取文件,尽管我不是 100% 确定我将如何实现它
static SharePointOnlineCredentials SPCreds = new SharePointOnlineCredentials("*********", new System.Net.NetworkCredential("", "*******").SecurePassword);
static string sharepointURL = "https://********";
static string ListName = "Documents";
static string docuemntsStartWith = "Shared Documents";
static List<string> files = new List<string>();
static void Main(string[] args)
{
using (ClientContext clientContext = new ClientContext(sharepointURL))
{
clientContext.Credentials = SPCreds;
List list = clientContext.Web.Lists.GetByTitle(ListName);
clientContext.Load(list);
getFilesInFolder(clientContext, list.RootFolder);
}
Console.WriteLine("Extracted all " + files.Count + " files");
Console.Read();
foreach(string f in files)
{
Console.WriteLine(f);
}
Console.WriteLine("DONE");
Console.Read();
}
public static void getFilesInFolder(ClientContext cc, SP.Folder SPFolder)
{
cc.Load(SPFolder);
cc.Load(SPFolder.Folders);
cc.Load(SPFolder.Files);
cc.ExecuteQuery();
FolderCollection fcol = SPFolder.Folders;
FileCollection filecol = SPFolder.Files;
foreach (SP.File SPF in filecol)
{
files.Add(SPF.ServerRelativeUrl);
}
foreach (SP.Folder SPF in fcol)
{
getFilesInFolder(cc, SPF);
}
}
试试下面使用 CAML 查询的代码:
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class RetrieveListItems
{
static void Main()
{
string siteUrl = "http://gauti.sharepoint.com/sites/SP";
ClientContext clientContext = new ClientContext(siteUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle("NewBook");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
"<Value Type='Number'>19</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>";
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem);
clientContext.ExecuteQuery();
foreach (ListItem oListItem in collListItem)
{
Console.WriteLine("ID: {0} \nTitle: {1} \nBody: {2}", oListItem.Id, oListItem["Title"], oListItem["Body"]);
}
}
}
}