在通过microsoft-graph上传到sharepoint之前,将字段信息添加到文件中

时间:2017-11-15 12:03:53

标签: c# sharepoint microsoft-graph

我正在使用c#客户端库。

当我上传文件时,我希望能够为该文件设置一个字段(SharePoint列)。 我已尝试更新FieldValueSet之后DriveItem.ListItem的{​​{1}}更新PutAsync<DriveItem>(filestream),如此

var newfile = await gClient
    .Sites[mySIteId]
    .Drives[myDriveId]
    .Items[mySubFolderId]
    .ItemWithPath("newFile.txt")
    .Content
    .Request()
    .PutAsync<DriveItem>(fs);

var li = await gClient
    .Sites[mySIteId]
    .Drives[myDriveId]
    .Items[newifle.Id]
    .ListItem
    .Request()
    .GetAsync();

li.Fields.AdditionalData.Add("Title", "My New File Title");

await gClient
    .Sites[mySIteId]
    .Drives[myDriveId]
    .Items[newifle.Id]
    .ListItem
    .Request()
    .UpdateAsync(li);

但我得到了这个例外:

  

ServiceException字段'id'无法识别

上面需要3次调用API,但是在一次调用中上传文件时,我无法看到添加字段的方法。

按要求更新了信息 -

如果我尝试只设置1个字段,那么'id'仍会出现在JSON中。这是实际的代码和'clean'JSON

    var fs = new FileStream(@"c:\temp\TestImage.jpg", FileMode.Open);
    var newfile = await gClient.Sites[SPSiteId]
      .Drives[LibraryId]
      .Root
      .ItemWithPath("TestImage.jpg")
      .Content
      .Request()
      .PutAsync<DriveItem>(fs);
    var li = await gClient.Sites[SPSiteId]
      .Drives[LibraryId]
      .Items[newifle.Id]
      .ListItem
      .Request()
      .GetAsync();
   var d = new Dictionary<string, object>();
   d.Add("Title", "This should be the title");
   li.Fields.AdditionalData = d;

    await gClient.Sites[SPSiteId]
      .Drives[LibraryId]
      .Items[newifle.Id]
      .ListItem.Request()
      .UpdateAsync(li);

第一个电话:

PUT /v1.0/sites/xxxxxx.sharepoint.com,af844170-6c96-448b-bb75-b1144fa09f3c,d3904f65-6367-4071-9201-e96ea7c3c5d7/drives/b!cEGEr5Zsi0S7dbEUT6CfPGVPkNNnY3FAkgHpbqfDxdeiLjGMWnChSroudPhjGXRg/root:/TestImage.jpg:/content HTTP/1.1

enter image description here

第二次电话:

GET /v1.0/sites/xxxxxx.sharepoint.com,af844170-6c96-448b-bb75-b1144fa09f3c,d3904f65-6367-4071-9201-e96ea7c3c5d7/drives/b!cEGEr5Zsi0S7dbEUT6CfPGVPkNNnY3FAkgHpbqfDxdeiLjGMWnChSroudPhjGXRg/items/01QX3GBJL3RSNVFPDYGBG2FKKARXI7TSBV/listItem HTTP/1.1

enter image description here

第三次呼叫(响应为HTTP 400 Bad Request):

PATCH /v1.0/sites/xxxxx.sharepoint.com,af844170-6c96-448b-bb75-b1144fa09f3c,d3904f65-6367-4071-9201-e96ea7c3c5d7/drives/b!cEGEr5Zsi0S7dbEUT6CfPGVPkNNnY3FAkgHpbqfDxdeiLjGMWnChSroudPhjGXRg/items/01QX3GBJL3RSNVFPDYGBG2FKKARXI7TSBV/listItem HTTP/1.1

enter image description here

1 个答案:

答案 0 :(得分:0)

当您更新记录时,不会传入整个对象。相反,只传入您想要更改的字段:

var li = new ListItem();
li.Fields.AdditionalData.Add("Title", "My New File Title");
await gClient
    .Sites[mySIteId]
    .Drives[myDriveId]
    .Items[newifle.Id]
    .ListItem
    .Request()
    .UpdateAsync(li);

当您传入完整对象时,您传递了几个只读属性。 Microsoft Graph将假定应该应用您指定的任何值,如果这些值中的任何值以只读属性为目标,则会失败。