2

I'd like to create a site collection from a custom application page in central admin. The administrator needs the option to specify an existing, mounted content database to store the new site collection. I'm running SharePoint Foundation 2010.

Creating a site collection with PowerShell allows an existing mounted content database to be specified, but none of the SPSiteCollection.Add() object model overloads allow the same. I can create a new database, but not specify an existing database.

Browsing the Microsoft.SharePoint.PowerShell assembly with Reflector shows the New-SPSite cmdlet uses an additional, internally scoped SPSiteCollection.Add() overload that includes a content database parameter. I assume the Microsoft.SharePoint.PowerShell assembly is listed in an InternalsVisibleToAttribute in the Microsoft.SharePoint assembly, allowing PowerShell access to the internal function while remaining hidden to my code.

I'm aware that I can execute PowerShell scripts from C#, is there any reason not to do this by calling the New-SPSite cmdlet from code? Is there any other method I've missed?

2 Answers 2

3

PowerShell is just a wrapper for the object model. You shouldn't need to reference PowerShell in your feature, just create the new site collection with C# inside your feature. There's a blog post here that illustrates some code that may get you started. http://blog.mastykarz.nl/programmatically-creating-sites-site-collections-custom-web-template/

1
  • Creating a site collection while specifying a pre-mounted content database appears to be something you can only do with PowerShell.
    – Andrew
    Commented Oct 20, 2011 at 8:08
1

This can be done by adding the new SPSite using the SPContentDatabase object instead of the SPWebApplication object.

 SPContentDatabase myDB = ......
 myDb.Sites.Add("/Sites/Name", "domain\login","[email protected]");

There are also many 6 other overloads of that function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.