0

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!

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

4 Answers 4

2

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

1
  • 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, 2012 at 14:38
1

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); 
    });
1
  • Tried and modified the question..
    – Ziba Leah
    Apr 3, 2012 at 8:13
1

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.

1
  • I just check'd.. When I access the Application.Sites.Add I'm Sharepoint\System..
    – Ziba Leah
    Apr 3, 2012 at 11:54
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.