I've moved my blog to!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Thursday, January 31, 2008

Create Content Database

It's pretty easy to create a content database via the browser and you can create a new content database easily enough when creating a new site collection via stsadm, createsiteinnewdb, or directly using addcontentdb. But what if you need to script the creation of a new content database without creating a site collection and you need to set some of the properties found via the browser that addcontentdb doesn't provide? To do this I created a new command which basically does the same as addcontentdb but adds the additional properties found within central admin: gl-createcontentdb. To accomplish this I first do some checks to make sure that you're not trying to create a database using a name that already exists. I then use the SPWebApplication's ContentDatabases property to add the new database using the parameters provided. Once the database has been created we can set the search service settings:

   1: public override int Run(string command, StringDictionary keyValues, out string output)
   2: {
   3:  output = string.Empty;
   5:  InitParameters(keyValues);
   7:  string dbserver = Params["dbserver"].Value;
   8:  string dbname = Params["dbname"].Value;
  10:  if (string.IsNullOrEmpty(dbserver))
  11:  {
  12:   dbserver = SPWebService.ContentService.DefaultDatabaseInstance.NormalizedDataSource;
  13:  }
  15:  using (SPSite site = new SPSite(Params["webapp"].Value))
  16:  {
  17:   SPContentDatabase db = null;
  18:   foreach (SPContentDatabase tempDB in site.WebApplication.ContentDatabases)
  19:   {
  20:    if (tempDB.Name.ToLower() == dbname.ToLower())
  21:    {
  22:     db = tempDB;
  23:     break;
  24:    }
  25:   }
  26:   if (db != null)
  27:    throw new Exception("Content database already exists.");
  29:   SPObjectStatus status = (SPObjectStatus)Enum.Parse(typeof(SPObjectStatus), Params["status"].Value, true);
  31:   db = site.WebApplication.ContentDatabases.Add(dbserver, dbname, null, null, 
  32:    int.Parse(Params["warningsitecount"].Value),
  33:    int.Parse(Params["maxsites"].Value), (status == SPObjectStatus.Online?0:1));
  35:   if (Params["searchserver"].UserTypedIn && !string.IsNullOrEmpty(Params["searchserver"].Value))
  36:   {
  37:    // If they specified a search server then we need to try and find a valid
  38:    // matching search server using the server address property.
  39:    SPSearchService service = SPFarm.Local.Services.GetValue<SPSearchService>("SPSearch");
  40:    SPServiceInstance searchServiceServer = null;
  41:    foreach (SPServiceInstance tempsvc in service.Instances)
  42:    {
  43:     if (!(tempsvc is SPSearchServiceInstance))
  44:      continue;
  46:     if (tempsvc.Status != SPObjectStatus.Online)
  47:      continue;
  49:     if (tempsvc.Server.Address.ToLowerInvariant() == Params["searchserver"].Value.ToLowerInvariant())
  50:     {
  51:      // We found a match so bug out of the loop.
  52:      searchServiceServer = tempsvc;
  53:      break;
  54:     }
  55:    }
  56:    if (searchServiceServer != null)
  57:    {
  58:     db.SearchServiceInstance = searchServiceServer;
  59:    }
  60:    else
  61:     throw new Exception("Search server not found.");
  62:   }
  63:   else if (Params["searchserver"].UserTypedIn)
  64:   {
  65:    // The user specified the searchserver switch with no value which is what we use to indicate
  66:    // clearing the value.
  67:    db.SearchServiceInstance = null;
  68:   }
  70:   db.Update();
  71:  }
  73:  return 1;
  74: }

The syntax of the command can be seen below:

C:\>stsadm -help gl-createcontentdb

stsadm -o gl-createcontentdb

Creates a new content database.

        -dbname <content database name>
        -webapp <web application url>
        [-dbserver <content database server>]
        [-maxsites <<maximum number of sites allowed in the db (default is 15000)>]
        [-warningsitecount <number of sites before a warning event is generated (default is 9000)>]
        [-searchserver <search server (leave empty to clear the search server)>]
        [-dbuser <database username (if using SQL Authentication and not Windows Authentication)>]
        [-dbuser <database username (if using SQL Authentication and not Windows Authentication)>]
        [-dbpwd <database password (if using SQL Authentication and not Windows Authentication)>]
        [-status <online | disabled>]
Here's an example of how to create a new content database:
stsadm -o gl-createcontentdb -dbname "SharePoint_ContentDB1" -webapp "http://intranet" -maxsites 500 -warningsitecount 400


Nikhil Joshi said...

Thank you sir. This post help me a lot. Please keep writing.

Nikhil Joshi

Anonymous said...

Hi Garry,

how can we crate a content datbase only using object model...

I am getting the error of while updating

Object reference not set to an instance......

if you can, pl do let me know