Microsoft Graph .NET客户端库:将成员和团队添加到组中

时间:2018-12-23 10:09:56

标签: c# .net microsoft-graph 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;

1 个答案:

答案 0 :(得分:0)

我将我的成员和所有者从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();
 }
}

所有者的foreach相同,除了发出命令是

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; }
}

希望它对某人有帮助:-)