我们已经使用 Microsoft Graph API 一段时间来设置扩展属性的值。以前我们使用过 Microsoft.Azure.ActiveDirectory.GraphClient,但一年后出现了 Microsoft.Graph Nuget 包。 Microsoft.Azure.ActiveDirectory.GraphClient 有一个名为 SetExtendedProperty 的方法,但 Microsoft.Graph 包中没有这样的方法。有人建议如何使用 Microsoft.Graph 包设置扩展属性吗?
如果您需要更多信息,请告诉我!
最佳答案
您可以使用 microsoft graph sdk v1.4.0 版本创建架构扩展。相关讨论&代码示例here和 here供您引用。
例如,为组创建架构扩展:
// Create a schema extension for groups.
SchemaExtension extensionDefinition = new SchemaExtension()
{
Description = "This extension correlates a group with a foreign database.",
Id = $"crmForeignKey", // Microsoft Graph will prepend 8 chars
Properties = new List<ExtensionSchemaProperty>()
{
new ExtensionSchemaProperty() { Name = "fid", Type = "Integer" }
},
TargetTypes = new List<string>()
{
"Group"
}
};
// Create the schema extension. This results in a call to Microsoft Graph.
SchemaExtension schemaExtension = await graphClient.SchemaExtensions.Request().AddAsync(extensionDefinition);
然后为扩展创建一个类:
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class MyDBExtensionClass
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "fid", Required = Newtonsoft.Json.Required.Default)]
public int FID { get; set; }
public MyDBExtensionClass(int fid)
{
FID = fid;
}
}
接下来,通过更新组的 AdditionalData
属性将架构扩展属性设置为现有组:
// Update a group.
// This snippet changes the group name.
// This snippet requires an admin work account.
public async Task<List<ResultsItem>> UpdateGroup(GraphServiceClient graphClient, string id, string name)
{
List<ResultsItem> items = new List<ResultsItem>();
IDictionary<string, object> extensionInstance = new Dictionary<string, object>();
extensionInstance.Add(extensionIDYouGet, new MyDBExtensionClass(1213));
// Update the group.
await graphClient.Groups[id].Request().UpdateAsync(new Group
{
DisplayName = Resource.Updated + name,
AdditionalData= extensionInstance
});
items.Add(new ResultsItem
{
// This operation doesn't return anything.
Properties = new Dictionary<string, object>
{
{ Resource.No_Return_Data, "" }
}
});
return items;
}
您还可以将扩展程序添加到新的创建组,如上面的链接所示。
更新:
您的要求是将架构扩展属性设置为 user ,然后更新 user 的 AdditionalData
属性:
await graphClient.Users["ID"].Request().UpdateAsync(new User
{
DisplayName = Resource.Updated + name,
AdditionalData = extensionInstance
});
如果您想为用户实体创建架构扩展,User
值应包含在 SchemaExtension 对象的 TargetTypes
属性中。
关于c# - Microsoft.Graph api 设置用户扩展属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44676448/