如何使用Microsoft Graph .NET客户端库清除字段 [英] How to clear a field using using Microsoft Graph .NET Client Library
问题描述
使用以下代码设置不为空的值可以正常工作
Setting a value that is not empty works fine with the following code
var patch = new Group();
patch.Description = "your description here";
var req = new GroupRequest(graphClient.Groups["<group id>"].Request().RequestUrl, graphClient, new List<Option>());
req.UpdateAsync(patch).Wait();
但是,如果我将patch.Description设置为",则会出现异常
However, if I set patch.Description to "" i get an Exception
代码:Request_BadRequest消息:为属性指定了无效的值资源组"的描述".
Code: Request_BadRequest Message: Invalid value specified for property 'description' of resource 'Group'.
如果我将patch.Description的值设置为null(对于Group()的新实例,它已经是),则什么也没有发生(我也可以在Fiddler中看到,在主体中不包含任何描述)补丁程序请求.)
If i set the value of patch.Description to null (which it already is for a new instance of Group()) nothing happens at all (I can also see in Fiddler, that no description is not contained in the body of the patch request).
所以我的问题是,清除值的正确方法是什么?
So my question is, what is the correct way to clear a value?
推荐答案
清除字符串属性的正确方法是为其分配空值.由于最近已修复服务问题,因此您的PATCH请求无法正常工作.客户端库默认情况下不会发送空值.您可以使用AdditionalData属性包强制其发送:
The correct way to clear a string attribute is to assign it null value. Your PATCH request didn't work as expected due to an issue with the service that was recently fixed. The client library doesn't send null values by default though. You can force it to send it by using the AdditionalData property bag:
var groupToUpdate = new Group();
//groupToUpdate.Description = "New description";
var prop = new Dictionary<string, object>();
prop.Add("description", null);
groupToUpdate.AdditionalData = prop;
await graphClient.Groups[groupId].Request().UpdateAsync(groupToUpdate);
这篇关于如何使用Microsoft Graph .NET客户端库清除字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!