I am trying to iterate over all site collections in a SharePoint Online Tenant.

I found this code on the web and adapted it to my needs:

        context = SharePointContextProvider.Current.GetSharePointContext(Context);
        ClientContext con = context.CreateUserClientContextForSPAppWeb();

        SPOSitePropertiesEnumerable spp = null;
        Microsoft.Online.SharePoint.TenantAdministration.Tenant tenant = new Tenant(con);
        int startIndex = 0;

        string sites = String.Empty;
        while (spp == null || spp.Count > 0)
            spp = tenant.GetSiteProperties(startIndex, true);

            foreach (SiteProperties sp in spp)


But I keep getting the following error:

Current site is not a tenant administration site.

I am using the following library for accessing the tenants: C:\Program Files\SharePoint Client Components\Assemblies\Microsoft.Online.SharePoint.Client.Tenant.dll which has Version 15.

What am I doing wrong?


The ClientContext have to be on the tenant-admin.sharepoint.com site. Right now your context is in the AppWeb.

ClientContext con = new ClientContext(new Uri(<url to tenant admin site>));
  • Hi, thanks for this tip. The problem is that i do not want to enter/code the credentials. How can this be done? – AdelPoint Mar 2 '15 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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