none
How to add member to Office 365 outlook group using c#? RRS feed

  • Question

  • I am trying to add email to existing office 365 group(s) using c#. I have tried in google but could not find right code. Please if anyone has code to share.


    ItsMeSri SP 2013 Foundation

    Friday, September 27, 2019 3:41 PM

All replies

  • Below are references for you.

    First you need to get the group object, then add user to it.

    Get user

    https://docs.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=csharp

    Get group

    https://docs.microsoft.com/en-us/graph/api/group-get?view=graph-rest-1.0&tabs=csharp

    Add member

    The example seems not very correct so please have a look at both three links.https://docs.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-1.0&tabs=csharp


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Sunday, September 29, 2019 1:19 AM
  • check the code of UnifiedGroupsUtility.UpdateMembers() method from OfficeDevPnP. It has good example of how to set owners for the O365 group:

    private static async Task UpdateMembers(string[] members, GraphServiceClient graphClient, Group targetGroup)
    {
    	foreach (var m in members)
    	{
    		// Search for the user object
    		var memberQuery = await graphClient.Users
    			.Request()
    			.Filter($"userPrincipalName eq '{Uri.EscapeDataString(m.Replace("'", "''"))}'")
    			.GetAsync();
    
    		var member = memberQuery.FirstOrDefault();
    
    		if (member != null)
    		{
    			try
    			{
    				// And if any, add it to the collection of group's owners
    				await graphClient.Groups[targetGroup.Id].Members.References.Request().AddAsync(member);
    			}
    			catch (ServiceException ex)
    			{
    				if (ex.Error.Code == "Request_BadRequest" &&
    					ex.Error.Message.Contains("added object references already exist"))
    				{
    					// Skip any already existing member
    				}
    				else
    				{
    					throw ex;
    				}
    			}
    		}
    	}
    
    	// Remove any leftover member
    	var fullListOfMembers = await graphClient.Groups[targetGroup.Id].Members.Request().Select("userPrincipalName, Id").GetAsync();
    	var pageExists = true;
    
    	while (pageExists)
    	{
    		foreach (var member in fullListOfMembers)
    		{
    			var currentMemberPrincipalName = (member as Microsoft.Graph.User)?.UserPrincipalName;
    			if (!String.IsNullOrEmpty(currentMemberPrincipalName) &&
    				!members.Contains(currentMemberPrincipalName, StringComparer.InvariantCultureIgnoreCase))
    			{
    				try
    				{
    					// If it is not in the list of current owners, just remove it
    					await graphClient.Groups[targetGroup.Id].Members[member.Id].Reference.Request().DeleteAsync();
    				}
    				catch (ServiceException ex)
    				{
    					if (ex.Error.Code == "Request_BadRequest")
    					{
    						// Skip any failing removal
    					}
    					else
    					{
    						throw ex;
    					}
    				}
    			}
    		}
    
    		if (fullListOfMembers.NextPageRequest != null)
    		{
    			fullListOfMembers = await fullListOfMembers.NextPageRequest.GetAsync();
    		}
    		else
    		{
    			pageExists = false;
    		}
    	}
    }


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - https://github.com/sadomovalex/camlex

    Monday, September 30, 2019 2:57 PM