从SharePoint站点中的列表中提取ListItems。

问题描述 投票:0回答:1

我试图提取一个SharePoint站点中根站点下面的项目列表,地址为 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);

// 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调用?(假设我事先不知道Site 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 microsoft-graph-sdks
1个回答
1
投票

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

正确的网站ID为 https:/host.sharepoint.comsitesmysite我的列表。 是。

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();

0
投票

通过一个API调用就能实现.

GET https://graph.microsoft.com/v1.0/sites/{host}.sharepoint.com:/sites/{siteName}:/lists/{listTitle}/items?$expand=Fields
© www.soinside.com 2019 - 2024. All rights reserved.