3

I am trying to create a new site (SPSite), in a SharePoint 2016 portal environment, using the following code:

SPWebApplication webApplication
   = SPWebApplication.Lookup(new System.Uri("http://portal.companyname.com"));
SPSite newSiteCollection
   = webApplication.Sites.Add("/products", "Product Portfolio", "", 1033, 
                              "STS#0", "DOMAINNAME\\SiteAdmin", "Site Admin",
                              "siteadmin@companyname.com");
newSiteCollection.Dispose();

I developed a SharePoint solution that entails a custom master page and CSS to customize the branding of all websites created in the portal. Rather than enabling that feature in the site provisioning code above, I stapled that feature globally to the web application, so all new sites have the feature automatically activated. Somehow, the code fails to create the site and the following exception is thrown.

Exception message: The web being updated was changed by an external process. 
Exception stack trace:
  at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
  at Microsoft.SharePoint.Library.SPRequest.ApplyWebTemplate(String bstrUrl, String bstrWebTemplateContent, Int32 fWebTemplateContentFromSubweb, Int32 fDeleteGlobalListsWithWebTemplateContent, Int32 fIgnoreMissingFeatures, String& bstrWebTemplate, Int32& plWebTemplateId)
  at Microsoft.SharePoint.SPWeb.ProvisionWebTemplate(SPWebTemplate webTemplate, String webTemplateToUse, SPFeatureWebTemplate featureWebTemplate, Page page, SPFeatureDependencyErrorBehavior featureDependencyErrorBehavior, ICollection`1& featureDependencyErrors)
  at Microsoft.SharePoint.SPWeb.ApplyWebTemplate(SPWebTemplate webTemplate, Page page, SPFeatureDependencyErrorBehavior featureDependencyErrorBehavior, ICollection`1& featureDependencyErrors)
  at Microsoft.SharePoint.Administration.SPSiteCollection.AddInternal(SPSiteCollectionAddParameters param)
  at Microsoft.SharePoint.Administration.SPSiteCollection.Add(SPSiteCollectionAddParameters param)

If I deactivate the feature stapler, then the code works just fine and the site collection is created.

What really surprised me is that if the feature stapler is activated and I create the same site via PowerShell (see below), the site collection is provisioned and the branding is applied (as expected).

$template = Get-SPWebTemplate "STS#0"
New-SPSite -Url "http://portal.companyname.com/products" -OwnerAlias "DOMAINNAME\siteadmin" -Template $template

Obviously, the C# code and inner-workings of the New-SPSite command are not equivalent. Is there anything I can do to work around this problem? Is it a SharePoint bug?

Update: 2017-12-10 After a non-stop 8-hour call with Microsoft SharePoint developer support (that had to end mostly because I was exhausted!), the take away was that the SP Server Object Model is not well suited for my use case, i.e. to use the API as described above from a console application running in a particular WFE in a multi-server farm environment. No explanation was provided for why the PowerShell cmdlet works, from that same WFE. The code that actually causes the exception is in the branding feature that is stapled to the creation of an SPWeb using either GLOBAL#0 or STS#1 template:

using (SPWeb web = properties.Feature.Parent as SPWeb)
{
    // code updates the default master page, custom master page
    // and site logo URL properties of the SPWeb
    web.Update(); // <- throws exception
}

I was told that instead of using a command line application, I can consider writing a Windows Service. Or even rewriting the entire command line application with either CSOM (time consuming) or PowerShell (time consuming and mostly impractical since the application is quite large and modular leveraging C# practices).

I was disappointed that Microsoft Support was not willing to do a root cause analysis offline, and were willing to continue working on this case only if I could be on the phone with them non-stop for an indeterminate period of time.

Your Answer

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

Browse other questions tagged or ask your own question.