【问题标题】:Extracting ListItems from a List in a SharePoint Site从 SharePoint 网站中的列表中提取 ListItem
【发布时间】:2020-05-28 07:15:55
【问题描述】:

我正在尝试在 host.sharepoint.com/sites/mysite 的根站点下方的 SharePoint 站点中提取项目列表。我尝试了很多不同的方法,但似乎只有一种有效:

var host        = "host.sharepoint.com:/";
var siteName    = "mysite";
var listName    = "MyList";

// Generate the Client Connection
var graphHelper = new ApplicationAuthenticatedClient(ClientId, Tenant, ClientSecret);
await graphHelper.ConnectAsync().ConfigureAwait(false);

// Code: itemNotFound
//Message: The provided path does not exist, or does not represent a site
//var list = await graphHelper.GraphClient.Sites[$"{host}{siteName}"].Request().GetAsync();

// Returns a Site, no Lists.
//var list = await graphHelper.GraphClient.Sites[host].Sites[siteName].Request().GetAsync();

//Code: itemNotFound
//Message: The provided path does not exist, or does not represent a site
//var list = await graphHelper.GraphClient.Sites[host].Sites[siteName].Lists[listName].Request().GetAsync();

// List retrieved, but no Items
//var site = await graphHelper.GraphClient.Sites[host].Sites[siteName].Request().Expand("lists").GetAsync();
//var list = await graphHelper.GraphClient.Sites[site.Id].Lists[listName].Request().Expand("Items").GetAsync();

//Code: invalidRequest
//Message: Can only provide expand and select for expand options
//var queryOptions = new List<QueryOption>() { new QueryOption("expand", "fields") };

// This works
var site = await graphHelper.GraphClient.Sites[host].Sites[siteName].Request().GetAsync();
var list = await graphHelper.GraphClient.Sites[site.Id].Lists[listName].Items.Request().Expand("Fields").GetAsync();

我终于设法让它连接,但我想知道是否有更好的方法来导航到列表,而不是两个 API 调用? (假设我事先不知道站点 ID)

编辑:使用图形资源管理器,我可以使用https://graph.microsoft.com/v1.0/sites/{host}.sharepoint.com:/sites/{siteName}:/lists/{listName}/items?expand=fields 访问这些项目,但我不知道如何(或是否)在 .NET API 的单个调用中访问该 API 调用。

【问题讨论】:

    标签: c# sharepoint microsoft-graph-api microsoft-graph-sdks


    【解决方案1】:

    看来我在 var list = await graphHelper.GraphClient.Sites[$"{host}{siteName}"].Request().GetAsync(); 的正确轨道上,但 URI 的格式不正确。

    https://host.sharepoint.com/sites/mysite/MyList 的正确站点 ID 是:

    Sites[host.sharepoint.com:/sites/mysite:"]
    

    从我原来问题的代码中检索列表如下所示:

    var host        = "host.sharepoint.com";
    var siteName    = "mysite";
    var listName    = "MyList";
    
    // Generate the Client Connection
    var graphHelper = new ApplicationAuthenticatedClient(ClientId, Tenant, ClientSecret);
    await graphHelper.ConnectAsync().ConfigureAwait(false);
    
    var list = await graphHelper.GraphClient.Sites[$"{host}:/sites/{siteName}:"].Lists[listName].Request().GetAsync();
    

    【讨论】:

      【解决方案2】:

      一次 API 调用即可。

      GET https://graph.microsoft.com/v1.0/sites/{host}.sharepoint.com:/sites/{siteName}:/lists/{listTitle}/items?$expand=Fields
      

      【讨论】:

      • 感谢您的回答。在我提出问题之前,我确实使用 Graph Exlporer 找到了 API 调用,但在原始问题中没有提及。我已经更新了这个问题。我最初的问题仍然存在 - 如何通过 .NET SDK 中的单个 API 调用访问此列表?
      猜你喜欢
      • 2012-06-03
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      相关资源
      最近更新 更多