【问题标题】:How to find SharePoint documents by custom column value using Microsoft Graph API如何使用 Microsoft Graph API 按自定义列值查找 SharePoint 文档
【发布时间】:2019-01-25 23:12:22
【问题描述】:

通过我的 Office 365 帐户在 SharePoint Online 网站中,我在我的文档中添加了一个列 - “CustomerId”。我想在 C#(不是 JavaScript)中查找 CustomerId 为 102 的所有文档。

到目前为止,我能够获取文件夹下的所有文件

var files = graphClient.Sites[siteId].Drive.Items[driveItemId]
            .Children.Request().GetAsync().Result;

或者在 Graph Explorer 中查看相同的结果 https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{driveItemId}/children

但我还没有找到正确的语法来使用 C# 或图形资源管理器中的自定义列过滤条件获取所有文档 (driveIems)。我尝试过的事情的例子:

在 C# 中

var files = graphClient.Sites[siteId].Drive.Items[driveItemId]
            .Search("fields/CustomerId eq 102").Request().GetAsync().Result;

在图形资源管理器中 https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{driveItemId}/search(q='CustomerId eq 102')

希望有人能帮我解决这个问题。

更新:
以前我从

获得了 driveItemId
var customerFolder = graphClient.Sites[siteId].Drive.Root
    .ItemWithPath("CustomerGroup/IndustryGroup").Request().GetAsync().Result;
string driveItemId = customerFolder.Id;

我知道我可以得到一个 ListItem

var customerFolder = graphClient.Sites[siteId].Drive.Root
    .ItemWithPath("CustomerGroup/IndustryGroup").Request()
    .Expand(d => d.ListItem).GetAsync().Result;

但我只从 customerFolder.ListItem.Id 中找到了“4”的列表 ID

如何获取列表 ID 以便在 graphClient.Sites[siteId].Lists[listId] 中使用它?

【问题讨论】:

    标签: c# rest api microsoft-graph-api sharepoint-online


    【解决方案1】:

    我建议使用以下查询:

    https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items?filter=fields/CustomerId eq 123&expand=driveItem
    

    解释:

    • 通过filter查询选项过滤列表中的项目
    • 通过expand查询选项返回列表项的关联驱动器项

    这是msgraph-sdk-dotnet 的示例:

    var request = await graphClient.Sites[siteId].Lists[listId].Items.Request().Filter("fields/CustomerId eq 123").Expand(item => item.DriveItem).GetAsync();
    foreach (var item in request)
    {
        Console.WriteLine(item.DriveItem.WebUrl);
    }
    

    更新

    驱动器的底层文档库列表(连同它的属性)可以这样检索:

    var list = await graphClient.Sites[siteId].Drive.List.Request().GetAsync();
    Console.WriteLine(list.Id);  //gives SharePoint List Id
    

    注意:https://graph.microsoft.com/beta/sites/{site-id}/drive 端点返回此站点的默认驱动器(文档库)

    参考

    Working with SharePoint sites in Microsoft Graph

    【讨论】:

    • 谢谢瓦迪姆。您能否检查我更新的问题,因为我找不到获取 ListItemId 的方法?
    • 再次感谢瓦迪姆。你的方法确实有效!有没有办法只在共享点文件夹(客户组/行业组)下搜索/过滤文件?
    • 我如何简单地返回每个子项的所有自定义列及其值,而不是过滤?默认情况下它们不显示,?expand=fields 对我不起作用。
    猜你喜欢
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多