I want to add 'Administrator'(Site Collection Administrator) to site collection owners for all users' personal sites(OneDrive for Business sites) programmatically(preferably using REST API) in Office365 .

How to do this ?

Any help would be appreciated.


1 Answer 1


Recently my research on OD4B have taken me to Add a Site Collection administrator to all Site Collections in a Tenant by Vardhaman Deshpande, which gives example on how to add site collection admin to OD4B Sites

1) Get the account names of the users in a tenant using People Search. Bear in mind that search has a limitation of returning a maximum of 500 rows on SharePoint Online. That will be 500 users in our case. So if your tenant as more than 500 users, you will need to call search in batches of 500 to get the account names of all the users.

2) Once we have the account names of the users, we need to get the url of their OneDrive for Business sites. We can do this by querying the CSOM UserProfile API

3) After getting the OneDrive for Business site urls, all we need to do is use the SetSiteAdmin function.

using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search.Query;
using Microsoft.SharePoint.Client.UserProfiles;
using System;
using System.Collections.Generic;
using System.Security;

namespace CSOM.Console.Test
    class Program
        static void Main(string[] args)
            //Tenant Details
            string tenantAdminUrl = "https://tenantName-admin.sharepoint.com/";
            string tenantAdminLoginName = "[email protected]";
            string tenantAdminPassword = "password";
            string mySiteHostUrl = "https://tenantName-my.sharepoint.com";

            using (ClientContext clientContext = new ClientContext(tenantAdminUrl))
                var passWord = new SecureString();
                foreach (char c in tenantAdminPassword.ToCharArray()) passWord.AppendChar(c);
                clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, passWord);

                //Get accountnames of users in the tenant
                var accountNames = GetUserAccountNames(clientContext);

                //Get OD4B site urls
                var OD4BSites = GetUserOD4BSites(mySiteHostUrl, clientContext, accountNames);

                //Set User as Site collection admin on the OD4B sites.
                SetAdminOnOD4BSites(clientContext, OD4BSites);


        private static List<string> GetUserAccountNames(ClientContext clientContext)
            //Use search to get the AccountNames of users in a tenant. 
            var keywordQuery = new KeywordQuery(clientContext);
            keywordQuery.QueryText = "*";
            keywordQuery.SourceId = new Guid("b09a7990-05ea-4af9-81ef-edfab16c4e31");
            keywordQuery.BypassResultTypes = true;
            var searchExecutor = new SearchExecutor(clientContext);
            var searchResults = searchExecutor.ExecuteQuery(keywordQuery);
            var accountNames = new List<string>();
            if (searchResults.Value.Count > 0 && searchResults.Value[0].RowCount > 0)
                foreach (var resultRow in searchResults.Value[0].ResultRows)


            return accountNames;

        private static List<string> GetUserOD4BSites(string mySiteHostUrl, ClientContext clientContext, List<string> accountNames)
            var OD4BSites = new List<string>();

            foreach (var accountName in accountNames)
                // Get the people manager instance
                var peopleManager = new PeopleManager(clientContext);

                // Get the OneDrive for Business Site Url
                var OD4BSite = peopleManager.GetUserProfilePropertyFor(accountName, "PersonalSpace");


                OD4BSites.Add(mySiteHostUrl + OD4BSite.Value);

            return OD4BSites;

        private static void SetAdminOnOD4BSites(ClientContext clientContext, List<string> OD4BSites)
            var tenant = new Tenant(clientContext);

            //Get login name of the current user
            var currentUser = clientContext.Web.CurrentUser;
            clientContext.Load(currentUser, u => u.LoginName);


            //Iterate the site collectio urls
            foreach (var OD4BSite in OD4BSites)
                    //assign the specified user (current user in this case) as the tenant admin. 
                    tenant.SetSiteAdmin(OD4BSite, currentUser.LoginName, true);
                    //Set the last parameter to false if you want to remove the user from the site collection admins


                catch (Exception ex)

                    System.Console.WriteLine("Error on: " + OD4BSite + " " + ex.Message);

If you are inclined to use Rest, then refer Frank Marasco's: So you want to programmatically provision Personal Sites (One Drive for Business) in Office 365? on msdn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.