Unable to provision modern site using provider hosted app in SharePoint 2019

Brass Contributor

We are trying to create a modern site from the provider hosted app in SharePoint 2019.
We have tried creating a modern site using SharePoint in-built(team "STS#3" and communication "SITEPAGEPUBLISHING#0") site collection template, but it creates a classic site. Also, we have tried creating classic team/communication site and added a modern page in that and saved that site as a template. Taking base as that template, we tried creating a modern site, but here also it creates a classic site only.

Environment

[x] SharePoint 2019

Build Name: SharePoint Server 2019 RTM

CU Version: 16.0.10337.12109

Expected or Desired Behavior

We have tried creating a modern site using SharePoint in-built(team "STS#3" and communication "SITEPAGEPUBLISHING#0") site collection template, It should create a modern site.

Observed Behavior

We have tried creating a modern site using SharePoint in-built(team "STS#3" and communication "SITEPAGEPUBLISHING#0") site collection template, but it creates a classic site.

Steps to Reproduce

We are using the below project Reference link for creating this app: https://github.com/SharePoint/PnP/tree/master/Samples/Provisioning.SiteCol.OnPrem

Source code for the code snippet
 private string CreateSiteCollection(string hostWebUrl, string url, string template, string title, string adminAccount)
        {
            // Resolve root site collection URL from host web. We assume that this has been set as the "TenantAdminSite"
            string rootSiteUrl = hostWebUrl.Substring(0, 8 + hostWebUrl.Substring(8).IndexOf("/"));

            //Resolve URL for the new site collection
            var webUrl = string.Format("{0}/sites/{1}", rootSiteUrl, url);

            // Notice that this assumes that AdministrationSiteType as been set as TenantAdministration for root site collection
            // If this tenant admin URI is pointing to site collection which is host named site collection, code does create host named site collection as well
            var tenantAdminUri = new Uri(rootSiteUrl);
            string realm = TokenHelper.GetRealmFromTargetUrl(tenantAdminUri);
            var token = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, tenantAdminUri.Authority, realm).AccessToken;
            using (var adminContext = TokenHelper.GetClientContextWithAccessToken(tenantAdminUri.ToString(), token))
            {
                var tenant = new Tenant(adminContext);
                var properties = new SiteCreationProperties()
                {
                    Url = webUrl,
                    Owner = adminAccount,
                    Title = title,
                    Template = template
                };

                //start the SPO operation to create the site
                SpoOperation op = tenant.CreateSite(properties);
                adminContext.Load(op, i => i.IsComplete);
                adminContext.ExecuteQuery();
            }

            // Set theme for the new site
            SetThemeToNewSite(webUrl);

            // Return URL for redirection
            return webUrl;
        }
6 Replies

@ArvindKumar Mepaniwhat version of Microsoft.Online.SharePoint.Client.Tenant.dll are you referencing in your project?

 

I've just created a simple Console Application to see if I was able to create both a modern team site and a communication site collection in my environment and it gives me the modern sites as expected. I'm basically using the same code as yours except that I don't call SetThemeToNewSite in the end. Could it be this method is causing your site to revert back to classic experience somehow?

 

I'm using version 16.1.9021.1200 (latest version of Microsoft.SharePointOnline.CSOM NuGet package). 

Thanks for your help, @Paul Pascha 

The version of Microsoft.Online.SharePoint.Client.Tenant.dll is 16.1.9021.1200.

I am also able to create a modern site collection using a console app. But when I tried creating modern site collection with the same code using provider hosted app, it always creates classic site only.

I have removed SetThemeToNewSite method as well.

Weird!

The Console App I wrote is technically a Provider-hosted add-in (using app only authentication) so I can't really see the difference compared to a typical web-based Provider-hosted add-in.

I've checked the CU level of my Farm which is slightly higher than yours, (May 2019 CU).

 

Hi @Paul Pascha ,

We have updated the CU version (May 2019 CU) but still we are not able to create modern site collection using provider hosted app.

Note: we are not using App only authentication. We have added GetS2SAccessTokenWithWindowsIdentity for the authentication as per our requirement.

We are using following code snippet is for authentication:

TokenHelper.GetS2SAccessTokenWithWindowsIdentity(tenantAdminUri, HttpContext.Current.User.Identity as WindowsIdentity);

Is it possible for you to share your full app? I can then try installing and running on my environment to see what happens

@Paul Pascha Hello, can you please tell me if provider hosted app can be created using modern team or communication sites?

Also, can we select ASP.Net MVC from visual studio ?