示例#1
0
        public static void GroupMembership(Group _,
                                           List <GroupMember> groupMembers)
        {
            var gremlinVertices = new List <GremlinVertex>();
            var gremlinEdges    = new List <GremlinEdge>();

            var vertex = new GremlinVertex(_.Id, nameof(Models.BloodHound.Group));

            vertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, _.Id.GetHashCode());
            vertex.AddProperty(nameof(_.DisplayName), _.DisplayName?.ToUpper() ?? string.Empty);
            gremlinVertices.Add(vertex);

            groupMembers.ForEach(member =>
            {
                var gremlinEdge = new GremlinEdge(
                    _.Id + member.Id,
                    "MemberOf",
                    member.Id,
                    _.Id,
                    member.MemberType,
                    nameof(Models.BloodHound.Group),
                    member.Id.GetHashCode(),
                    _.Id.GetHashCode());

                gremlinEdges.Add(gremlinEdge);
            });

            CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices);
            CosmosDbHelper.RunImportEdgesBlock.Post(gremlinEdges);
        }
示例#2
0
 public AADGroupMember(Graph.Group group, string id, string memberType)
 {
     this.groupId    = group.Id;
     this.groupName  = group.DisplayName;
     this.id         = id;
     this.memberType = memberType;
 }
示例#3
0
        public async Task <bool> AddDepartmentToTable(DepartmentList department)
        {
            string           clientId     = _appSettings.ClientId;
            string           clientSecret = _appSettings.clientSecret;
            string           directory    = _appSettings.directory;
            ClientCredential creds        = new ClientCredential(clientId, clientSecret);

            GraphServiceClient graphClient = new GraphServiceClient(new AzureAuthenticationProvider(creds, directory));

            var grpToAdd = new Microsoft.Graph.Group();

            grpToAdd.DisplayName = department.Name;
            grpToAdd.MailEnabled = false;
            string withoutSpaces = Regex.Replace(department.Name, @"\s+", "");

            grpToAdd.MailNickname    = withoutSpaces;
            grpToAdd.SecurityEnabled = true;
            grpToAdd.MailEnabled     = false;

            var addedgroup = await graphClient.Groups.Request().AddAsync(grpToAdd);


            if (addedgroup != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public async Task <IEnumerable <GroupInfo> > GetAllGroupsOfUser(string mail)
        {
            IList <GroupInfo> result = new List <GroupInfo>();

            IUserMemberOfCollectionWithReferencesPage memberOfGroups = await _graphServiceClient.Users[mail].MemberOf.Request().GetAsync();

            //var transitiveMemberOf = await _graphServiceClient.Users[mail].TransitiveMemberOf.Request().GetAsync();

            do
            {
                foreach (var directoryObject in memberOfGroups)
                {
                    // We only want groups, so ignore DirectoryRole objects.
                    if (directoryObject is Group)
                    {
                        Group group = directoryObject as Group;
                        result.Add(new GroupInfo {
                            Id = group.Id, DisplayName = group.DisplayName
                        });
                    }
                }
            }while (memberOfGroups.NextPageRequest != null && (memberOfGroups = await memberOfGroups.NextPageRequest.GetAsync()).Count > 0);

            return(result);
        }
示例#5
0
        public async Task <string> CreateGroup(Connection connection)
        {
            string[] scopes      = { "Group.ReadWrite.All", "Directory.ReadWrite.All", "Directory.AccessAsUser.All" };
            var      accessToken = await GraphClientConfig.GetTokenForUserAsync(connection, scopes);

            var graphClient = CreateGraphClient(accessToken);
            //var groups = await graphClient.Groups.Request().GetAsync();
            var group = new Microsoft.Graph.Group
            {
                DisplayName = "test team 123",
                Description = "Created using Graph API",
                // Mail = "*****@*****.**",
                MailEnabled  = true,
                MailNickname = "testteam1",
                GroupTypes   = new List <string> {
                    "Unified"
                },
                Visibility      = "private",
                SecurityEnabled = false
            };
            await graphClient.Groups
            .Request()
            .AddAsync(group);

            return(null);
        }
示例#6
0
        private static async Task <Microsoft.Graph.Group> CreateGroupAsync(GraphServiceClient client)
        {
            // create object to define members & owners as 'additionalData'
            var additionalData = new Dictionary <string, object>();

            additionalData.Add("*****@*****.**",
                               new string[] {
                "https://graph.microsoft.com/v1.0/users/3f8f64d5-961f-4067-9f3e-8f5cdcf1b0df"
            }
                               );
            additionalData.Add("*****@*****.**",
                               new string[] {
                "https://graph.microsoft.com/v1.0/users/851f0875-e1c1-4c7e-bdec-3143bb3d4192",
                "https://graph.microsoft.com/v1.0/users/97f4b654-3756-4246-9f9f-1588250f2531"
            }
                               );

            var group = new Microsoft.Graph.Group
            {
                AdditionalData = additionalData,
                Description    = "My first group created with the Microsoft Graph .NET SDK",
                DisplayName    = "My First Group",
                GroupTypes     = new List <String>()
                {
                    "Unified"
                },
                MailEnabled     = true,
                MailNickname    = "myfirstgroup01",
                SecurityEnabled = false
            };

            var requestNewGroup = client.Groups.Request();

            return(await requestNewGroup.AddAsync(group));
        }
示例#7
0
        private async Task UploadImageForGroupAsync(GraphServiceClient graphService, Graph.Group group, string imagePath)
        {
            var filePath = _baseFolderPath + imagePath;

            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                await graphService.Groups[group.Id].Photo.Content.Request().PutAsync(fs);
                return;
            }
        }
示例#8
0
        private async Task <List <string> > GetGroupFrom(GraphServiceClient graphClient, string id)
        {
            List <string> list = new List <string>();

            try
            {
                Microsoft.Graph.Group group = await graphClient.Groups[id].Request().GetAsync();

                if (group != null)
                {
                    IGroupMembersCollectionWithReferencesPage members = await graphClient.Groups[id].Members.Request().GetAsync();
                    var foundMembers = members.CurrentPage.ToList();
                    if (foundMembers != null && foundMembers.Count > 0)
                    {
                        do
                        {
                            foundMembers = members.CurrentPage.ToList();
                            foreach (var member in foundMembers)
                            {
                                try
                                {
                                    var graphMember = await graphClient.Users[member.Id].Request().GetAsync();
                                    list.Add(graphMember.UserPrincipalName.ToLower());

                                    _logger.LogInformation(8, id + "--> " + graphMember.UserPrincipalName.ToLower());
                                }
                                catch (Exception e)
                                {
                                    _logger.LogWarning(8, e, "member: " + member.Id + "|| group: " + group.DisplayName);
                                    // don't crash
                                }
                            }


                            if (members.NextPageRequest != null)
                            {
                                members = await members.NextPageRequest.GetAsync();
                            }
                            else
                            {
                                members = null;
                            }
                        } while (members != null && members.Count > 0);
                    }
                }
                return(list);
            }
            catch (Exception e)
            {
                _logger.LogError(8, e, "GetGroupFrom " + id.ToString());
                return(list);
            }
        }
        public Group CreateGroup(string groupName)
        {
            var group = new Microsoft.Graph.Group
            {
                DisplayName     = groupName,
                MailNickname    = groupName,
                MailEnabled     = true,
                GroupTypes      = new string[] { "Unified" },
                SecurityEnabled = true
            };

            return(groupService.Groups.Request().AddAsync(group).Result);
        }
示例#10
0
        private async Task <PrincipalViewModel> MapGroup(Microsoft.Graph.Group g)
        {
            if (g == null)
            {
                return(null);
            }

            PrincipalViewModel group = new PrincipalViewModel()
            {
                DisplayName = g.DisplayName,
                Mail        = g.Mail,
                JobTitle    = "Group"
            };

            return(group);
        }
示例#11
0
        public async Task <CreateGroupOutputData> Execute(CreateGroupInputData request)
        {
            CreateGroupOutputData response = null;

            try
            {
                var group = new Microsoft.Graph.Group
                {
                    DisplayName = request.Group.GroupName,
                    MailEnabled = true,
                    GroupTypes  = new List <string>
                    {
                        "Unified"
                    },
                    SecurityEnabled = false,
                    MailNickname    = TranslitHelper.Execute(request.Group.GroupName.ToLower())
                };

                var result = await Graph.Groups
                             .Request()
                             .AddAsync(group);

                response = new CreateGroupOutputData
                {
                    Group = new GroupModel(result)
                };

                logger.Information(
                    $"Type: CreateGroupActivity; Method: Execute; Info: Create group with name: {request.Group.GroupName} successfully");
            }
            catch (Exception e)
            {
                logger.Error($"Type: CreateGroupActivity; Method: Execute; Error: {e.Message}");
                throw;
            }

            return(await Task.FromResult(response));
        }
        public static void GroupMembership <T>(Group _, List <T> groupMembers) where T : GroupMember
        {
            try
            {
                var gremlinVertices = new List <GremlinVertex>();
                var gremlinEdges    = new List <GremlinEdge>();

                var vertex = new GremlinVertex(_.Id, nameof(AzureAdLateralMovement.Models.BloodHound.Group));
                vertex.AddProperty(CosmosDbHelper.CollectionPartitionKey, _.Id.GetHashCode());
                vertex.AddProperty(nameof(_.DisplayName), _.DisplayName?.ToUpper() ?? string.Empty);
                gremlinVertices.Add(vertex);

                groupMembers.ForEach(member =>
                {
                    var gremlinEdge = new GremlinEdge(
                        _.Id + member.Id,
                        member is  GroupOwner ? "Owner" : "MemberOf",
                        member.Id,
                        _.Id,
                        member.Type,
                        nameof(AzureAdLateralMovement.Models.BloodHound.Group),
                        member.Id.GetHashCode(),
                        _.Id.GetHashCode());

                    gremlinEdges.Add(gremlinEdge);
                });

                CosmosDbHelper.RunImportVerticesBlock.Post(gremlinVertices);
                CosmosDbHelper.RunImportEdgesBlock.Post(gremlinEdges);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        /// <summary>
        /// This method will create an Office 365 group
        /// </summary>
        /// <param name="graphClient"></param>
        /// <param name="log"></param>
        /// <param name="description"></param>
        /// <param name="displayName"></param>
        /// <param name="mailNickname"></param>
        /// <returns></returns>
        public static async Task <string> CreateGroupAndSite(GraphServiceClient graphClient, TraceWriter log, string description, string displayName, string mailNickname)
        {
            var o365Group = new Microsoft.Graph.Group
            {
                Description = description,
                DisplayName = $@"{displayName}",
                GroupTypes  = new List <String>()
                {
                    "Unified"
                },
                MailEnabled     = true,
                MailNickname    = mailNickname,
                SecurityEnabled = false,
                Visibility      = "Private"
            };

            var result = await graphClient.Groups
                         .Request()
                         .AddAsync(o365Group);

            log.Info($"Site and Office 365 {displayName} created successfully.");

            return(result.Id);
        }
        /// <summary>
        /// Creates a new Office 365 Group (i.e. Unified Group) with its backing Modern SharePoint Site
        /// </summary>
        /// <param name="displayName">The Display Name for the Office 365 Group</param>
        /// <param name="description">The Description for the Office 365 Group</param>
        /// <param name="mailNickname">The Mail Nickname for the Office 365 Group</param>
        /// <param name="accessToken">The OAuth 2.0 Access Token to use for invoking the Microsoft Graph</param>
        /// <param name="owners">A list of UPNs for group owners, if any</param>
        /// <param name="members">A list of UPNs for group members, if any</param>
        /// <param name="groupLogo">The binary stream of the logo for the Office 365 Group</param>
        /// <param name="isPrivate">Defines whether the group will be private or public, optional with default false (i.e. public)</param>
        /// <param name="retryCount">Number of times to retry the request in case of throttling</param>
        /// <param name="delay">Milliseconds to wait before retrying the request. The delay will be increased (doubled) every retry</param>
        /// <returns>The just created Office 365 Group</returns>
        public static UnifiedGroupEntity CreateUnifiedGroup(string displayName, string description, string mailNickname,
                                                            string accessToken, string[] owners = null, string[] members = null, Stream groupLogo = null,
                                                            bool isPrivate = false, int retryCount = 10, int delay = 500)
        {
            UnifiedGroupEntity result = null;

            if (String.IsNullOrEmpty(displayName))
            {
                throw new ArgumentNullException(nameof(displayName));
            }

            if (String.IsNullOrEmpty(description))
            {
                throw new ArgumentNullException(nameof(description));
            }

            if (String.IsNullOrEmpty(mailNickname))
            {
                throw new ArgumentNullException(nameof(mailNickname));
            }

            if (String.IsNullOrEmpty(accessToken))
            {
                throw new ArgumentNullException(nameof(accessToken));
            }

            // Use a synchronous model to invoke the asynchronous process
            result = Task.Run(async() =>
            {
                var group = new UnifiedGroupEntity();

                var graphClient = CreateGraphClient(accessToken, retryCount, delay);

                // Prepare the group resource object
                var newGroup = new Microsoft.Graph.Group
                {
                    DisplayName     = displayName,
                    Description     = description,
                    MailNickname    = mailNickname,
                    MailEnabled     = true,
                    SecurityEnabled = false,
                    Visibility      = isPrivate == true ? "Private" : "Public",
                    GroupTypes      = new List <string> {
                        "Unified"
                    },
                };

                Microsoft.Graph.Group addedGroup = null;
                String modernSiteUrl             = null;

                // Add the group to the collection of groups (if it does not exist
                if (addedGroup == null)
                {
                    addedGroup = await graphClient.Groups.Request().AddAsync(newGroup);

                    if (addedGroup != null)
                    {
                        group.DisplayName  = addedGroup.DisplayName;
                        group.Description  = addedGroup.Description;
                        group.GroupId      = addedGroup.Id;
                        group.Mail         = addedGroup.Mail;
                        group.MailNickname = addedGroup.MailNickname;

                        int imageRetryCount = retryCount;

                        if (groupLogo != null)
                        {
                            using (var memGroupLogo = new MemoryStream())
                            {
                                groupLogo.CopyTo(memGroupLogo);

                                while (imageRetryCount > 0)
                                {
                                    bool groupLogoUpdated = false;
                                    memGroupLogo.Position = 0;

                                    using (var tempGroupLogo = new MemoryStream())
                                    {
                                        memGroupLogo.CopyTo(tempGroupLogo);
                                        tempGroupLogo.Position = 0;

                                        try
                                        {
                                            groupLogoUpdated = UpdateUnifiedGroup(addedGroup.Id,
                                                                                  accessToken, groupLogo: tempGroupLogo);
                                        }
                                        catch
                                        {
                                            // Skip any exception and simply retry
                                        }
                                    }

                                    // In case of failure retry up to 10 times, with 500ms delay in between
                                    if (!groupLogoUpdated)
                                    {
                                        // Pop up the delay for the group image
                                        Thread.Sleep(TimeSpan.FromMilliseconds(delay * (retryCount - imageRetryCount)));
                                        imageRetryCount--;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }

                        int driveRetryCount = retryCount;

                        while (driveRetryCount > 0 && String.IsNullOrEmpty(modernSiteUrl))
                        {
                            try
                            {
                                modernSiteUrl = GetUnifiedGroupSiteUrl(addedGroup.Id, accessToken);
                            }
                            catch
                            {
                                // Skip any exception and simply retry
                            }

                            // In case of failure retry up to 10 times, with 500ms delay in between
                            if (String.IsNullOrEmpty(modernSiteUrl))
                            {
                                Thread.Sleep(TimeSpan.FromMilliseconds(delay * (retryCount - driveRetryCount)));
                                driveRetryCount--;
                            }
                        }

                        group.SiteUrl = modernSiteUrl;
                    }
                }

                #region Handle group's owners

                if (owners != null && owners.Length > 0)
                {
                    // For each and every owner
                    foreach (var o in owners)
                    {
                        // Search for the user object
                        var ownerQuery = await graphClient.Users
                                         .Request()
                                         .Filter($"userPrincipalName eq '{o}'")
                                         .GetAsync();

                        var owner = ownerQuery.FirstOrDefault();

                        if (owner != null)
                        {
                            try
                            {
                                // And if any, add it to the collection of group's owners
                                await graphClient.Groups[addedGroup.Id].Owners.References.Request().AddAsync(owner);
                            }
                            catch (ServiceException ex)
                            {
                                if (ex.Error.Code == "Request_BadRequest" &&
                                    ex.Error.Message.Contains("added object references already exist"))
                                {
                                    // Skip any already existing owner
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                        }
                    }
                }

                #endregion

                #region Handle group's members

                if (members != null && members.Length > 0)
                {
                    // For each and every owner
                    foreach (var m in members)
                    {
                        // Search for the user object
                        var memberQuery = await graphClient.Users
                                          .Request()
                                          .Filter($"userPrincipalName eq '{m}'")
                                          .GetAsync();

                        var member = memberQuery.FirstOrDefault();

                        if (member != null)
                        {
                            try
                            {
                                // And if any, add it to the collection of group's owners
                                await graphClient.Groups[addedGroup.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;
                                }
                            }
                        }
                    }
                }

                #endregion

                return(group);
            }).GetAwaiter().GetResult();

            return(result);
        }
        public static async Task Run()
        {
            /////////////////
            //
            // Configuration
            //
            /////////////////

            AzureAdOptions azureAdOptions = new AzureAdOptions();

            var settingsFilename = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "appsettings.json");
            var builder          = new ConfigurationBuilder()
                                   .AddJsonFile(settingsFilename, optional: false);
            var config = builder.Build();

            config.Bind("AzureAd", azureAdOptions);

            // Log Http Request/Response
            var logger = new StringBuilderHttpMessageLogger();

            // Use the system browser to login
            //  https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/System-Browser-on-.Net-Core#how-to-use-the-system-browser-ie-the-default-browser-of-the-os

            var options = new PublicClientApplicationOptions()
            {
                AadAuthorityAudience = AadAuthorityAudience.AzureAdMyOrg,
                AzureCloudInstance   = AzureCloudInstance.AzurePublic,
                ClientId             = azureAdOptions.ClientId,
                TenantId             = azureAdOptions.TenantId,
                RedirectUri          = "http://localhost"
            };

            // Create the public client application (desktop app), with a default redirect URI
            var pca = PublicClientApplicationBuilder.CreateWithApplicationOptions(options)
                      .Build();

            // Enable a simple token cache serialiation so that the user does not need to
            // re-sign-in each time the application is run
            TokenCacheHelper.EnableSerialization(pca.UserTokenCache);

            // Create an authentication provider to attach the token to requests
            var scopes = new string[] { "https://graph.microsoft.com/User.ReadBasic.All" };
            IAuthenticationProvider ap = new InteractiveAuthenticationProvider(pca, scopes);


            ////////////////////////////////////////////////////////////////
            //
            //  Create a GraphClient with the Logging handler
            //
            ////////////////////////////////////////////////////////////////

            // Configure our client
            CommunityGraphClientOptions clientOptions = new CommunityGraphClientOptions()
            {
                UserAgent = "NOVA365-UG-Demo"
            };

            var graphServiceClient = CommunityGraphClientFactory.Create(clientOptions, logger, ap);


            ////////////////////////////
            //
            // Setup is complete, run the sample
            //
            ////////////////////////////

            try
            {
                var searchFor    = "al";
                var filterString = $"startswith(givenName,'{searchFor}') or startswith(surname,'{searchFor}') or startswith(displayName,'{searchFor}')";

                var users = await graphServiceClient
                            .Users
                            .Request()
                            .Filter(filterString)
                            .GetAsync();


                var u = await graphServiceClient.Users["*****@*****.**"].Request().GetAsync();

                var g = new Microsoft.Graph.Group
                {
                    DisplayName     = "NOVA 365 UG",
                    MailEnabled     = false,
                    MailNickname    = "novaug",
                    SecurityEnabled = true
                };

                g.AddMember(u.Id);
                g = await graphServiceClient.Groups.Request().AddAsync(g);

                Console.WriteLine($"Group: {g.DisplayName} ({g.Id})");
            }
            catch (Exception ex)
            {
                await logger.WriteLine("");

                await logger.WriteLine("================== Exception caught ==================");

                await logger.WriteLine(ex.ToString());
            }


            Console.WriteLine("Press enter to show log");
            Console.ReadLine();
            Console.WriteLine();
            var log = logger.GetLog();

            Console.WriteLine(log);
        }
        /// <summary>
        /// Creates a new Office 365 Group (i.e. Unified Group) with its backing Modern SharePoint Site
        /// </summary>
        /// <param name="displayName">The Display Name for the Office 365 Group</param>
        /// <param name="description">The Description for the Office 365 Group</param>
        /// <param name="mailNickname">The Mail Nickname for the Office 365 Group</param>
        /// <param name="accessToken">The OAuth 2.0 Access Token to use for invoking the Microsoft Graph</param>
        /// <param name="owners">A list of UPNs for group owners, if any</param>
        /// <param name="members">A list of UPNs for group members, if any</param>
        /// <param name="groupLogo">The binary stream of the logo for the Office 365 Group</param>
        /// <param name="isPrivate">Defines whether the group will be private or public, optional with default false (i.e. public)</param>
        /// <param name="retryCount">Number of times to retry the request in case of throttling</param>
        /// <param name="delay">Milliseconds to wait before retrying the request. The delay will be increased (doubled) every retry</param>
        /// <returns>The just created Office 365 Group</returns>
        public static UnifiedGroupEntity CreateUnifiedGroup(string displayName, string description, string mailNickname,
                                                            string accessToken, string[] owners = null, string[] members = null, Stream groupLogo = null,
                                                            bool isPrivate = false, int retryCount = 10, int delay = 500)
        {
            UnifiedGroupEntity result = null;

            if (String.IsNullOrEmpty(displayName))
            {
                throw new ArgumentNullException(nameof(displayName));
            }

            if (String.IsNullOrEmpty(description))
            {
                throw new ArgumentNullException(nameof(description));
            }

            if (String.IsNullOrEmpty(mailNickname))
            {
                throw new ArgumentNullException(nameof(mailNickname));
            }

            if (String.IsNullOrEmpty(accessToken))
            {
                throw new ArgumentNullException(nameof(accessToken));
            }

            try
            {
                // Use a synchronous model to invoke the asynchronous process
                result = Task.Run(async() =>
                {
                    var group = new UnifiedGroupEntity();

                    var graphClient = CreateGraphClient(accessToken, retryCount, delay);

                    // Prepare the group resource object
                    var newGroup = new Microsoft.Graph.Group
                    {
                        DisplayName     = displayName,
                        Description     = description,
                        MailNickname    = mailNickname,
                        MailEnabled     = true,
                        SecurityEnabled = false,
                        Visibility      = isPrivate == true ? "Private" : "Public",
                        GroupTypes      = new List <string> {
                            "Unified"
                        },
                    };

                    Microsoft.Graph.Group addedGroup = null;
                    String modernSiteUrl             = null;

                    // Add the group to the collection of groups (if it does not exist
                    if (addedGroup == null)
                    {
                        addedGroup = await graphClient.Groups.Request().AddAsync(newGroup);

                        if (addedGroup != null)
                        {
                            group.DisplayName  = addedGroup.DisplayName;
                            group.Description  = addedGroup.Description;
                            group.GroupId      = addedGroup.Id;
                            group.Mail         = addedGroup.Mail;
                            group.MailNickname = addedGroup.MailNickname;

                            int imageRetryCount = retryCount;

                            if (groupLogo != null)
                            {
                                using (var memGroupLogo = new MemoryStream())
                                {
                                    groupLogo.CopyTo(memGroupLogo);

                                    while (imageRetryCount > 0)
                                    {
                                        bool groupLogoUpdated = false;
                                        memGroupLogo.Position = 0;

                                        using (var tempGroupLogo = new MemoryStream())
                                        {
                                            memGroupLogo.CopyTo(tempGroupLogo);
                                            tempGroupLogo.Position = 0;

                                            try
                                            {
                                                groupLogoUpdated = UpdateUnifiedGroup(addedGroup.Id, accessToken, groupLogo: tempGroupLogo);
                                            }
                                            catch
                                            {
                                                // Skip any exception and simply retry
                                            }
                                        }

                                        // In case of failure retry up to 10 times, with 500ms delay in between
                                        if (!groupLogoUpdated)
                                        {
                                            // Pop up the delay for the group image
                                            await Task.Delay(delay * (retryCount - imageRetryCount));
                                            imageRetryCount--;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }

                            int driveRetryCount = retryCount;

                            while (driveRetryCount > 0 && String.IsNullOrEmpty(modernSiteUrl))
                            {
                                try
                                {
                                    modernSiteUrl = GetUnifiedGroupSiteUrl(addedGroup.Id, accessToken);
                                }
                                catch
                                {
                                    // Skip any exception and simply retry
                                }

                                // In case of failure retry up to 10 times, with 500ms delay in between
                                if (String.IsNullOrEmpty(modernSiteUrl))
                                {
                                    await Task.Delay(delay * (retryCount - driveRetryCount));
                                    driveRetryCount--;
                                }
                            }

                            group.SiteUrl = modernSiteUrl;
                        }
                    }

                    #region Handle group's owners

                    if (owners != null && owners.Length > 0)
                    {
                        await UpdateOwners(owners, graphClient, addedGroup);
                    }

                    #endregion

                    #region Handle group's members

                    if (members != null && members.Length > 0)
                    {
                        await UpdateMembers(members, graphClient, addedGroup);
                    }

                    #endregion

                    return(group);
                }).GetAwaiter().GetResult();
            }
            catch (ServiceException ex)
            {
                Log.Error(Constants.LOGGING_SOURCE, CoreResources.GraphExtensions_ErrorOccured, ex.Error.Message);
                throw;
            }
            return(result);
        }
示例#17
0
        /// <summary>
        /// Initializes any collection properties after deserialization, like next requests for paging.
        /// </summary>
        /// <param name="groupToInitialize">The <see cref="Group"/> with the collection properties to initialize.</param>
        private void InitializeCollectionProperties(Group groupToInitialize)
        {
            if (groupToInitialize != null && groupToInitialize.AdditionalData != null)
            {
                if (groupToInitialize.Members != null && groupToInitialize.Members.CurrentPage != null)
                {
                    groupToInitialize.Members.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Members.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.MemberOf != null && groupToInitialize.MemberOf.CurrentPage != null)
                {
                    groupToInitialize.MemberOf.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.MemberOf.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.MembersWithLicenseErrors != null && groupToInitialize.MembersWithLicenseErrors.CurrentPage != null)
                {
                    groupToInitialize.MembersWithLicenseErrors.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.MembersWithLicenseErrors.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.TransitiveMembers != null && groupToInitialize.TransitiveMembers.CurrentPage != null)
                {
                    groupToInitialize.TransitiveMembers.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.TransitiveMembers.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.TransitiveMemberOf != null && groupToInitialize.TransitiveMemberOf.CurrentPage != null)
                {
                    groupToInitialize.TransitiveMemberOf.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.TransitiveMemberOf.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Owners != null && groupToInitialize.Owners.CurrentPage != null)
                {
                    groupToInitialize.Owners.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Owners.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Settings != null && groupToInitialize.Settings.CurrentPage != null)
                {
                    groupToInitialize.Settings.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Settings.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Extensions != null && groupToInitialize.Extensions.CurrentPage != null)
                {
                    groupToInitialize.Extensions.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Extensions.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Threads != null && groupToInitialize.Threads.CurrentPage != null)
                {
                    groupToInitialize.Threads.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Threads.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.CalendarView != null && groupToInitialize.CalendarView.CurrentPage != null)
                {
                    groupToInitialize.CalendarView.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.CalendarView.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Events != null && groupToInitialize.Events.CurrentPage != null)
                {
                    groupToInitialize.Events.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Events.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Conversations != null && groupToInitialize.Conversations.CurrentPage != null)
                {
                    groupToInitialize.Conversations.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Conversations.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Photos != null && groupToInitialize.Photos.CurrentPage != null)
                {
                    groupToInitialize.Photos.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Photos.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.AcceptedSenders != null && groupToInitialize.AcceptedSenders.CurrentPage != null)
                {
                    groupToInitialize.AcceptedSenders.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.AcceptedSenders.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.RejectedSenders != null && groupToInitialize.RejectedSenders.CurrentPage != null)
                {
                    groupToInitialize.RejectedSenders.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.RejectedSenders.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Drives != null && groupToInitialize.Drives.CurrentPage != null)
                {
                    groupToInitialize.Drives.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Drives.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Sites != null && groupToInitialize.Sites.CurrentPage != null)
                {
                    groupToInitialize.Sites.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Sites.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.GroupLifecyclePolicies != null && groupToInitialize.GroupLifecyclePolicies.CurrentPage != null)
                {
                    groupToInitialize.GroupLifecyclePolicies.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.GroupLifecyclePolicies.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }
            }
        }
示例#18
0
 /// <summary>
 /// Creates the specified Group using POST.
 /// </summary>
 /// <param name="groupToCreate">The Group to create.</param>
 /// <returns>The created Group.</returns>
 public System.Threading.Tasks.Task <Group> CreateAsync(Group groupToCreate)
 {
     return(this.CreateAsync(groupToCreate, CancellationToken.None));
 }
        public async Task <CreateOrAddTeamOutputData> Execute(CreateOrAddTeamInputData request)
        {
            CreateOrAddTeamOutputData response = null;

            try
            {
                foreach (var item in request.List)
                {
                    var group = new Microsoft.Graph.Group
                    {
                        DisplayName = item.Name,
                        MailEnabled = true,
                        GroupTypes  = new List <string>
                        {
                            "Unified"
                        },
                        SecurityEnabled = false,
                        MailNickname    = TranslitHelper.Execute(item.Name.Replace(" ", "").Replace(".", "").ToLower())
                    };

                    var result = await Graph.Groups
                                 .Request()
                                 .AddAsync(group);

                    var owner = new DirectoryObject
                    {
                        Id = item.OwnerGUID
                    };

                    await Graph.Groups[result.Id].Owners.References
                    .Request()
                    .AddAsync(owner);

                    var teamSettings = new Microsoft.Graph.Team
                    {
                        MemberSettings = new TeamMemberSettings
                        {
                            AllowCreatePrivateChannels = true,
                            AllowCreateUpdateChannels  = true,
                            ODataType = null
                        },
                        MessagingSettings = new TeamMessagingSettings
                        {
                            AllowUserEditMessages   = true,
                            AllowUserDeleteMessages = true,
                            ODataType = null
                        },
                        FunSettings = new TeamFunSettings
                        {
                            AllowGiphy         = true,
                            GiphyContentRating = GiphyRatingType.Strict,
                            ODataType          = null
                        },
                        ODataType = null
                    };

                    var team = await Graph.Groups[result.Id].Team
                               .Request()
                               .PutAsync(teamSettings);


                    var conversationMember = new AadUserConversationMember
                    {
                        AdditionalData = new Dictionary <string, object>()
                    };

                    foreach (var row in item.ParticipantsList)
                    {
                        conversationMember.AdditionalData.Add(
                            "*****@*****.**", $"https://graph.microsoft.com/v1.0/users('{row}')");
                    }

                    await Graph.Teams[team.Id].Members
                    .Request()
                    .AddAsync(conversationMember);
                }

                response = new CreateOrAddTeamOutputData();

                logger.Information(
                    "Type: CreateOrAddTeamActivity; Method: Execute; Info: Create team for request successfully");
            }
            catch (Exception e)
            {
                logger.Error($"Type: CreateOrAddTeamActivity; Method: Execute; Error: {e.Message}");
                throw;
            }

            return(await Task.FromResult(response));
        }
示例#20
0
 public AADUser(Graph.Group group, string id, string memberType, string displayName, string userPrincipalName) : base(group, id, "User")
 {
     this.displayName       = displayName;
     this.userPrincipalName = userPrincipalName;
 }
示例#21
0
 public AADServicePrincipal(Graph.Group group, string id, string memberType, string displayName, string applicationId) : base(group, id, "SPN")
 {
     this.displayName   = displayName;
     this.applicationId = applicationId;
 }
示例#22
0
        /// <summary>
        /// Initializes any collection properties after deserialization, like next requests for paging.
        /// </summary>
        /// <param name="groupToInitialize">The <see cref="Group"/> with the collection properties to initialize.</param>
        private void InitializeCollectionProperties(Group groupToInitialize)
        {
            if (groupToInitialize != null && groupToInitialize.AdditionalData != null)
            {
                if (groupToInitialize.Members != null && groupToInitialize.Members.CurrentPage != null)
                {
                    groupToInitialize.Members.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Members.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.MemberOf != null && groupToInitialize.MemberOf.CurrentPage != null)
                {
                    groupToInitialize.MemberOf.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.MemberOf.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Owners != null && groupToInitialize.Owners.CurrentPage != null)
                {
                    groupToInitialize.Owners.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Owners.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Threads != null && groupToInitialize.Threads.CurrentPage != null)
                {
                    groupToInitialize.Threads.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Threads.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.CalendarView != null && groupToInitialize.CalendarView.CurrentPage != null)
                {
                    groupToInitialize.CalendarView.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.CalendarView.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Events != null && groupToInitialize.Events.CurrentPage != null)
                {
                    groupToInitialize.Events.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Events.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.Conversations != null && groupToInitialize.Conversations.CurrentPage != null)
                {
                    groupToInitialize.Conversations.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.Conversations.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.AcceptedSenders != null && groupToInitialize.AcceptedSenders.CurrentPage != null)
                {
                    groupToInitialize.AcceptedSenders.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.AcceptedSenders.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }

                if (groupToInitialize.RejectedSenders != null && groupToInitialize.RejectedSenders.CurrentPage != null)
                {
                    groupToInitialize.RejectedSenders.AdditionalData = groupToInitialize.AdditionalData;

                    object nextPageLink;
                    groupToInitialize.AdditionalData.TryGetValue("*****@*****.**", out nextPageLink);
                    var nextPageLinkString = nextPageLink as string;

                    if (!string.IsNullOrEmpty(nextPageLinkString))
                    {
                        groupToInitialize.RejectedSenders.InitializeNextPageRequest(
                            this.Client,
                            nextPageLinkString);
                    }
                }
            }
        }
示例#23
0
 /// <summary>
 /// Adds the specified Group to the collection via POST.
 /// </summary>
 /// <param name="group">The Group to add.</param>
 /// <returns>The created Group.</returns>
 public System.Threading.Tasks.Task <Group> AddAsync(Group group)
 {
     return(this.AddAsync(group, CancellationToken.None));
 }
        public async Task <IGroup> CreateUnifiedGroupForPropertyAsync(GraphService graphService, ListItem propertyItem, IEnumerable <Graph.IUser> members)
        {
            var propertyTitle     = propertyItem["Title"] as string;
            var propertyOwnerName = propertyItem["sl_owner"] as string;
            var propertyOwner     = await graphService.GetFirstUserAsync(i => i.displayName == propertyOwnerName);

            // Create group
            var group = new Graph.Group
            {
                displayName     = propertyTitle,
                mailNickname    = propertyTitle.Replace(" ", ""),
                securityEnabled = false,
                mailEnabled     = true,
                groupType       = "Unified",
                description     = "Property Group",
            };

            try
            {
                await graphService.groups.AddGroupAsync(group);
            }
            catch { }

            // Add users to group
            var groupMembers  = new HashSet <Graph.IUser>(members);
            var groupOwners   = new HashSet <Graph.IUser>();
            var adminUserName = HttpContext.Current.User.Identity.Name;
            var admin         = await graphService.GetFirstUserAsync(i => i.mail == adminUserName);

            if (admin != null)
            {
                groupMembers.Add(admin);
                groupOwners.Add(admin);
            }
            if (propertyOwner != null)
            {
                groupMembers.Add(propertyOwner);
                groupOwners.Add(propertyOwner);
            }

            foreach (var user in groupMembers.OfType <Graph.User>())
            {
                group.members.Add(user);
                try
                {
                    await group.SaveChangesAsync();
                }
                catch { }
            }

            foreach (var user in groupOwners.OfType <Graph.User>())
            {
                group.owners.Add(user);
                try
                {
                    await group.SaveChangesAsync();
                }
                catch { }
            }

            return(group);
        }
示例#25
0
        private async static void CreateGraphGroup(string title)
        {
            string nick        = UrlFriendlyString(title);
            string accessToken = ContextHelper.GetAccessToken().Result;


            GraphServiceClient graphClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    async(requestMessage) =>
            {
                // Append the access token to the request.
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
            }));



            Microsoft.Graph.Group addedGroup = null;

            var existinggroups = await graphClient.Groups.Request().Filter("mailNickName eq '" + nick + "'").GetAsync();

            if (existinggroups.Count > 0)
            {
                addedGroup = existinggroups[0];
            }

            if (addedGroup == null)
            {
                var newGroup = new Microsoft.Graph.Group
                {
                    DisplayName     = title,
                    Description     = title,
                    MailNickname    = nick,
                    MailEnabled     = true,
                    SecurityEnabled = false,
                    Visibility      = "Private",
                    GroupTypes      = new List <string> {
                        "Unified"
                    }
                };

                addedGroup = await graphClient.Groups.Request().AddAsync(newGroup);

                var user = await graphClient.Users["*****@*****.**"].Request().GetAsync();
                await graphClient.Groups[addedGroup.Id].Owners.References.Request().AddAsync(user);
            }

            string webUrl  = "";
            int    seconds = 0;

            do
            {
                Thread.Sleep(2000);
                seconds += 2;
                try
                {
                    var site = await graphClient.Groups[addedGroup.Id].Sites["root"].Request().Select("webUrl").GetAsync();
                    webUrl = site.WebUrl;
                }
                catch (Exception)
                {
                    Console.WriteLine("not found at " + seconds);
                    if (seconds > 30)
                    {
                        break;
                    }
                }
            } while (webUrl == "");
            Console.WriteLine(webUrl);
            using (ClientContext ctx = ContextHelper.GetContext(webUrl))
            {
                ctx.Load(ctx.Web);
                ctx.ExecuteQuery();

                Console.WriteLine(ctx.Web.Title);
            }
        }
        /// <summary>
        /// Creates a new Office 365 Group (i.e. Unified Group) with its backing Modern SharePoint Site
        /// </summary>
        /// <param name="displayName">The Display Name for the Office 365 Group</param>
        /// <param name="description">The Description for the Office 365 Group</param>
        /// <param name="mailNickname">The Mail Nickname for the Office 365 Group</param>
        /// <param name="accessToken">The OAuth 2.0 Access Token to use for invoking the Microsoft Graph</param>
        /// <param name="owners">A list of UPNs for group owners, if any</param>
        /// <param name="members">A list of UPNs for group members, if any</param>
        /// <param name="groupLogo">The binary stream of the logo for the Office 365 Group</param>
        /// <param name="isPrivate">Defines whether the group will be private or public, optional with default false (i.e. public)</param>
        /// <param name="retryCount">Number of times to retry the request in case of throttling</param>
        /// <param name="delay">Milliseconds to wait before retrying the request. The delay will be increased (doubled) every retry</param>
        /// <returns>The just created Office 365 Group</returns>
        public static UnifiedGroupEntity CreateUnifiedGroup(string displayName, string description, string mailNickname,
            string accessToken, string[] owners = null, string[] members = null, Stream groupLogo = null,
            bool isPrivate = false, int retryCount = 10, int delay = 500)
        {
            UnifiedGroupEntity result = null;

            if (String.IsNullOrEmpty(displayName))
            {
                throw new ArgumentNullException("displayName");
            }

            if (String.IsNullOrEmpty(description))
            {
                throw new ArgumentNullException("description");
            }

            if (String.IsNullOrEmpty(mailNickname))
            {
                throw new ArgumentNullException("mailNickname");
            }

            if (String.IsNullOrEmpty(accessToken))
            {
                throw new ArgumentNullException("accessToken");
            }

            // Use a synchronous model to invoke the asynchronous process
            result = Task.Run(async () =>
            {
                var group = new UnifiedGroupEntity();

                var graphClient = CreateGraphClient(accessToken, retryCount, delay);

                // Prepare the group resource object
                var newGroup = new Microsoft.Graph.Group
                {
                    DisplayName = displayName,
                    Description = description,
                    MailNickname = mailNickname,
                    MailEnabled = true,
                    SecurityEnabled = false,
                    GroupTypes = new List<string> { "Unified" },
                };

                Microsoft.Graph.Group addedGroup = null;
                String modernSiteUrl = null;

                // Add the group to the collection of groups (if it does not exist
                if (addedGroup == null)
                {
                    addedGroup = await graphClient.Groups.Request().AddAsync(newGroup);

                    if (addedGroup != null)
                    {
                        group.DisplayName = addedGroup.DisplayName;
                        group.Description = addedGroup.Description;
                        group.GroupId = addedGroup.Id;
                        group.Mail = addedGroup.Mail;
                        group.MailNickname = addedGroup.MailNickname;

                        int imageRetryCount = retryCount;

                        if (groupLogo != null)
                        {
                            using (var memGroupLogo = new MemoryStream())
                            {
                                groupLogo.CopyTo(memGroupLogo);

                                while (imageRetryCount > 0)
                                {
                                    bool groupLogoUpdated = false;
                                    memGroupLogo.Position = 0;

                                    using (var tempGroupLogo = new MemoryStream())
                                    {
                                        memGroupLogo.CopyTo(tempGroupLogo);
                                        tempGroupLogo.Position = 0;
                                        
                                        try
                                        {
                                            groupLogoUpdated = UpdateUnifiedGroup(addedGroup.Id,
                                                accessToken, groupLogo: tempGroupLogo);
                                        }
                                        catch
                                        {
                                            // Skip any exception and simply retry
                                        }
                                    }

                                    // In case of failure retry up to 10 times, with 500ms delay in between
                                    if (!groupLogoUpdated)
                                    {
                                        // Pop up the delay for the group image
                                        Thread.Sleep(TimeSpan.FromMilliseconds(delay * (retryCount - imageRetryCount)));
                                        imageRetryCount--;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }

                        int driveRetryCount = retryCount;

                        while (driveRetryCount > 0 && String.IsNullOrEmpty(modernSiteUrl))
                        {
                            try
                            {
                                modernSiteUrl = GetUnifiedGroupSiteUrl(addedGroup.Id, accessToken);
                            }
                            catch
                            {
                                // Skip any exception and simply retry
                            }

                            // In case of failure retry up to 10 times, with 500ms delay in between
                            if (String.IsNullOrEmpty(modernSiteUrl))
                            {
                                Thread.Sleep(TimeSpan.FromMilliseconds(delay * (retryCount - driveRetryCount)));
                                driveRetryCount--;
                            }
                        }

                        group.SiteUrl = modernSiteUrl;
                    }
                }

                #region Handle group's owners

                if (owners != null && owners.Length > 0)
                {
                    // For each and every owner
                    foreach (var o in owners)
                    {
                        // Search for the user object
                        var ownerQuery = await graphClient.Users
                            .Request()
                            .Filter($"userPrincipalName eq '{o}'")
                            .GetAsync();

                        var owner = ownerQuery.FirstOrDefault();

                        if (owner != null)
                        {
                            try
                            {
                                // And if any, add it to the collection of group's owners
                                await graphClient.Groups[addedGroup.Id].Owners.References.Request().AddAsync(owner);
                            }
                            catch (ServiceException ex)
                            {
                                if (ex.Error.Code == "Request_BadRequest" &&
                                    ex.Error.Message.Contains("added object references already exist"))
                                {
                                    // Skip any already existing owner
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                        }
                    }
                }

                #endregion

                #region Handle group's members

                if (members != null && members.Length > 0)
                {
                    // For each and every owner
                    foreach (var m in members)
                    {
                        // Search for the user object
                        var memberQuery = await graphClient.Users
                            .Request()
                            .Filter($"userPrincipalName eq '{m}'")
                            .GetAsync();

                        var member = memberQuery.FirstOrDefault();

                        if (member != null)
                        {
                            try
                            {
                                // And if any, add it to the collection of group's owners
                                await graphClient.Groups[addedGroup.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;
                                }
                            }
                        }
                    }
                }

                #endregion

                return (group);

            }).GetAwaiter().GetResult();

            return (result);
        }
示例#27
0
        public async Task <Graph.Group> CreateUnifiedGroupForPropertyAsync(GraphServiceClient graphService, string graphAccessToken, ListItem propertyItem, IEnumerable <Graph.User> members)
        {
            var g = new Graph.Group
            {
                DisplayName     = "My Group",
                MailNickname    = "MyGroup",
                SecurityEnabled = false,
                MailEnabled     = true,
                Description     = "A nice group",
                GroupTypes      = new[] { "Unified" },
            };

            g = await graphService.Groups.Request().AddAsync(g);

            var propertyTitle       = propertyItem["Title"] as string;
            var propertyOwnerName   = propertyItem["sl_owner"] as string;
            var querypropertyOwners = (await graphService.Users.Request().Filter(string.Format("displayName eq '{0}'", propertyOwnerName)).GetAsync()).CurrentPage;
            var propertyOwner       = querypropertyOwners.Count > 0 ? querypropertyOwners[0] : null;

            if (propertyOwner == null)
            {
                return(null);
            }
            // Create Office 365 Group
            string  groupId   = string.Empty;
            dynamic groupJSON = new JObject();

            groupJSON.displayName     = propertyTitle;
            groupJSON.mailNickname    = propertyTitle.Replace(" ", "");
            groupJSON.securityEnabled = false;
            groupJSON.mailEnabled     = true;
            groupJSON.description     = "Property Group";
            groupJSON.groupTypes      = new JArray("Unified");
            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, string.Format("{0}groups", AADAppSettings.GraphResourceUrl));

            message.Content = new StringContent(groupJSON.ToString(), System.Text.Encoding.UTF8, "application/json");
            message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", graphAccessToken);
            using (HttpClient client = new HttpClient())
            {
                var responseMessage = await client.SendAsync(message);

                if (responseMessage.StatusCode != System.Net.HttpStatusCode.Created)
                {
                    throw new System.Web.Http.HttpResponseException(responseMessage.StatusCode);
                }

                var payload = await responseMessage.Content.ReadAsStringAsync();

                groupId = JObject.Parse(payload)["id"].ToString();
            }
            var group = await graphService.Groups[groupId].Request().GetAsync() as Graph.Group;

            // Add users to Office 365 Group
            var groupMembers  = new HashSet <Graph.User>(members);
            var groupOwners   = new HashSet <Graph.User>();
            var adminUserName = _currentUserName;
            var queryAdmins   = (await graphService.Users.Request().Filter(string.Format("mail eq '{0}'", adminUserName)).GetAsync()).CurrentPage;
            var admin         = queryAdmins.Count > 0 ? queryAdmins[0] : null;

            if (admin != null)
            {
                groupMembers.Add(admin);
                groupOwners.Add(admin);
            }
            if (propertyOwner != null)
            {
                groupMembers.Add(propertyOwner);
                groupOwners.Add(propertyOwner);
            }

            foreach (var user in groupMembers.OfType <Graph.User>())
            {
                try
                {
                    await graphService.Groups[group.Id].Members.References.Request().AddAsync(user);
                }
                catch { }
            }

            foreach (var user in groupOwners.OfType <Graph.User>())
            {
                try
                {
                    await graphService.Groups[group.Id].Owners.References.Request().AddAsync(user);
                }
                catch { }
            }

            return(group);
        }
示例#28
0
 /// <summary>
 /// Adds the specified Group to the collection via POST.
 /// </summary>
 /// <param name="group">The Group to add.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param>
 /// <returns>The created Group.</returns>
 public System.Threading.Tasks.Task <Group> AddAsync(Group group, CancellationToken cancellationToken)
 {
     this.ContentType = "application/json";
     this.Method      = "POST";
     return(this.SendAsync <Group>(group, cancellationToken));
 }
示例#29
0
        private async Task UploadExcelToGroupOneDriveAsync(GraphServiceClient graphService, string graphAccessToken, Graph.Group group, string excelPath)
        {
            string excelPathName = "Property-Costs.xlsx";

            string[] excelPathArry = excelPath.Split(new char[] { '\\' });
            if (excelPathArry.Length > 0)
            {
                excelPathName = excelPathArry[excelPathArry.Length - 1];
            }
            DriveItem driveItem = new DriveItem();

            driveItem.Name = excelPathName;
            driveItem.File = new Microsoft.Graph.File();
            driveItem      = await graphService.Groups[group.Id].Drive.Root.Children.Request().AddAsync(driveItem);
            using (FileStream file = new FileStream(_baseFolderPath + excelPath, FileMode.Open, FileAccess.Read))
            {
                await graphService.Groups[group.Id].Drive.Items[driveItem.Id].Content.Request().PutAsync <DriveItem>(file);
            }
        }
        /// <summary>
        /// Creates a new Office 365 Group (i.e. Unified Group) with its backing Modern SharePoint Site
        /// </summary>
        /// <param name="displayName">The Display Name for the Office 365 Group</param>
        /// <param name="description">The Description for the Office 365 Group</param>
        /// <param name="mailNickname">The Mail Nickname for the Office 365 Group</param>
        /// <param name="accessToken">The OAuth 2.0 Access Token to use for invoking the Microsoft Graph</param>
        /// <param name="owners">A list of UPNs for group owners, if any</param>
        /// <param name="members">A list of UPNs for group members, if any</param>
        /// <param name="isPrivate">Defines whether the group will be private or public, optional with default false (i.e. public)</param>
        /// <param name="retryCount">Number of times to retry the request in case of throttling</param>
        /// <param name="delay">Milliseconds to wait before retrying the request. The delay will be increased (doubled) every retry</param>
        /// <returns>The just created Office 365 Group</returns>
        public static UnifiedGroupEntity CreateUnifiedGroup(string displayName, string description, string mailNickname,
                                                            string accessToken, string[] owners = null, string[] members = null,
                                                            bool isPrivate = false, int retryCount = 10, int delay = 500)
        {
            UnifiedGroupEntity result = null;

            if (String.IsNullOrEmpty(displayName))
            {
                throw new ArgumentNullException("displayName");
            }

            if (String.IsNullOrEmpty(description))
            {
                throw new ArgumentNullException("description");
            }

            if (String.IsNullOrEmpty(mailNickname))
            {
                throw new ArgumentNullException("mailNickname");
            }

            if (String.IsNullOrEmpty(accessToken))
            {
                throw new ArgumentNullException("accessToken");
            }

            // Use a synchronous model to invoke the asynchronous process
            result = Task.Run(async() =>
            {
                var group = new UnifiedGroupEntity();

                var graphClient = CreateGraphClient(accessToken, retryCount, delay);

                // Prepare the group resource object
                var newGroup = new Microsoft.Graph.Group
                {
                    DisplayName     = displayName,
                    Description     = description,
                    MailNickname    = mailNickname,
                    MailEnabled     = true,
                    SecurityEnabled = false,
                    GroupTypes      = new List <string> {
                        "Unified"
                    },
                };

                Microsoft.Graph.Group addedGroup = null;
                String modernSiteUrl             = null;

                // Add the group to the collection of groups (if it does not exist
                if (addedGroup == null)
                {
                    addedGroup = await graphClient.Groups.Request().AddAsync(newGroup);

                    // Just to add a short delay :-) ...
                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    if (addedGroup != null)
                    {
                        group.DisplayName  = addedGroup.DisplayName;
                        group.Description  = addedGroup.Description;
                        group.GroupId      = addedGroup.Id;
                        group.Mail         = addedGroup.Mail;
                        group.MailNickname = addedGroup.MailNickname;

                        try
                        {
                            modernSiteUrl = GetUnifiedGroupSiteUrl(addedGroup.Id, accessToken);
                        }
                        catch
                        {
                            // NOOP, we simply need to wakeup the OD4B/Site creation
                        }
                    }
                }

                #region Handle group's owners

                if (owners != null && owners.Length > 0)
                {
                    // For each and every owner
                    foreach (var o in owners)
                    {
                        // Search for the user object
                        var ownerQuery = await graphClient.Users
                                         .Request()
                                         .Filter($"userPrincipalName eq '{o}'")
                                         .GetAsync();

                        var owner = ownerQuery.FirstOrDefault();

                        if (owner != null)
                        {
                            try
                            {
                                // And if any, add it to the collection of group's owners
                                await graphClient.Groups[addedGroup.Id].Owners.References.Request().AddAsync(owner);
                            }
                            catch (ServiceException ex)
                            {
                                if (ex.Error.Code == "Request_BadRequest" &&
                                    ex.Error.Message.Contains("added object references already exist"))
                                {
                                    // Skip any already existing owner
                                }
                                else
                                {
                                    throw ex;
                                }
                            }
                        }
                    }
                }

                #endregion

                #region Handle group's members

                if (members != null && members.Length > 0)
                {
                    // For each and every owner
                    foreach (var m in members)
                    {
                        // Search for the user object
                        var memberQuery = await graphClient.Users
                                          .Request()
                                          .Filter($"userPrincipalName eq '{m}'")
                                          .GetAsync();

                        var member = memberQuery.FirstOrDefault();

                        if (member != null)
                        {
                            try
                            {
                                // And if any, add it to the collection of group's owners
                                await graphClient.Groups[addedGroup.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;
                                }
                            }
                        }
                    }
                }

                #endregion

                int driveRetryCount = 10;

                while (driveRetryCount > 0 && String.IsNullOrEmpty(modernSiteUrl))
                {
                    modernSiteUrl = GetUnifiedGroupSiteUrl(addedGroup.Id, accessToken);

                    // In case of failure retry up to 10 times, with 500ms delay in between
                    if (String.IsNullOrEmpty(modernSiteUrl))
                    {
                        Thread.Sleep(TimeSpan.FromMilliseconds(500));
                        driveRetryCount--;
                    }
                }

                group.SiteUrl = modernSiteUrl;

                return(group);
            }).GetAwaiter().GetResult();

            return(result);
        }
        public async Task<IGroup> CreateUnifiedGroupForPropertyAsync(GraphService graphService, ListItem propertyItem, IEnumerable<Graph.IUser> members)
        {
            var propertyTitle = propertyItem["Title"] as string;
            var propertyOwnerName = propertyItem["sl_owner"] as string;
            var propertyOwner = await graphService.GetFirstUserAsync(i => i.displayName == propertyOwnerName);

            // Create group
            var group = new Graph.Group
            {
                displayName = propertyTitle,
                mailNickname = propertyTitle.Replace(" ", ""),
                securityEnabled = false,
                mailEnabled = true,
                groupType = "Unified",
                description = "Property Group",
            };
            try
            {
                await graphService.groups.AddGroupAsync(group);
            }
            catch { }

            // Add users to group
            var groupMembers = new HashSet<Graph.IUser>(members);
            var groupOwners = new HashSet<Graph.IUser>();
            var adminUserName = HttpContext.Current.User.Identity.Name;
            var admin = await graphService.GetFirstUserAsync(i => i.mail == adminUserName);
            if (admin != null)
            {
                groupMembers.Add(admin);
                groupOwners.Add(admin);
            }
            if (propertyOwner != null)
            {
                groupMembers.Add(propertyOwner);
                groupOwners.Add(propertyOwner);
            }
            
            foreach (var user in groupMembers.OfType<Graph.User>())
            {
                group.members.Add(user);
                try
                {
                    await group.SaveChangesAsync();
                }
                catch { }
            }

            foreach (var user in groupOwners.OfType<Graph.User>())
            {
                group.owners.Add(user);
                try
                {
                    await group.SaveChangesAsync();
                }
                catch { }
            }

            return group;
        }
示例#32
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post")] CreateGroupRequest request, TraceWriter log)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(request.Name))
                {
                    throw new ArgumentException("Parameter cannot be null", "Name");
                }
                if (string.IsNullOrWhiteSpace(request.Description))
                {
                    throw new ArgumentException("Parameter cannot be null", "Description");
                }
                string mailNickName = await GetUniqueMailAlias(request);

                string             displayName = GetDisplayName(request);
                GraphServiceClient client      = ConnectADAL.GetGraphClient(GraphEndpoint.Beta);
                var newGroup = new Group
                {
                    DisplayName     = displayName,
                    Description     = GetDescription(request.Description, 1000),
                    MailNickname    = mailNickName,
                    MailEnabled     = true,
                    SecurityEnabled = false,
                    Visibility      = request.Public ? "Public" : "Private",
                    GroupTypes      = new List <string> {
                        "Unified"
                    },
                    Classification = request.Classification
                };
                var addedGroup = await client.Groups.Request().AddAsync(newGroup);

                var createGroupResponse = new CreateGroupResponse
                {
                    GroupId     = addedGroup.Id,
                    DisplayName = displayName,
                    Mail        = addedGroup.Mail
                };
                try
                {
                    if (!request.AllowToAddGuests)
                    {
                        var groupUnifiedGuestSetting = new GroupSetting()
                        {
                            DisplayName = "Group.Unified.Guest",
                            TemplateId  = "08d542b9-071f-4e16-94b0-74abb372e3d9",
                            Values      = new List <SettingValue> {
                                new SettingValue()
                                {
                                    Name = "AllowToAddGuests", Value = "false"
                                }
                            }
                        };
                        log.Info($"Setting setting in Group.Unified.Guest (08d542b9-071f-4e16-94b0-74abb372e3d9), AllowToAddGuests = false");
                        await client.Groups[addedGroup.Id].Settings.Request().AddAsync(groupUnifiedGuestSetting);
                    }
                }
                catch (Exception e)
                {
                    log.Error($"Error setting AllowToAddGuests for group {addedGroup.Id}: {e.Message }\n\n{e.StackTrace}");
                }
                return(await Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ObjectContent <CreateGroupResponse>(createGroupResponse, new JsonMediaTypeFormatter())
                }));
            }
            catch (Exception e)
            {
                log.Error($"Error:  {e.Message }\n\n{e.StackTrace}");
                return(await Task.FromResult(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)
                {
                    Content = new ObjectContent <string>(e.Message, new JsonMediaTypeFormatter())
                }));
            }
        }