locked
Issue while Adding Tenant admin to a sharepoint site in office 365 using CSOM. RRS feed

  • Question

  • we have a requirement for which we are creating an automated process to get site owners/ members of site owner group using azure function with Tenant Admin Creds. However there are few confidential sites where a tenant admin that doesnt have access. The plan is to add the tenant admin to the site collection as site admin get the site owner info and then remove the permission of the tenant admin from these site. I found a piece of code from Link to add tenant admin as in the site collection.

    using (ClientContext clientContext = new ClientContext("https://testtenant-admin.sharepoint.com"))
                {
                    clientContext.Credentials = new SharePointOnlineCredentials(userMail, password);
                    var tenant = new Tenant(clientContext);
                    List<string> siteCollList = new List<string>();
                    int startIndex = 0;
                    SPOSitePropertiesEnumerable siteProperties;
                    do
                    {
                        //Get urls of site collections in the tenant in batches of 300 (Does not include the OneDrive for Business sites)
                        siteProperties = tenant.GetSiteProperties(startIndex, false);
                        clientContext.Load(siteProperties, siteProps => siteProps.Include(site => site.Url));
                        clientContext.ExecuteQuery();
                        
                        //Iterate the site collectio urls
                        foreach (var siteProperty in siteProperties)
                        {
                            
                            try
                            {
                                siteCollList.Add(siteProperty.Url);
                                if (siteProperty.Url.Contains(@"https://testtenant.sharepoint.com/sites/GetSiteOwnerSite"))
                                {
                                    //assign the specified user (current user in this case) as the site collection admin. 
                                    tenant.SetSiteAdmin(siteProperty.Url, "amteam@testtenant.com", true);
                                    
    
                                    clientContext.ExecuteQuery();
    
                                    System.Console.WriteLine(siteProperty.Url);
                                }
                            }
                            catch (Exception ex)
                            {
                                System.Console.WriteLine("Error on: " + siteProperty.Url + " " + ex.Message);
                            }
                        }
    
                        startIndex += 300;
    
                    } while (siteProperties.Count >= 300);
                }

    The issue that I am facing with this is that siteProperties = tenant.GetSiteProperties(startIndex, false) is only getting me the classic sites and not the modern sites for some reason and hence I am not able to add tenant admin. 

    Is this an expected behavior. what can i do to add tenant admin to any site collection even if the tenant admin doesnt have permission to that specific site. With UI it is possible to add tenant admin to a site collection via admin centre. 

    Monday, October 26, 2020 11:38 AM

All replies

  • Hi Pushpanjay Umrao,

    The SharePoint Server products and SharePoint Online services forums have transitioned to Microsoft Q&A, 
    https://docs.microsoft.com/en-us/answers/products/m365

    I am writing this reply to inform you that I have created a question in the Q&A for your issue here.

    Now an answer has been posted on that thread. You could go have a check and communicate with community memebers there.

    Best Regards,

    Michael Han


    "SharePoint" forums will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !

    Wednesday, October 28, 2020 9:44 AM