c# - Microsoft Graph .NET 客户端库 : Add members and team to a group

标签 c# .net microsoft-graph-api microsoft-teams

我刚刚开始使用 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/

相关文章:

c# - SQL Server CE 架构

c# - WPF:日期时间选择器选择问题

c# - .net 中的单元和集成测试组织

.net - 从 .NET 中的字符串获取 Type 对象的最佳方法

c# - 无需担心超出字符串边界的简单子(monad)字符串方法?

microsoft-graph-api - 使用 Microsoft Graph 客户端库更新联系人

azure-active-directory - 如何使用/adminconsent端点为一组用户而不是整个组织授予权限?

microsoft-graph-api - 如何使用 Graph API 读取 Azure B2C 自定义属性(适用于 Azure AD Graph)

c# - 为什么我们需要 EndInvoke() 来返回委托(delegate)中异步调用的值?

C# 避免多个 SWITCH 语句 .net