Esempio n. 1
0
        /// <summary>
        /// Removes an administrators from the site collection
        /// </summary>
        /// <param name="web">Site to operate on</param>
        /// <param name="admin"><see cref="OfficeDevPnP.Core.Entities.UserEntity"/> that describes the admin to be removed</param>
        public static void RemoveAdministrator(this Web web, UserEntity admin)
        {
            var users = web.SiteUsers;
            web.Context.Load(users);
            web.Context.ExecuteQueryRetry();

            var adminToRemove = users.FirstOrDefault(u => String.Equals(u.LoginName, admin.LoginName, StringComparison.CurrentCultureIgnoreCase));
            if (adminToRemove != null && adminToRemove.IsSiteAdmin)
            {
                adminToRemove.IsSiteAdmin = false;
                adminToRemove.Update();
                web.Context.ExecuteQueryRetry();
            }

        }
        protected override void ExecuteCmdlet()
        {
            Tenant.SetSiteProperties(Url, title: Title, sharingCapability: Sharing, storageMaximumLevel: StorageMaximumLevel, allowSelfServiceUpgrade: AllowSelfServiceUpgrade, userCodeMaximumLevel: UserCodeMaximumLevel, userCodeWarningLevel: UserCodeWarningLevel);

            if (Owners != null && Owners.Count > 0)
            {
                var admins = new List<UserEntity>();
                foreach (string owner in Owners)
                {
                    var userEntity = new UserEntity { LoginName = owner };
                    admins.Add(userEntity);
                }
                Tenant.AddAdministrators(admins, new Uri(Url));
            }
        }
        public void AddAdministratorsTest()
        {
            // Difficult to test on a developer (MSDN) tenant, as there is only one user allowed.
            using (var clientContext = TestCommon.CreateClientContext())
            {
                // Count admins
                var initialCount = clientContext.Web.GetAdministrators().Count;
                var userEntity = new UserEntity() { LoginName = _userLogin, Email = _userLogin };
                clientContext.Web.AddAdministrators(new List<UserEntity>() { userEntity }, false);

                var newCount = clientContext.Web.GetAdministrators().Count;
                Assert.IsTrue(initialCount == newCount); // Assumes that we're on a dev tenant, and that the existing sitecol admin is the same as the user being added.

                clientContext.Web.RemoveAdministrator(userEntity);
            }
        }
