简体   繁体   English

以C#编程创建内容数据库(SP 2013)

[英]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. 目标是拥有一个Web用户界面,其中包含使用新内容数据库创建新网站集的选项。

With the admin user I can manually in the CA create new Content Databases. 使用admin用户,我可以在CA中手动创建新的内容数据库。 I can also create a new site collection in this content database. 我还可以在此内容数据库中创建新的网站集。

The idea was to create an event receiver (C#). 想法是创建一个事件接收器(C#)。 If the user adds data to a table, the mentioned actions are to be executed. 如果用户将数据添加到表中,则执行上述操作。

Experiments: a) Console application - works! 实验:a)控制台应用程序 - 工作!

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! b)事件接收器 - 仅创建网站集工作,创建内容数据库不起作用! Error: Access Denied. 错误:访问被拒绝。

c) Web Service - Does not work! c)Web服务 - 不起作用! 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? 那么,为什么我在创建网站集时会收到错误Access Denied,但是只创建内容数据库?

Finally I executed PS Script - but this also doesn´t work. 最后我执行了PS脚本 - 但这也行不通。

# 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 . 我在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. 但是,使用提升的权限运行代码是不够的,因为当您以这种方式执行代码时,SharePoint会模拟对应Web应用程序的应用程序池用户。

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). 如果“ http:// sp2013 ”的应用程序池用户没有场管理员权限,则无法使用事件接收器创建内容数据库(可能因此您的事件接收器和Web服务代码存在“拒绝访问”错误)。 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. 您可以创建SharePoint场解决方案,并创建计时器作业,因为通常使用场管理员帐户执行计时器作业。 In this way, you could create a content database. 通过这种方式,您可以创建内容数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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