Example #1
0
 private IEnumerable <Models.User> ToUserObjects(IGraphServiceUsersCollectionPage graphUsers)
 {
     return(graphUsers.Select(graphUser => new Models.User()
     {
         UserId = graphUser.Id,
         DisplayName = graphUser.DisplayName
     }));
 }
Example #2
0
            public async Task <IEnumerable <MMM.User> > GetNextPage(bool next)
            {
                IGraphServiceUsersCollectionPage users = null;

                // We only support forward paging
                if (next)
                {
                    if (_next == null)
                    {
                        users = await _graphClient.Users.Request()
                                .Top(_pageSize)
                                .Filter(BuildFilter())
                                .GetAsync();
                    }
                    else
                    {
                        users = await _next.GetAsync();
                    }
                }

                _next = users.NextPageRequest;

                return(users.Select(user => user.ConvertObject <MMM.User>()));
            }
        /// <summary>
        ///
        /// </summary>
        /// <param name="graphClient"></param>
        /// <param name="emailID"></param>
        /// <param name="groupName"></param>
        /// <returns>Return value represents returnStatus, message, userObjectID, groupID respectively</returns>
        public async Task <Tuple <bool, string, string, string> > AssociateUserWithAGroup(GraphServiceClient graphClient, string emailID, string groupName)
        {
            bool   ActionStatus = false;
            string message      = string.Empty;

            string groupid = "", userObjectID = "";

            if (graphClient == null || string.IsNullOrEmpty(emailID) || string.IsNullOrEmpty(groupName))
            {
                return(new Tuple <bool, string, string, string>(ActionStatus, "Invalid input", null, null));
            }

            try
            {
                IGraphServiceUsersCollectionPage _usersFilteredId = await graphClient.Users.Request().Filter($"Mail eq '" + emailID + "' or UserPrincipalName eq '" + emailID + "'").GetAsync();

                userObjectID = _usersFilteredId.Select(a => a.Id).SingleOrDefault().ToString();

                if (!string.IsNullOrEmpty(userObjectID))
                {
                    //find group Id by groupname
                    IGraphServiceGroupsCollectionPage groupsCollectionPage = graphClient.Groups.Request().Filter($"DisplayName eq '" + groupName + "'").GetAsync().Result;

                    if (groupsCollectionPage != null)
                    {
                        groupid = groupsCollectionPage.Select(a => a.Id).SingleOrDefault().ToString();

                        if (groupid != null)
                        {
                            //check if the user is already associated
                            var associatedmember = await graphClient.Groups[groupid].Members.Request().GetAsync();
                            if (associatedmember.Where(a => a.Id == userObjectID).FirstOrDefault() == null)
                            {
                                Microsoft.Graph.User userToAdd = await graphClient.Users[userObjectID].Request().GetAsync();
                                await graphClient.Groups[groupid].Members.References.Request().AddAsync(userToAdd);

                                ActionStatus = true;
                                message      = "User association with group was successful";
                            }
                            else
                            {
                                message = "User already associated with the group";
                            }
                        }
                        else
                        {
                            message = "Invalid group";
                        }
                    }
                    else
                    {
                        message = "Invalid group";
                    }
                }
                else
                {
                    message = "Invalid user";
                }
            }
            catch (Exception ex)
            {
                message = ex.Message + (ex.InnerException != null ? Environment.NewLine + ex.InnerException.Message : "");
            }

            return(new Tuple <bool, string, string, string>(ActionStatus, message, userObjectID, groupid));
        }