0

i have this Method Async to create a group, using the nuget Microsoft Graph, I wanted to know if they help me to add a user to this group in the same moment that is believed.

I have tried it, but the class "Group" in your content, "Owners", "Members" and "MemberOf" they are Interfaces.

i tried that after of to created the group add un Owner, as well a member; but it does not work.

this is one of my proof: User jona = await graphClient.Me.Request().GetAsync(); // This line of code brings all the information of me user logged group.Owners.add(jona); // this lines before of the condition "if"

but appears a exception "SystemNullException".

public async Task<List<ResultsItem>> CreateGroup(GraphServiceClient graphClient)
{

    List<ResultsItem> items = new List<ResultsItem>();
    string guid = Guid.NewGuid().ToString();
    Group group = await graphClient.Groups.Request().AddAsync(new Group
    {
        GroupTypes = new List<string> { "Unified" },
        DisplayName = Resource.Group + guid.Substring(0, 8),
        Description = Resource.Group + guid,
        MailNickname = Resource.Group.ToLower() + guid.Substring(0, 8),
        MailEnabled = false,
        SecurityEnabled = false
    });


    if (group != null)
    {


        // Get group properties.
        items.Add(new ResultsItem
        {
            Display = group.DisplayName,
            Id = group.Id,
            Properties = new Dictionary<string, object>
                {
                    { Resource.Prop_Description, group.Description },
                    { Resource.Prop_Email, group.Mail },
                    { Resource.Prop_Created, group.AdditionalData["createdDateTime"] }, // Temporary solution for a known SDK problem.
                    { Resource.Prop_Id, group.Id }

                }

        });
    }

    return items;
}
Nan Yu
  • 26,101
  • 9
  • 68
  • 148

1 Answers1

2

To add user to Group using Microsoft Graph .NET Client SDK ,you could use :

User userToAdd = await graphClient.Users["objectID"].Request().GetAsync(); 
await graphClient.Groups["groupObjectID"].Members.References.Request().AddAsync(userToAdd);

In addition , you could click here for Microsoft Graph Snippets Sample for ASP.NET 4.6

Nan Yu
  • 26,101
  • 9
  • 68
  • 148
  • I was trying to find an answer to this question, but I was misled by the fact, that graphClient.Groups["groupObjectID"].Members.Request().GetAsync() returns a promising interface typed object that has Add method... but there is nothing further to do with that. What could be the purpose of that? – ZorgoZ Oct 12 '17 at 05:49
  • What is your requirement ? Do you want to get members of a group ? If yes ,see my reply [here](https://stackoverflow.com/a/44172196/5751404) . – Nan Yu Oct 12 '17 at 05:58
  • You got me wrong. I was just wondering why is there an Add method if it has no actual effect. – ZorgoZ Oct 12 '17 at 06:57