Programmatically create a ContentDatabase C# SP2013
Question
I am searching for a solution of my problem. I banged my head for the last few hours and tried several things and solutions found on google or stack-exchange.
The database should be created via Event Receiver
. I know that elevated prev impersonate as the app pool account which has not enough rights to create a database. But even as a farm admin on my development machine (there is only one account for all services, app pools and so on) I run into an access denied (with the super admin itself :-D )
I don´t know how to go on. Is it possible to SQL authentication and use a SQL user in some way? I tried this way on my development machine with the sa user and the right password BUT access denied.
Due to the fact that this should be a self service solution timer jobs are no solution for me :-( WebService and farm application page are not the best way from my point of view.
Solution
It happens from time to time, in the server OM, there's a check against the context (Web or not) so some operations are forbidden even if the identity running the code has the correct permissions.
You don' want to consider a timer job, but in your case I would think again: with a timer job you'd have no problem at all.
That job could be a work item job, and run every x minutes (even with x=1) to check the queue of work item. If nothing in the queue, it does nothing, thus consummes no resources. If there's a work item, it creates the DB (which is by the way a very long operation, so the x minutes delay the job adds may not be a problem).
I would really consider that approach, since it frees you from permissions/Web contexts problem, avoid potential security holes, and prevent you from running a very long operation from the ER/Web context.
OTHER TIPS
This thing is driving me crazy so I need to ask a few questions.
This is how I "create" the job:
CreateDBWorkItemTimerJob job = new CreateDBWorkItemTimerJob(JobName, webApp);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.Interval = 1;
schedule.EndSecond = 59;
job.Schedule = schedule;
job.Update();
based on that:
public CreateDBWorkItemTimerJob(string name, SPWebApplication webApplication)
: base(name, webApplication, null, SPJobLockType.None)
{
this.Title = "Create new ContentDB for WebApplication";
}
No error but still no job in the job list. What´s wrong? I also tried to use it with a SPOneTimeSchedule - no luck.