Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

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/

share|improve this answer
    
Creating a site collection while specifying a pre-mounted content database appears to be something you can only do with PowerShell. –  Andrew Oct 20 '11 at 8:08
add comment

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@x.com");

There are also many 6 other overloads of that function

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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