2
votes

Im just starting with Microsoft Graph API and .net.

How can I add members and a team to a group using the latest Microsoft.Graph 1.13.0-preview nuget package (Microsoft Graph .NET Client Library)?

Can it be done in one request?

I was not able to set the Members, Owners or Team properties in the new Group statement.

Also I am not able to add a team to the newly created group (see last statement). The CreateAsync call does not return.

What am I doing wrong?

        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
You'll add a team like this: graphClient.Groups[groupPage[8].Id].Team.Request().PutAsync(team) This will be available in the next release. github.com/microsoftgraph/msgraph-sdk-dotnet/pull/352 - Arun-MSFT

1 Answers

1
votes

I pull my members and owners from SP list and then I add them into the group while cycling trough them.

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

Same foreach for owners except the issuing command is

await graphServiceClient.Groups[groupID].Owners.References.Request().AddAsync(addOwner);

This is the calss that receives the attributes of people from JSON -> pulling out the email of the members which is also UPN.You can define any other thing you would need from there.

public class Receiver
{
    public string LookupId  { get; set; }
    public string LookupValue { get; set; }
    public string Email { get; set; }
}

Hope it helps someone:-)