Issue while adding Tenant admin to a sharepoint site in office 365 using CSOM.

Pushpanjay Umrao 161 Reputation points
2020-10-26T11:37:14.78+00:00

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.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,809 questions
0 comments No comments
{count} votes

Accepted answer
  1. Baker Kong-MSFT 3,801 Reputation points
    2020-10-27T01:55:12.573+00:00

    Hi @Pushpanjay Umrao ,

    I reviewed the issue description, the core question is how to list all sites? Right? If so, please take a reference of the below method:

    In the tenant admin site, there is a hidden list named DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS that store a copy of aggregated site collections data from all contentdb.

    You can get it like below:

    var siteurl = "https://tenant-admin.sharepoint.com"  // Tenant site, not regular site!   
    List AllSiteList = clientContext.Web.Lists.GetByTitle("DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS");  
    ListItemCollection AllSiteItems = AllSiteList.GetItems(CamlQuery.CreateAllItemsQuery());  
                  
    clientContext.Load(AllSiteItems);  
    clientContext.ExecuteQuery();  
    

    The corresponding rest api is:

    • /_api/web/lists/getbytitle('DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS')/items

    Best Regards,
    Baker Kong


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Nilesh Bibhuti 1 Reputation point
    2020-10-26T22:38:15.603+00:00

    Try to use this namespace : Microsoft.Online.SharePoint.TenantAdministration and use this method : tenant.GetSitePropertiesFromSharePoint

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.