0

I'm trying to create a new site collection using below code:

SPWebApplication webApp = new SPSite(webAppURL).WebApplication;
                    SPSiteCollection siteCollections = webApp.Sites;

                    if (!SPSite.Exists(new Uri(webAppURL + siteURL)))
                    {
                        var storeContext = HttpContext.Current;
                        HttpContext.Current = null;

                        SPSite newSiteCollection = siteCollections.Add(siteURL,
                        projectCode,
                        projectTitle,
                        CommonConstants.LCID,
                        CommonConstants.SiteTemplate,
                        primaryUserName,
                        string.Empty,
                        string.Empty,
                        secondaryUserName,
                        string.Empty,
                        string.Empty,
                        dbServer,
                        projectCode,
                        dbUserName,
                        dbPassword);

                        SPContentDatabase contentDatabase = newSiteCollection.ContentDatabase;
                        contentDatabase.MaximumSiteCount = 1;
                        contentDatabase.WarningSiteCount = 0;
                        contentDatabase.Update();

                        HttpContext.Current = storeContext;
                    return true;
                    }

If I run the Visual studio it is working fine. However, If I'm creating a build and hosting it in IIS, the content database is created successfully but cannot create site collection and throwing below error:

enter image description here

Application Pool Identity is the farm account.

Farm Configurations:

  • SharePoint 2019
  • SQL Server 2017

Any help is much appreciated!

1

According to your source code you are creating a site collection then updating the related content database with a maximum site collection with one.

That piece of code might work only one time for one web application if before you are not creating a new content database or incrementing the MaximumSiteCount by 1.

So, before calling the SPSiteCollection.Add method, ensure that you have at least one content database available for a new one into your WebApplication otherwise you will have that kind of issue again.

In the past I had that requirement, one content database per site collection. The way was managing first the content database creation then creating the site collection inside by using the content database as main reference instead of the WebApplication.

SPContentDatabase.Sites.Add

  • Even after following above approach, I'm encountering same error while hosting it on IIS but works fine if we run VS on debug mode – Sohail Shaikh Nov 19 '19 at 7:46
  • 1
    Did you run your code into SPSecurity.RunWithElevatedPriviledge? By the way, avoid instanciate an SPSite to retrieve your WebApplication, use SPWebApplication.Lookup static method. – Vladimir Herrlein Nov 19 '19 at 8:11
  • Adding "SPSecurity.RunWithElevatedPriviledge" fixed it. – Sohail Shaikh Nov 19 '19 at 8:44
  • Great news, enjoy your SP development :) – Vladimir Herrlein Nov 19 '19 at 8:47

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.