我刚刚开始使用 Microsoft Graph API 和 .net。
如何使用最新的 Microsoft.Graph 1.13.0 预览版 nuget 包(Microsoft Graph .NET 客户端库)向群组添加成员和团队?
可以在一个请求中完成吗?
我无法在新的组语句中设置成员、所有者或团队属性。
此外,我无法将团队添加到新创建的组中(请参阅最后一条语句)。 CreateAsync 调用不会返回。
我做错了什么?
Group group = new Group
{
DisplayName = "Test Group",
Description = "description",
MailNickname = "testgroup",
Visibility = "Private",
MailEnabled = true,
SecurityEnabled = false,
GroupTypes = new List<string> { "Unified" },
Members = {} // not working,
Owners = {} // not working,
Team = {} // not working
};
var createdGroup = await graphClient.Groups.Request().AddAsync(group);
foreach (var item in groupMembers)
{
await raphClient.Groups[createdGroup.Id]
.Members.References.Request().AddAsync(item);
}
// not working
var createdTeam = await graphClient.Groups[createdGroup.Id]
.Team.Request().CreateAsync(new Team());
return createdGroup;
最佳答案
我从 SP 列表中拉出我的成员和所有者,然后在循环使用它们时将他们添加到组中。
using Newtonsoft.Json;
// Look up people in SPO column Members and try to add them to the newly created group
string membersPull = item.Fields.AdditionalData["_x010c_lenov_x00e9_t_x00fd_mu"].ToString();
string ownersPull = item.Fields.AdditionalData["SpravciTymu"].ToString();
//on the very bottom, there is another class utilizing this JSON extraction
var members = JsonConvert.DeserializeObject<List<Receiver>>(membersPull);
var owners = JsonConvert.DeserializeObject<List<Receiver>>(ownersPull);
foreach (var member in members)
{
var memberUPN = member.Email;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Adding user: " + member.Email);
Console.ResetColor();
try
{
var addMember = await graphServiceClient.Users[memberUPN].Request().Select("id").GetAsync();
await graphServiceClient.Groups[groupID].Members.References.Request().AddAsync(addMember);
}
catch
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("USER " + memberUPN + "is already MEMBER of: " + group2.DisplayName);
Console.ResetColor();
}
}
对于所有者来说,除了发出的命令是相同的之外
await graphServiceClient.Groups[groupID].Owners.References.Request().AddAsync(addOwner);
这是从 JSON 接收人员属性的调用 -> 提取成员的电子邮件(也是 UPN)。您可以从那里定义您需要的任何其他内容。
public class Receiver
{
public string LookupId { get; set; }
public string LookupValue { get; set; }
public string Email { get; set; }
}
希望它能帮助别人:-)
关于c# - Microsoft Graph .NET 客户端库 : Add members and team to a group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53902719/