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'm encountering some problems while creating programmatically a Site Collection.. I tried in many ways, but without fortune.

First of all, I must say that the account I'm using is:

  • Farm Administrator

  • Site Collection Admin

  • Web App Full Control

The code I'm using is very simple and I check'd if the "Current User" is the one with all those grant (in my case virtualsp\Administrator)

Here is the code:

        string SiteCollectionUrl = "/sites/Prova1/";
        SPWebApplication Application;


        Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
                  {
                      using (SPSite site = new SPSite("http://sp2010dev:44411/"))
                      {
                          using (SPWeb web = site.OpenWeb())
                          {
                              site.AllowUnsafeUpdates = true;
                              web.AllowUnsafeUpdates = true;

                              var newSite = site.WebApplication.Sites.Add(SiteCollectionUrl, "virtualsp\\Administrator", string.Empty, 1033, "STS#0", "virtualsp\\Administrator", "virtualsp\\Administrator", String.Empty);

                          }
                      }
                 });

It generates an exception like "the object is not in a valid state"... What could I do? I tried also impersonating with token other farm admin.. How could I do?

Thank you very much!!

The exception details are:

 at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)
 at Microsoft.SharePoint.Utilities.SPUtility.RedirectToAccessDeniedPage(HttpContext context)
 at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(HttpContext context)
 at Microsoft.SharePoint.Utilities.SPUtility.HandleAccessDenied(Exception ex)
 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)
 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)
 at TT2SP.tt_test.<>c__DisplayClass1b.<TestNuovaSC>b__1a()
 at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
 at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)

(even tring with this code:

 string SiteCollectionUrl = "/sites/Prova2";  

 Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(() => 
 {  
   var Application=SPWebApplication.Lookup(new Uri("http://sp2010dev:44411/"));  
   Application.Sites.Add(SiteCollectionUrl, "virtualsp\\Administrator", string.Empty, 1033, "STS#0", "virtualsp\\Administrator", "virtualsp\\Administrator", String.Empty);  
   }); 

LEVEL UP:

If I use the @Per Jakobsen code in a Console Application, everything works fine... I must understand WHY Console Application works but on WS doesn't... Suggestion?? Thank you very much!

share|improve this question
    
In which context does this code run? Feature receiver, button in the page, console app, etc..? –  Kai Apr 2 '12 at 12:17
    
Inside a WS installed on the _layouts folder.. every other operation goes well.. –  Ziba Leah Apr 2 '12 at 12:27

4 Answers 4

See the @default answer on this thread. It can be problem with access to the Configuration DB.

share|improve this answer
    
The article refers to SP2007 and I cannot make it works on my 2010 installation.. I tried also to add my user as Database Owner on the DB i think are influenced (Content and Configuration) but the problem is always there. I tried also activating the SelfServiceCreateSite but it doesn't work at all.. –  Ziba Leah Apr 2 '12 at 14:38

A couple of remarks:

  • You end the url with / which isn't valid
  • You allocate a lot of objects for no reason and assign a two of AllowUnsafeUpdates to objects not being updated.
  • Are you sure you don't have a half created site collection at the url you specify?

Try something like:

string SiteCollectionUrl = "/sites/Prova2"; 

Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(() =>
    { 
    var Application=SPWebApplication.Lookup(new Uri("http://sp2010dev:44411/")); 
    Application.Sites.Add(SiteCollectionUrl, "virtualsp\\Administrator", string.Empty, 1033, "STS#0", "virtualsp\\Administrator", "virtualsp\\Administrator", String.Empty); 
    });
share|improve this answer
    
Tried and modified the question.. –  Ziba Leah Apr 3 '12 at 8:13

Does the application pool account of the website (where you call the _layouts file) have the rights you mentioned (farm, sitecol, webapp...)?

Calling RunWithElevatedPrivileges make the code run under Application Pool account.

share|improve this answer
    
I just check'd.. When I access the Application.Sites.Add I'm Sharepoint\System.. –  Ziba Leah Apr 3 '12 at 11:54

It has the recommendation to have sitecollections be created through SelfServiceSite creation due security requirements to use the above method. More information on this can be found at this blogpost

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.