如何通过其唯一 ID 获取 Sharepoint 上的文档?

How to get a document on Sharepoint by its unique ID?

我正在尝试在 Sharepoint Online 实例上使用 Sharepoint 客户端 Object 模型 (CSOM) 实现以下目标:

在伪代码中:

var results = Sharepoint.GetSearchResults("Keyword");
var specificItem = results[2];
var itemId = sepcificItem.UniqueId;
Store(itemId);

以后:

var itemId = LoadItemId();
var item = Sharepoint.GetItem(itemId);

查询部分工作正常,我得到一个搜索结果 table 为每个结果提供很多字段值,例如不同的 URL 以及唯一标识符、标题等。

var clientContext = new ClientContext(SharepointUri);
clientContext.Credentials = new SharePointOnlineCredentials(SharepointUserName, GetSecureString(SharepointPassword));
var keywordQuery = new KeywordQuery(clientContext);
keywordQuery.QueryText = keyword;
var searchExecutor = new SearchExecutor(clientContext);
var sharepointResultTable = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();
foreach (var resultRow in sharepointResultTable.Value[0].ResultRows)
{
    var path = (string) resultRow["ServerRedirectedURL"];
    var id = Guid.Parse((string) resultRow["UniqueId"]);        
}

我现在缺少的部分是:我应该为结果行存储哪些值以及如何根据以下条件执行 高效 查询此数据稍后获取项目?(当然我可以再次使用相同的关键字执行查询,但据我所知,我知道 id 或我存储的任何内容,必须有更有效的方法。此外,该项目可能不再是前一个关键字的结果,因为它可能已经更改)。

我试过了,使用结果 table 列中的 id 'UniqueId':

var clientContext = new ClientContext(SharepointUri);
clientContext.Credentials = new SharePointOnlineCredentials(SharepointUserName, GetSecureString(SharepointPassword));
var file = clientContext.Web.GetFileById(location);
clientContext.ExecuteQuery();
Console.WriteLine(file.Name);
return true;

不过没用:

Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException: The property or field 'Name' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

老实说,我不知道我在这里做什么,因为真的很难告诉我各种 Id 的含义(DocId、WorkId、UniqueId,仅举三个...)。

谁有更多 Sharepoint 经验可以告诉我这里的路吗?

错误:

Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException: The property or field 'Name' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

表示尚未请求客户端对象 (File)。为了请求客户端对象,需要使用 ClientRuntimeContext.Load and ClientRuntimeContext.LoadQuery 方法。

例子

//1. Retrieve File
var file = ctx.Web.GetFileById(id);
ctx.Load(file);
ctx.ExecuteQuery();


//2.Explicitly specify what properties (e.g. `Name`) to retrieve 
var file = ctx.Web.GetFileById(id);
ctx.Load(file, f=>f.Name);
ctx.ExecuteQuery();

另一方面,由于 SharePoint 搜索查询 API returns 结果跨网站集,您需要考虑为 ClientContext 指定正确的 url 以便检索 File,例如:

using (var ctx = GetContext(webUri, userName, password))
{

     var keywordQuery = new KeywordQuery(ctx) {QueryText = queryText};
     var searchExecutor = new SearchExecutor(ctx);
     var resultTables = searchExecutor.ExecuteQuery(keywordQuery);
     ctx.ExecuteQuery();
     foreach (var resultRow in resultTables.Value[0].ResultRows)
     {
          var itemWebUri = new Uri((string)resultRow["SPWebUrl"]); //<- get web url of item
          var itemId = Guid.Parse((string)resultRow["UniqueId"]);


          using (var curCtx = GetContext(itemWebUri, userName, password))
          {
              var file = curCtx.Web.GetFileById(itemId);
              curCtx.Load(file, f => f.Name);
              curCtx.ExecuteQuery();
              Console.WriteLine(file.Name);     
          }        
     }
}

哪里

    public static ClientContext GetContext(Uri webUri, string userName, string password)
    {
        var securePassword = new SecureString();
        foreach (var ch in password) securePassword.AppendChar(ch);
        return new ClientContext(webUri) { Credentials = new SharePointOnlineCredentials(userName, securePassword) };
    }