我正在使用 ms graph 将文件上传到文档库。 我已经实现了大文件的代码并且它有效 但我不知道如何设置“驱动器”的自定义列
这是我的代码。我已经使用了additionalData,但没有成功。
有什么想法吗?
谢谢
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);
}
最佳答案
我找到了使用其余 API 而不是 SDK 的解决方案。 使用图形浏览器将是:
补丁:https://graph.microsoft.com/v1.0/sites/ {siteID}/lists/{ListId}/items/{ItemId}/fields
内容类型:application/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 实现???
关于c# - MS图API : Set custom tags (columns) of a DriveItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56223986/