使用 Graph SDK C# 更新 Sharepoint 文档库中文件的字段

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

我在我的 C# 应用程序中使用 C# SDK 的 Graph API 来与 Sharepoint 配合使用。 这对于用自定义字段的数据填充列表非常有效,并且在文档库中上传文件也可以正常工作。但是,更新文档库中文件的自定义字段似乎不起作用。

我正在使用 .NET 6 和 Microsoft.Graph NuGet 包 (5.27.0)

我使用以下请求上传文件:

 var newFile = graphClient
        .Drives[await GetDriveId(docLibraryName, link)]
        .Root
        .ItemWithPath(fileName)
        .Content
        .PutAsync(fileStream);

GetDriveId() 将使用站点 ID 来获取文档库的驱动器 ID

我使用上传的 id 来获取 ListItem 的 id:

var listItem = await graphClient
    .Drives[await GetDriveId(docLibraryName, link)]
    .Items[newFile.Id]
    .ListItem
    .GetAsync((requestConfiguration) =>
    {
        requestConfiguration.QueryParameters.Select = new string[] { "Id" };
    });

之后我使用 id 来更新 ListItem 的字段:

var itemToAdd = new Microsoft.Graph.Models.ListItem
{
    Fields = new Microsoft.Graph.Models.FieldValueSet
    {
        AdditionalData = new Dictionary<string, object>()
        {
            { SpoDocument.Owner, document.Owner },
            { SpoDocument.Datum, document.Datum },
            { SpoDocument.Ref1, document.Ref1 },
            { SpoDocument.Ref2, document.Ref2 },
            { SpoDocument.Ref3, document.Ref3 },
            { SpoDocument.Ref4, document.Ref4 },
            { SpoDocument.MailTo, document.MailTo },
            { SpoDocument.MailBcc, document.MailBCC },
            { SpoDocument.MailCc, document.MailCC },
            { SpoDocument.BodyMail, document.BodyMail },
            { SpoDocument.ContactPersonen, document.Contactpersonen },
            { SpoDocument.Activiteit, document.Activiteit },
            { SpoDocument.VerkoopCategorie, document.VerkoopCategorie },
            { SpoDocument.InkoopCategorie, document.InkoopCategorie },
        }
    }
};

 await graphClient
 .Drives[await GetDriveId(docLibraryName, link)]
 .List
 .Items[listItem.Id]
 .PatchAsync(itemToAdd)

然而,这会返回一个带有“处理时的一般异常”的异常。 SpoDocumentClass 仅包含常量字符串以及文档库中的可用列, here you can see some columns

如果之后我尝试使用以下函数获取这些自定义字段,它们不会出现在字段列表中,这让我认为它们不可用? As can be seen here

var test = await graphClient
    .Drives[await GetDriveId(docLibraryName, link)]
    .Items[fileId]
    .ListItem
    .GetAsync((requestConfiguration) =>
    {
        requestConfiguration.QueryParameters.Expand = new string[] { "Fields" };
    });

但是,如果我获取文档库的列列表,它们确实会出现在其中as can be seen here

var test2 = await graphClient
    .Drives[await GetDriveId(docLibraryName, link)]
    .List.GetAsync((requestConfiguration) =>
    {
        requestConfiguration.QueryParameters.Expand = new string[] { "Columns" };
    });

我希望能够使用 Graph SDK 更新文档库中文档的字段

c# sharepoint microsoft-graph-api sharepointdocumentlibrary
1个回答
0
投票

您可以使用以下代码来更新字段

using System;
using System.Collections.Generic;
using Microsoft.Graph;
using Microsoft.Identity.Client;

class Program
{
    static async System.Threading.Tasks.Task Main(string[] args)
    {
        string clientId = "YourClientId";
        string clientSecret = "YourClientSecret";
        string tenantId = "YourTenantId";
        string siteId = "YourSiteId";
        string documentLibraryId = "YourDocumentLibraryId";
        string fileId = "YourFileId";

        IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
            .Create(clientId)
            .WithClientSecret(clientSecret)
            .WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}"))
            .Build();

        var authProvider = new ClientCredentialProvider(confidentialClientApplication, "https://graph.microsoft.com/.default");

        var graphServiceClient = new GraphServiceClient(authProvider);

        // Define the properties you want to update
        var updatedProperties = new Dictionary<string, object>
        {
            { "Title", "New Title" },
            { "Description", "New Description" },
            // Add other properties as needed
        };

        // Update file properties
        var updatedDriveItem = await graphServiceClient
            .Sites[siteId]
            .Drives[documentLibraryId]
            .Items[fileId]
            .Request()
            .UpdateAsync(new DriveItem
            {
                AdditionalData = updatedProperties,
            });

        Console.WriteLine($"Updated file properties. New title: {updatedDriveItem.Name}");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.