2

我正在使用 ms graph 将文件上传到文档库中。我已经为大文件实现了一个代码,它可以工作,但我不知道如何设置“驱动器”的自定义列

这是我的代码。我玩过附加数据,但没有成功。

任何想法??

谢谢

     public static async Task<DriveItem> uploadFile()
            {
                Stream fileStream = new FileStream(@"C:\prueba\prueba2.xlsx", FileMode.Open);
                DriveItem uploadedFile = null;
                UploadSession uploadSession = null;

    uploadSession = await objGraphServiceClient.Sites["SITE_ID"].Lists["LIST-ID"].Drive.Root.ItemWithPath("EISE_PRUEBA2.xlsx").CreateUploadSession().Request().PostAsync();

 if (uploadSession != null)
            {
                // Chunk size must be divisible by 320KiB, our chunk size will be slightly more than 1MB 
                int maxSizeChunk = (320 * 1024 * 10) * 4;
                ChunkedUploadProvider uploadProvider = new ChunkedUploadProvider(uploadSession, objGraphServiceClient, fileStream, maxSizeChunk);
                var chunkRequests = uploadProvider.GetUploadChunkRequests();
                var exceptions = new List<Exception>();
                var readBuffer = new byte[maxSizeChunk];

                foreach (var request in chunkRequests)
                {


                       var result = await uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions);



                    if (result.UploadSucceeded)
                    {
                        uploadedFile = result.ItemResponse;

                        var uno = uploadedFile.AdditionalData;

                        var add = new Dictionary<string, object>();

                        add.Add("PositionCode", "Pos-01-" + DateTime.Now.ToString("mmmm"));
                        add.Add("Category", "Category_" + DateTime.Now.ToString("mmmm"));

                        var temp = new ListItem();
                        temp.Id = uploadedFile.Id;
                        temp.Fields = new FieldValueSet();
                        temp.Fields.AdditionalData = add;


                        var driveItem = new DriveItem();

                        //var users = await objGraphServiceClient.Users.Request().GetAsync();

                        //    var driveItem = new DriveItem
                        //    {
                        //        Name = "new-file-name.xlsx"

                        //    };
                        //    //driveItem.CreatedByUser = users.First();
                        //    driveItem.AdditionalData =  new Dictionary<string, object>();

                        //    driveItem.AdditionalData.Add(new KeyValuePair<string, object>("@odata.category", "Category" + DateTime.Now.ToString("mmmm")));
                        //    driveItem.AdditionalData.Add(new KeyValuePair<string, object>("@odata.PositionCode", "Pos-01-" + DateTime.Now.ToString("mmmm")));


                        // var updatedItem = await objGraphServiceClient.Sites["hispaniaassetmanagement.sharepoint.com,d04053f4-eb19-4ed8-9785-0f7aa2a908c8,6227bfe6-c7cb-4990-8f51-0a7fd8c28c1b"].Lists["67987e61-86cc-4f3e-93f2-0b6699b97a94"].Drive.Items[uploadedFile.Id].Request().UpdateAsync(driveItem);
                        //objGraphServiceClient.Sites["hispaniaassetmanagement.sharepoint.com,d04053f4-eb19-4ed8-9785-0f7aa2a908c8,6227bfe6-c7cb-4990-8f51-0a7fd8c28c1b"].Lists["67987e61-86cc-4f3e-93f2-0b6699b97a94"].Drive.Items["017ZCPK2DMW4ZEXUK7QZHJ7CQLPY7GHDFJ"]

                        uploadedFile.ListItem.Fields.AdditionalData = add;

                        var updatedItem = await objGraphServiceClient.Sites["SITE-ID"].Lists["LIST-ID"].Drive.List.Items[uploadedFile.Id].Request().UpdateAsync(uploadedFile.ListItem);
                    }
                }
            }
            return (uploadedFile);
        }
4

1 回答 1

3

我在 SDK 中使用其余 API 找到了解决方案。使用图形资源管理器将是:

补丁:https : //graph.microsoft.com/v1.0/sites/{siteID}/lists/{ListId}/items/{ItemId}/fields

内容类型:应用程序/json

{“自定义列”:“值”}


但!!!如果您想更新返回驱动器项目的上传文件的文件,则解决方案将是

 var result = await uploadProvider.GetChunkRequestResponseAsync(request, readBuffer, exceptions);

                if (result.UploadSucceeded)
                {
                    uploadedFile = result.ItemResponse;

                    string webApiUrl = "https://graph.microsoft.com/v1.0/sites/"+ siteIDDMS + "/drives/"+driveIDHAMSLib+"/items/"+uploadedFile.Id+"/listItem/fields";

                    JObject paramsJson = new JObject()
                    {
                        ["Category"] = "NewCat_" + DateTime.Now.ToString("mmmm")                            
                    };

                    await RunRestApi(webApiUrl, accessToken, paramsJson,"PATCH", Display);

                }

顺便说一句……有人知道这是否可以使用 SDK 实现???

于 2019-05-22T09:43:49.793 回答