<br />
<b>Deprecated</b>:  YoastSEO_Vendor\Symfony\Component\DependencyInjection\Container::__construct(): Implicitly marking parameter $parameterBag as nullable is deprecated, the explicit nullable type must be used instead in <b>/home/nubelus/sharedove/adisjugo/wp-content/plugins/wordpress-seo/vendor_prefixed/symfony/dependency-injection/Container.php</b> on line <b>60</b><br />
<br />
<b>Deprecated</b>:  YoastSEO_Vendor\League\OAuth2\Client\Provider\AbstractProvider::authorize(): Implicitly marking parameter $redirectHandler as nullable is deprecated, the explicit nullable type must be used instead in <b>/home/nubelus/sharedove/adisjugo/wp-content/plugins/wordpress-seo/vendor_prefixed/league/oauth2-client/src/Provider/AbstractProvider.php</b> on line <b>416</b><br />
<br />
<b>Deprecated</b>:  YoastSEO_Vendor\GuzzleHttp\Client::getConfig(): Implicitly marking parameter $option as nullable is deprecated, the explicit nullable type must be used instead in <b>/home/nubelus/sharedove/adisjugo/wp-content/plugins/wordpress-seo/vendor_prefixed/guzzlehttp/guzzle/src/Client.php</b> on line <b>181</b><br />
<br />
<b>Deprecated</b>:  YoastSEO_Vendor\GuzzleHttp\ClientInterface::getConfig(): Implicitly marking parameter $option as nullable is deprecated, the explicit nullable type must be used instead in <b>/home/nubelus/sharedove/adisjugo/wp-content/plugins/wordpress-seo/vendor_prefixed/guzzlehttp/guzzle/src/ClientInterface.php</b> on line <b>77</b><br />
{"id":1119,"date":"2012-07-31T23:27:57","date_gmt":"2012-07-31T21:27:57","guid":{"rendered":"https:\/\/blog.sharedove.com\/adisjugo\/?p=1119"},"modified":"2012-07-31T23:27:57","modified_gmt":"2012-07-31T21:27:57","slug":"creating-site-collections-in-specific-content-database","status":"publish","type":"post","link":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/","title":{"rendered":"Programmatically Creating Content Databases and Site Collections"},"content":{"rendered":"<p>Even if SharePoint 2013 hype is all around us, and we <strong>love<\/strong> to play with SP 2013 \u2013 let\u2019s be realistic, our daily bread is still with our 3 years old friend (or sometimes with even the older ones).<\/p>\n<p>We had a following task (scenario) in the last days: a customer needs automated creation of Site Collections. But, since there is going to be <strong>lot<\/strong> of Site Collections, and since total data volume will go up to 20 TB in first 2-3 years, we needed, because of the SharePoint <a href=\"http:\/\/sharepointgadget.blogspot.de\/2010\/05\/limits-in-sharepoint-2010.html\" target=\"_blank\">limits<\/a>, to find a clever way of creating and adding new content databases on the fly, and creating new site collection in those databases.<\/p>\n<p><!--more--><\/p>\n<p>Basically, we have configured thresholds \u2013 if in the \u201ccurrent\u201d content database there is more than X site collections, or total data volume is over Y bytes, create a new content database, and store all future site collections in that new content database. Until that database also reaches the thresholds, and then\u2026 you get the point. <\/p>\n<p>The following code does the trick. Note: it is recommended to execute it in the farm administrator context.<\/p>\n<h1>Step 1:&#160; get the list of the existing content databases<\/h1>\n<p>&#160;<\/p>\n<pre class=\"brush: csharp; title: Code sample:; notranslate\" title=\"Code sample:\">\n\/\/ get all content databases\nSPContentDatabaseCollection contentDbs = siteCollection.WebApplication.ContentDatabases;\n<\/pre>\n<p>In our case, content databases are all ending with the suffix &quot;XZY\u201d, where XYZ is nothing else but counter. E.g. \u201cMyContentDatabase001\u201d, \u201cMyContentDatabase002\u201d, \u201cMyContentDatabase003\u201d\u2026 etc. We search for the database with the highest index, and check it against (previously configured) thresholds.<\/p>\n<h1>Step 2:&#160; get the status of the \u201clast\u201d (working) content database (db size, number of site collections)<\/h1>\n<p>&#160;<\/p>\n<pre class=\"brush: csharp; title: Code sample:; notranslate\" title=\"Code sample:\">\nbool createNewDb = false;\n\nSPContentDatabase lastDatabase = contentDbs&#x5B;contentDbs.Count-1];\n\nif (\n    lastDatabase.DiskSizeRequired &gt; this.contentDatabaseMaxTresholdSize || \n    lastDatabase.Sites.Count &gt;= lastDatabase.MaximumSiteCount\n    )\n{\n    createNewDb = true;\n}\n<\/pre>\n<p>After we have checked the database, and decided if we need to create a new one for our site collections, let\u2019s do it if we need to:<\/p>\n<h1>Step 3: create a new content database<\/h1>\n<p>&#160;<\/p>\n<pre class=\"brush: csharp; title: Code sample:; notranslate\" title=\"Code sample:\">\nif (createNewDb)\n    contentDatabase = siteCollection.WebApplication.ContentDatabases.Add(\n        serverName, \n        newDbName, \n        username, \n        password, \n        warningSiteCollectionNumber, \n        maximumSiteCollectionsNumber, \n        0);\n    \t\t\t\t\t\t\t\t\t\t\n    contentDatabase.Update();\n    siteCollection.WebApplication.Update();\n}\n<\/pre>\n<p>In the code above, you can leave username and password empty, if you want to use windows authentication. Parameters \u201cwarningSiteCollectionNumber\u201d and \u201cmaximumSiteCollectionNumber\u201d define number of site collections when administrator starts to get the warnings, and the maximal number of the site collections for this content database.<\/p>\n<h1>Step 4: create the site collection in the database<\/h1>\n<p>&#160;<\/p>\n<pre class=\"brush: csharp; title: Code sample:; notranslate\" title=\"Code sample:\">\nnewSite = contentDatabase.Sites.Add(\n    newSiteColectionUrl, \n    siteCollectionTitle, \n    siteCollectionDescription, \n    1033, \n    &quot;MyTemplate#0&quot;, \n    firstAdminLogin, \n    firstAdminName, \n    firstAdminEmail, \n    secondAdminLogin, \n    secondAdminName, \n    secondAdminEmail);\n\n\/\/ Let&#039;s add the upper limit for the site collection\nSPQuota quota = new SPQuota();\nquota.StorageMaximumLevel = maximumBytesForSiteCollection;\nnewSite.Quota = quota;\n\n\/\/ And update the site collection and the content database\nnewSite.RootWeb.Update();\ncontentDatabase.Update();\n<\/pre>\n<p>As opposed to the \u201cusual\u201d way of creating site collections (WebApplication.Sites.Add), it is less known that we can create site collection directly from content database, as in this code sample. Since the content database belongs to a web application, it is obvious that the site collection will belong to that same web application. There are more overloads of this method, for example with only one site collection admin. <\/p>\n<p>And that would be all. New site collection is created in a specific content database. On this way, you can control and govern the growth of your content databases, site collections, and all related parameters.<\/p>\n<blockquote><p>System.InvalidOperationException: Operation is not valid due to the current state of the object<\/p><\/blockquote>\n<p>Oh, yes. We did have another issue. Since this code was executed from a WCF web service, which has it\u2019s own HttpContext (not the SharePoint one!), the code above has crashed each time we did anything with the SPContentDatabase (create, update, whatever\u2026). And if you have HttpContext, SharePoint wants you to have a valid <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.webcontrols.formdigest.aspx\" target=\"_blank\">FormDigest control<\/a>, it\u2019s a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.webcontrols.formdigest.aspx\" target=\"_blank\">security check<\/a>. Which, in the WCF code, we obviously do not have.<\/p>\n<p>So, to simulate the situation that we are outside the Sharepoint context (e.g. in a timer job, or in a console application), which would prevent SharePoint from doing a FormDigest check, just set the following piece of code in your WCF service:<\/p>\n<pre class=\"brush: csharp; title: Code sample:; notranslate\" title=\"Code sample:\">\nvar storeContext = HttpContext.Current;\nHttpContext.Current = null;\n\/\/ do your code\nHttpContext.Current = storeContext;\n<\/pre>\n<div class=\"fb-background-color\">\n\t\t\t  <div \n\t\t\t  \tclass = \"fb-comments\" \n\t\t\t  \tdata-href = \"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/\"\n\t\t\t  \tdata-numposts = \"59\"\n\t\t\t  \tdata-lazy = \"true\"\n\t\t\t\tdata-colorscheme = \"light\"\n\t\t\t\tdata-order-by = \"time\"\n\t\t\t\tdata-mobile=true>\n\t\t\t  <\/div><\/div>\n\t\t  <style>\n\t\t    .fb-background-color {\n\t\t\t\tbackground:  !important;\n\t\t\t}\n\t\t\t.fb_iframe_widget_fluid_desktop iframe {\n\t\t\t    width: 100% !important;\n\t\t\t}\n\t\t  <\/style>\n\t\t  ","protected":false},"excerpt":{"rendered":"<p>Even if SharePoint 2013 hype is all around us, and we love to play with SP 2013 \u2013 let\u2019s be realistic, our daily bread is still with our 3 years old friend (or sometimes with even the older ones). We had a following task (scenario) in the last days: a customer needs automated creation of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[11,12],"tags":[24,54,55],"class_list":["post-1119","post","type-post","status-publish","format-standard","hentry","category-development","category-howto","tag-c","tag-server-object-model","tag-sharepoint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Programmatically Creating Content Databases and Site Collections - Adis Jugo blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Programmatically Creating Content Databases and Site Collections - Adis Jugo blog\" \/>\n<meta property=\"og:description\" content=\"Even if SharePoint 2013 hype is all around us, and we love to play with SP 2013 \u2013 let\u2019s be realistic, our daily bread is still with our 3 years old friend (or sometimes with even the older ones). We had a following task (scenario) in the last days: a customer needs automated creation of [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/\" \/>\n<meta property=\"og:site_name\" content=\"Adis Jugo blog\" \/>\n<meta property=\"article:published_time\" content=\"2012-07-31T21:27:57+00:00\" \/>\n<meta name=\"author\" content=\"adis.jugo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"adis.jugo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/\",\"url\":\"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/\",\"name\":\"Programmatically Creating Content Databases and Site Collections - Adis Jugo blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.sharedove.com\/adisjugo\/#website\"},\"datePublished\":\"2012-07-31T21:27:57+00:00\",\"author\":{\"@id\":\"https:\/\/blog.sharedove.com\/adisjugo\/#\/schema\/person\/a5ca63552094ce9d5a0440f3a1ac9a4c\"},\"breadcrumb\":{\"@id\":\"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.sharedove.com\/adisjugo\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programmatically Creating Content Databases and Site Collections\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.sharedove.com\/adisjugo\/#website\",\"url\":\"https:\/\/blog.sharedove.com\/adisjugo\/\",\"name\":\"Adis Jugo blog\",\"description\":\"The Southern Side\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.sharedove.com\/adisjugo\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.sharedove.com\/adisjugo\/#\/schema\/person\/a5ca63552094ce9d5a0440f3a1ac9a4c\",\"name\":\"adis.jugo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.sharedove.com\/adisjugo\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cc5a23cf1bd0b9d8401c9dd65c6c141041ec0c6e37eedbb511779e4a40a198fd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cc5a23cf1bd0b9d8401c9dd65c6c141041ec0c6e37eedbb511779e4a40a198fd?s=96&d=mm&r=g\",\"caption\":\"adis.jugo\"},\"url\":\"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/author\/adisjugo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Programmatically Creating Content Databases and Site Collections - Adis Jugo blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/","og_locale":"en_US","og_type":"article","og_title":"Programmatically Creating Content Databases and Site Collections - Adis Jugo blog","og_description":"Even if SharePoint 2013 hype is all around us, and we love to play with SP 2013 \u2013 let\u2019s be realistic, our daily bread is still with our 3 years old friend (or sometimes with even the older ones). We had a following task (scenario) in the last days: a customer needs automated creation of [&hellip;]","og_url":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/","og_site_name":"Adis Jugo blog","article_published_time":"2012-07-31T21:27:57+00:00","author":"adis.jugo","twitter_card":"summary_large_image","twitter_misc":{"Written by":"adis.jugo","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/","url":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/","name":"Programmatically Creating Content Databases and Site Collections - Adis Jugo blog","isPartOf":{"@id":"https:\/\/blog.sharedove.com\/adisjugo\/#website"},"datePublished":"2012-07-31T21:27:57+00:00","author":{"@id":"https:\/\/blog.sharedove.com\/adisjugo\/#\/schema\/person\/a5ca63552094ce9d5a0440f3a1ac9a4c"},"breadcrumb":{"@id":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/2012\/07\/31\/creating-site-collections-in-specific-content-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.sharedove.com\/adisjugo\/"},{"@type":"ListItem","position":2,"name":"Programmatically Creating Content Databases and Site Collections"}]},{"@type":"WebSite","@id":"https:\/\/blog.sharedove.com\/adisjugo\/#website","url":"https:\/\/blog.sharedove.com\/adisjugo\/","name":"Adis Jugo blog","description":"The Southern Side","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.sharedove.com\/adisjugo\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.sharedove.com\/adisjugo\/#\/schema\/person\/a5ca63552094ce9d5a0440f3a1ac9a4c","name":"adis.jugo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.sharedove.com\/adisjugo\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cc5a23cf1bd0b9d8401c9dd65c6c141041ec0c6e37eedbb511779e4a40a198fd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cc5a23cf1bd0b9d8401c9dd65c6c141041ec0c6e37eedbb511779e4a40a198fd?s=96&d=mm&r=g","caption":"adis.jugo"},"url":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/author\/adisjugo\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/wp-json\/wp\/v2\/posts\/1119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/wp-json\/wp\/v2\/comments?post=1119"}],"version-history":[{"count":0,"href":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/wp-json\/wp\/v2\/posts\/1119\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/wp-json\/wp\/v2\/media?parent=1119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/wp-json\/wp\/v2\/categories?post=1119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sharedove.com\/adisjugo\/index.php\/wp-json\/wp\/v2\/tags?post=1119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}