Esempio n. 4
0
        public void AddAdministratorsTest()
        {
            // Difficult to test on a developer (MSDN) tenant, as there is only one user allowed.
            using (ClientContext clientContext = TestCommon.CreateClientContext())
            {
                // Count admins
                int initialCount = clientContext.Web.GetAdministrators().Count;
                #if !CLIENTSDKV15
                var userEntity = new UserEntity {LoginName = _userLogin, Email = _userLogin};
                #else
                var userEntity = new UserEntity { LoginName = _userLogin };
                #endif
                clientContext.Web.AddAdministrators(new List<UserEntity> {userEntity}, false);

                List<UserEntity> admins = clientContext.Web.GetAdministrators();
                bool found = false;
                foreach(var admin in admins) 
                {                    
                    string adminLoginName = admin.LoginName;
                    String[] parts = adminLoginName.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

                    if (parts.Length > 1)
                    {
                        adminLoginName = parts[2];
                    }
                    
                    if (adminLoginName.Equals(_userLogin, StringComparison.InvariantCultureIgnoreCase))
                    {
                        found = true;
                        break;
                    }
                }
                Assert.IsTrue(found);

                // Assumes that we're on a dev tenant, and that the existing sitecol admin is the same as the user being added.
                clientContext.Web.RemoveAdministrator(userEntity);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Removes an administrators from the site collection
        /// </summary>
        /// <param name="web">Site to operate on</param>
        /// <param name="admin"><see cref="OfficeDevPnP.Core.Entities.UserEntity"/> that describes the admin to be removed</param>
        public static void RemoveAdministrator(this Web web, UserEntity admin)
        {
            var users = web.SiteUsers;
            web.Context.Load(users);
            web.Context.ExecuteQuery();

            var adminToRemove = users.Where(u => u.LoginName == admin.LoginName).FirstOrDefault();
            if (adminToRemove != null && adminToRemove.IsSiteAdmin)
            {
                adminToRemove.IsSiteAdmin = false;
                adminToRemove.Update();
                web.Context.ExecuteQuery();
            }

        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            // Office 365 Multi-tenant sample
            // For a console application you need to create a context: this can be done by creating a context based on a user and password. 
            // Using this approach you can add administrators to site collections for which the user used to create the context is already an 
            // admin.
            Console.Write("Tenant name (e.g. ams if your SharePoint url is https://");
            ConsoleWriteColor("ams", ConsoleColor.Green);
            Console.WriteLine(".sharepoint.com) :");
            string tenantName = Console.ReadLine();

            Console.Write("Name of site collection to target permissions (siteName)\r\n(https://" + tenantName + ".sharepoint.com/sites/");
            ConsoleWriteColor("<siteName>", ConsoleColor.Green);
            Console.WriteLine(") :");
            string siteName = Console.ReadLine();
            
            Console.Write("Tenant admin name (");
            ConsoleWriteColor("<tenantAdminName>", ConsoleColor.Green);
            Console.WriteLine("@" + tenantName + ".onmicrosoft.com) :");
            string tenantAdminUserName = Console.ReadLine();

            Console.WriteLine("Create SharePoint ClientContext object for the web");
            string tenantAdminPassword = GetPassword();
            AuthenticationManager authManager = new AuthenticationManager();

            string targetSiteUrl = String.Format("https://{0}.sharepoint.com/sites/{1}", tenantName, siteName);
            string tenantAdmin = String.Format("{0}@{1}.onmicrosoft.com", tenantAdminUserName, tenantName);
            ClientContext cc = authManager.GetSharePointOnlineAuthenticatedContextTenant(targetSiteUrl, tenantAdmin, tenantAdminPassword);
            
            // Alternative approach is via an AppOnly app that has been registered via AppRegNew/AppInv. Good thing with this approach is
            // that you registered app can have tenant level permissions which makes that you can use below code to for example set the 
            // additional site collection administrators to site collections where you today are not listed as site collection admin. A 
            // typical example would be adding additional admins to OneDrive site collections to enable eDiscovery
            //ClientContext cc = authManager.GetAppOnlyAuthenticatedContext("https://tenantname-my.sharepoint.com/personal/user2", "<your tenant realm>", "<appID>", "<appsecret>");

            // Tenant admin site context
            Console.WriteLine("Create SharePoint ClientContext object for the tenant administration web");
            string tenantAdminUrl = String.Format("https://{0}-admin.sharepoint.com/", tenantName);
            ClientContext ccTenant = authManager.GetSharePointOnlineAuthenticatedContextTenant(tenantAdminUrl, tenantAdmin, tenantAdminPassword);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("----------------------------------------------------------------------");
            Console.WriteLine("Admins for site collection {0}:", targetSiteUrl);
            // Get a list of current admins
            List<UserEntity> admins = cc.Web.GetAdministrators();
            foreach (var admin in admins)
            {
                Console.WriteLine("{0} ({1})", admin.Title, admin.LoginName);
            }

            Console.WriteLine("----------------------------------------------------------------------");
            Console.WriteLine("Add administrators to the current site collection:");

            // Prepare a list of admins to add: below sample shows how to do this for Office 365 Multi-Tenant
            // NOTE: This is a sample of the code that must be implemented and WILL NOT EXECUTE since the user

            List<UserEntity> adminsToAdd = new List<UserEntity>();
            adminsToAdd.Add(new UserEntity() { LoginName = SAMPLE_USER_ACCOUNTNAME });

            cc.Web.AddAdministrators(adminsToAdd);
            foreach (var admin in adminsToAdd)
            {
                Console.WriteLine("Add: {0}", admin.LoginName);
            }

            Console.WriteLine("----------------------------------------------------------------------");
            Console.WriteLine("Remove administrators from the current site collection:");
            UserEntity adminToRemove = new UserEntity() { LoginName = SAMPLE_USER_ACCOUNTNAME };
            cc.Web.RemoveAdministrator(adminToRemove);
            Console.WriteLine("Removed: {0}", adminToRemove.LoginName);

            Console.WriteLine("----------------------------------------------------------------------");
            Console.WriteLine("External sharing settings for current site collection:");
            Console.WriteLine(ccTenant.Web.GetSharingCapabilitiesTenant(new Uri(targetSiteUrl)));

            Console.WriteLine("----------------------------------------------------------------------");
            Console.WriteLine("External users for current site collection:");
            List<ExternalUserEntity> externalUsers = ccTenant.Web.GetExternalUsersForSiteTenant(new Uri(targetSiteUrl));
            //List<ExternalUserEntity> externalUsers = ccTenant.Web.GetExternalUsersTenant();"
            foreach (var externalUser in externalUsers)
            {
                Console.WriteLine("{0} ({1})", externalUser.DisplayName, externalUser.AcceptedAs);
            }

            Console.WriteLine("Press enter to continue...");
            Console.ReadLine();
        }