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

This problem is somewhat related to this Creating a Site collection programmatically throws exception 0x80070005 question.

I cannot create site collections programmatically in Claims-based webapplications. I have no problem doing so through GUI or PowerShell. I have tried to create sitecollections in several ways. I have tried to impersonate the farm account using WindowsImpersonationContext and I have tried to use SPSecurity.RunWithElevatedPrivileges. The same error happens every time. I have also tried to pass username to the Add-method both with and without the 'Claims-prefix'. Finally, I tried to enable self service site creation and add site collection using sites[0].SelfServiceCreateSite(...), but this wouldn't work either.

I can confirm that it works in classic mode web applications.

I get the same error in ULS, event viewer and while debugging. The error is UnauthorizedAccessException <nativehr>0x80070005</nativehr><nativestack></nativestack> and the stacktrace I get is the following:

at Microsoft.SharePoint.Library.SPRequest.CreateSite(Guid gApplicationId, String bstrUrl, Int32 lZone, Guid gSiteId, Guid gDatabaseId, String bstrDatabaseServer, String bstrDatabaseName, String bstrDatabaseUsername, String bstrDatabasePassword, String bstrTitle, String bstrDescription, UInt32 nLCID, String bstrOwnerLogin, String bstrOwnerUserKey, String bstrOwnerName, String bstrOwnerEmail, String bstrSecondaryContactLogin, String bstrSecondaryContactUserKey, String bstrSecondaryContactName, String bstrSecondaryContactEmail, Boolean bADAccountMode, Boolean bHostHeaderIsSiteName, Int32 iDatabaseVersionMajor, Int32 iDatabaseVersionMinor, Int32 iDatabaseVersionBuild, Int32 iDatabaseVersionRevision) 
at Microsoft.SharePoint.Administration.SPSiteCollection.Add(SPContentDatabase database, SPSiteSubscription siteSubscription, String siteUrl, String title, String description, UInt32 nLCID, String webTemplate, String ownerLogin, String ownerName, String ownerEmail, String secondaryContactLogin, String secondaryContactName, String secondaryContactEmail, String quotaTemplate, String sscRootWebUrl, Boolean useHostHeaderAsSiteName) 
at Microsoft.SharePoint.Administration.SPSiteCollection.Add(SPSiteSubscription siteSubscription, String siteUrl, String title, String description, UInt32 nLCID, String webTemplate, String ownerLogin, String ownerName, String ownerEmail, String secondaryContactLogin, String secondaryContactName, String secondaryContactEmail, Boolean useHostHeaderAsSiteName) 
at Microsoft.SharePoint.Administration.SPSiteCollection.Add(String siteUrl, String title, String description, UInt32 nLCID, String webTemplate, String ownerLogin, String ownerName, String ownerEmail, String secondaryContactLogin, String secondaryContactName, String secondaryContactEmail) 
at customer.Collaboration.Common.Extensions.SPSiteCollectionExtensions.Add(SPSiteCollection sites, String url, String name, String description, String webTemplate, SPUser primaryAdmin, SPUser secondaryAdmin) 
at customer.Collaboration.Provisioning.SPI.EventReceivers.ProjectProvisioningEventReceiver.ProjectProvisioningEventReceiver.ItemAdded(SPItemEventProperties properties)

The relevant code snippet looks like the following:

var currentUser = properties.Web.CurrentUser ?? properties.Web.AllUsers[properties.UserLoginName];
var urlNameOfNewSite = Utility.GetValidUrlOfInputString(string.IsNullOrEmpty(projectUrlName) ? projectName : projectUrlName);
var urlToNewSite = String.Format("{0}/{1}", Constants.ManagedPaths.Projects, urlNameOfNewSite);

//Run site provisioning under the farm account
var impersonation = new Impersonation();
//Hard coding credentials ATM, just to see it work
if (impersonation.ImpersonateValidUser("FARMACCOUNT", "CUSTOMER", "password"))
{
try
{
    using (var topSite = new SPSite(properties.SiteId))
    using (var topWeb = topSite.OpenWeb())
    {
        //Find the right web template
        var templateId = ProvisioningHelper.FindTemplateIdFromTemplatesList(topWeb, chosenTemplateTitle);

        var webApp = topSite.WebApplication;
        var sites = webApp.Sites;

        var newSite = sites.Add(
            urlToNewSite,
            projectName,
            projectObjective,
            1033,
            templateId,
            currentUser.LoginName,
            currentUser.Name,
            currentUser.Email);
share|improve this question

2 Answers 2

customer.Collaboration.Provisioning.SPI.EventReceivers.ProjectProvisioningEventReceiver.ProjectProvisioningEventReceiver.ItemAdded(SPItemEventProperties properties) 

This means that your ItemAdded Receiver for ProjectProvisioning feature fails.

It will report site creation as failed because it rolls back and throws an exception.

Take out all custom features in the template you are trying to apply. My bet is site provisioning will work.

Update with sample :)

Uses self service, but this works running claims+kerberos:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPUtility.ValidateFormDigest();
    using (var site = rootSite.SelfServiceCreateSite(siteCollectionUrl, title, description, locale, null, adminLogin, adminName, adminEmail, secondaryAdminLogin, secondaryAdminName, secondaryAdminEmail))
    {
        var web = site.RootWeb;
        web.AllowUnsafeUpdates = true;

        //Activate web template feature
        site.Features.Add(webTemplateFeatureGuid);

        //Apply web Template
        using (var s = new SPSite(site.ID))
        {
            var webTemplates = s.RootWeb.GetAvailableWebTemplates(locale);
            var webTemplateId = "{" + webTemplateFeatureGuid.ToString() + "}#" + webTemplateName;
            var webTemplate = webTemplates[webTemplateId];
            if (webTemplate != null)
                web.ApplyWebTemplate(webTemplate);
        }

        // need to reload web. Features in webtemplate have modified it!
        web = site.OpenWeb(web.ServerRelativeUrl);

        // Continue doing stuff to new site
    }
});
share|improve this answer
    
It doesn't work with default site templates (e.g. STS#0) either... The loginnames you pass in to the site creation method, are they Claims or stripped? I.e. are they prerfixed with e.g. i:0#.w| ? –  tarjeieo Jan 6 '12 at 8:19
    
Been at home sick, but I guess you have something working then? :), for information, I don't process the claims. –  Kjetil Gullen Jan 12 '12 at 14:58
up vote 1 down vote accepted

"Solved" the problem using Self Service Site Creation, code very similiar to Gullen's above.

share|improve this answer

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.