简体   繁体   中英

Programmatically create content database in C# (SP 2013)

The goal is to have a web user interface with the option to create new site collections with new Content Database.

With the admin user I can manually in the CA create new Content Databases. I can also create a new site collection in this content database.

The idea was to create an event receiver (C#). If the user adds data to a table, the mentioned actions are to be executed.

Experiments: a) Console application - works!

using (SPSite site = new SPSite("http://sp2013")) {
  using (SPWeb spWeb = site.OpenWeb()) {
      SPWebApplication elevatedWebApp = spWeb.Site.WebApplication;
        elevatedWebApp.ContentDatabases.Add("sp2013", "WSS_Content_80_" + DateTime.Now.ToString("yyyymmddhhMMss"), null, null, 10, 15, 0);
    }
}

b) Event Receiver - Only create site collections works, creation of content databases does not work! Error: Access Denied.

c) Web Service - Does not work! Error: Access Denied.

So, why do I get the error Access Denied when I can create site collections, but only content databases creation not go?

Finally I executed PS Script - but this also doesn´t work.

# AUTHOR: Paul Kotlyar
# CONTACT: unclepaul84@gmail.com
# DESCRIPTION: sets an option on content web service that allows updating of SP Administration objects such as SPJobDefinition from content web applications
function Set-RemoteAdministratorAccessDenied-False()
{
    # load sharepoint api libs
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") > $null

  # get content web service
 $contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
  # turn off remote administration security
 $contentService.RemoteAdministratorAccessDenied = $false
  # update the web service
 $contentService.Update()
}

Maybe somebody knows a solution?

I asked the same question in Stackexchange .

This is a permissions problem, but not for clients account. The client user is irrelevant. The problem is that for content database creation your code need farm administrator privileges. However, running code with elevated privileges is insufficient, because when you execute code in this way, SharePoint impersonate with application pool user of correspondent web application.

If the app pool user for " http://sp2013 " do not have farm admin privileges, you cannot create content database with an event receiver (maybe for this reason you have an Access Denied error for your event receiver and your webservice code). The problem is giving farm admin privileges to an app pool or service user it's a very bad idea.

I recommend to you implement this solution as timer job. You can create a SharePoint farm solution, and make a timer job, because normally timer jobs are executed with a farm admin account. In this way, you could create a content database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM