Wednesday, June 07, 2006

Tbaginski - HOW TO: Creating SharePoint sites programmatically in MOSS 2007

HOW TO: Creating SharePoint sites programmatically in MOSS 2007



Knowing how to create SharePoint Sites programmatically in MOSS 2007 is a task that developers who work with MOSS 2007 will frequently need to perform in order to automate business processes.


 


Changes associated with programmatic SharePoint site creation in the new version


 


The object model calls to create SharePoint sites programmatically has not changed since the prior version.  However, some post creation tasks developers had to perform in the prior version no longer need to be done!


 


No longer needed: Registering Site Collections and sub sites with the SharePoint Indexing Engine


 


Developers no longer need to register sites with the SharePoint Indexing Service after site creation!  To test this concept, first I created Site Collections and sub sites programmatically in MOSS 2007.  Next, I uploaded documents to document libraries in these Site Collections and sub sites.  Finally, I searched from the home page of the portal for text inside the documents I uploaded and the documents were returned in the search results.  This is a very nice feature and saves us from writing the code to do this in MOSS 2007!


 


No longer needed: Adding Site Collections and sub sites to the Site Directory List


 


I was surprised when I created Site Collections and sub sites programmatically and found them inside the Site Directory list.  Previously, developers had to add Site Collections and sub sites to this list manually after creating them.  This is no longer the case, and once again this saves us the trouble of writing this code in MOSS 2007!


 


New item: Controlling navigation via the SPWeb object!


 


The SharePoint object model now supports customizing the navigation for sites.  For more details on how to do this please see the Programmatically customize site navigation in MOSS 2007 documentation.


 


Site creation basics


 


There are a few different ways to create sites in SharePoint.  You may create Site Collections and sub sites via the object model, and you may also create Site Collections via the SharePoint web service.  A later document will describe how to create SharePoint Site Collections via the SharePoint Web Services.


 


Creating Site Collections via the SharePoint object model


 


Creating a Site Collection is a relatively simple process that only requires a few pieces of information. 


 


First you need to know the Virtual Server the Site Collection will be created on.


 


To programmatically access the list of Virtual Servers on your SharePoint server use the following code:


 


SPGlobalAdmin globalAdmin = new SPGlobalAdmin();


SPVirtualServerCollection virtualServerCollection = new globalAdmin.VirtualServers;


 


Here is a list of the Virtual Servers inside this collection on my development machine.  This list contains the Description, the URL and the Port for each Virtual Server.


 


Default Web Site-Url-http://moss-b2/-Port-80


SharePoint (30250)-Url-http://moss-b2:30250/-Port-30250


SharePoint (80-Url-http://moss-b2/-Port-80


Office Server Web Services-Url-http://moss-b2:56737/-Port-56737


SharePoint Central Administration v3-Url-http://moss-b2:25592/-Port-25592


 


The code to create this list builds upon the code above, and looks like this:


 


foreach (SPVirtualServer vs in VirtualServerCollection)


{


   Console.WriteLine(vs.Description


                     + “-Url-“


                     + vs.Url.ToString()


                     + “-Port-“


                     + vs.Port;


}


 


You will need to determine which Virtual Server in this collection you wish to create your Site Collection upon.  When MOSS 2007 is installed, the Default Web Site is disabled, but it will still appear in this list.  The SharePoint (30250) Virtual Server is the Shared Services web site on my development machine.  The SharePoint (80 Virtual Server is the SharePoint web site.  I made a typo during installation and forgot the trailing ).  The other Virtual Servers are self explanatory based on their titles.


 


*Note:  I tried changing the (80 description in IIS to (80) to correct my type and the SharePoint web site would not load even after an IIS reset.  I haven’t found the table where this information is stored yet, but it must be in the SharePoint database somewhere.


 


Let’s create a new Site Collection called TopLevelSiteCollection on the SharePoint (80 Virtual Server.  To do this we will first create a SPSite object to represent the Virtual Server, like this:


 


SPSite parentSite = newSPSite(“http://moss-b2�);


 


Note:  You can also create site collections on the Shared Services Virtual Server, and I have tested this to verify it works.  To create a Site Collection on the Shared Services Virtual Server on my development machine I used the following line of code to create the SPSite object:


 


SPSite parentSite = newSPSite(“http://moss-b2:30250�);


 


Next, we will create an SPWeb object from the SPSite object and return the collection of sub webs so we can add a new one, like this:


 


SPWebCollection portalSiteWebs = parentSite.AllWebs;


 


Finally, we will call the Add() method of the SPWebCollection to create the new Site Collection.  You will also need to know the following pieces of information that the Add() method of the SPWebCollection object requires.  Here is an explanation of each:


 


strWebUrl: Specifies the unique portion of the URL that identifies the new sub site.


strTitle: Specifies the title of the site.  This appears in the title bar, and the top left portion of the page on the Team Site template.


strDescription: Specifies the description of the sub site


nLCID: The Locale ID for the new sub site.


strWebTemplate: The name of the site definition and configuration the new sub site will be created with.  STS corresponds to the STS site definition and #0 corresponds to configuration 0.  STS#0 collectively represents the Team Site template that SharePoint comes with out of the box.


useUniquePermissions: Specifies if the sub site inherits its security settings from the parents site or not.


bConvertIfThere: If set to true, the existing web folder will be converted to a new sub site.  If set to false, the object model will throw an exception when a web folder with the same name already exists.


 


To create the Site Collection, the code looks like this:


 


SPWeb newSiteCollection = portalSiteWebs.Add(“TopLevelSiteCollection�, “TopLevelSiteCollection “, “This is a programmatically created Site Collection�, 1033, STS#0, false, false);


 


Collectively, the code looks like this to create the Site Collection on the SharePoint (80 Virtual Server:


 


SPSite parentSite = newSPSite(“http://moss-b2�);


SPWebCollection portalSiteWebs = parentSite.AllWebs;


SPWeb newSiteCollection = portalSiteWebs.Add(“TopLevelSiteCollection�, “TopLevelSiteCollection “, “This is a programmatically created Site Collection�, 1033, STS#0, false, false);


 


Here is what the Site Collection looks like after the above code is executed:


 


 


Creating sub sites via the SharePoint object model


 


Creating a sub site is a relatively simple process that only requires a few pieces of information.


 


First, you need to know the URL of the SharePoint site that will be the parent site for the new sub site you wish to create.


 


You will also need to know the information that the Add() method of the SPWebCollection object requires.  See above for a complete explanation of each argument.


 


The following example assumes you have created a Site Collection at the following URL: http://SharePointServerName/SiteDirectory/TopLevelSiteCollection


 


To create a sub site named SubSite under the Site Collection named TopLevelSiteCollection use the following code:


 


SPSite topLevelSiteCollectionSite = new SPSite(“http://SharePointServerName/SiteDirectory/TopLevelSiteCollection�);


SPWeb topLevelSiteCollectionWeb = topLevelSiteCollectionSite.OpenWeb();


topLevelSiteCollectionWeb.AllowUnsafeUpdates = true;


SPWebCollection subSites = topLevelSiteCollectionWeb.Webs;


SPWeb newSubWeb = subSites.Add(“subsite�, “Sub Site�, “This is a programmatically created sub site.�, Convert.ToUInt32(localID), “STS#0�, false, false);


topLevelSiteCollectionWeb.AllowUnsafeUpdates = false;


 


Here is what the new sub site looks like when it is created, along with some markup, to show you where the different arguments of the Add() method are implemented in the sub site.


 


 


Here is a screen shot of the TopLevelSiteCollection Site Collection after the sub site has been created with the code above, along with some markup, to show you where the different arguments of the Add() method are implemented in the parent site.


 


 


Finally, here is a screenshot of the page on the TopLevelSiteCollection Site Collection that lists the all site content, along with some markup, to show you where the different arguments of the Add() method are implemented in the parent site.


 


 


Weird GOTCHA!


 


The following exception is thrown on four different machines I have installed MOSS 2007 Beta 2 on when I create a sub site programmatically, even though the sub site is created successfully.  This exception is slightly different than the one that was thrown by the Beta 1 Technical Refresh code, but you will still need to trap for it at the time being.  Does anyone know what this exception is indicating?


 


This is what I find in the Exception’s StackTrace:


 


at Microsoft.SharePoint.SPSite.get_Features()\r\n   at Microsoft.SharePoint.SPElementProvider.QueryForWorkflowDefinitions(SPWeb web)\r\n   at Microsoft.SharePoint.Workflow.SPWorkflowManager.RegisterFeatureTemplates(SPWorkflowTemplateCollection wftemplates, SPWeb web)\r\n   at Microsoft.SharePoint.SPWeb.get_WorkflowTemplates()\r\n   at Microsoft.SharePoint.Workflow.SPWorkflowAssociation.get_BaseTemplate()\r\n   at Microsoft.SharePoint.SPList.CreateWorkflowStatusColumn(SPWorkflowAssociation wa)\r\n   at Microsoft.SharePoint.Workflow.SPWorkflowAssociationCollection.AddCore(SPWorkflowAssociation wa, Guid id, SPList list, Boolean forceUtilityListCreation)\r\n   at Microsoft.SharePoint.Workflow.SPContentTypeWorkflowAssociationCollection.AddCoreCT(SPWorkflowAssociation associationTemplate)\r\n   at Microsoft.SharePoint.Workflow.SPContentTypeWorkflowAssociationCollection.UpdateOrAdd(SPWorkflowAssociation associationTemplate)\r\n   at Microsoft.SharePoint.SPContentType.CopyWorkflowAssociationsTo(SPContentType ctDst)\r\n   at Microsoft.SharePoint.SPWeb.SyncNewLists()\r\n   at Microsoft.SharePoint.SPWeb.ApplyWebTemplate(String strWebTemplate)\r\n   at Microsoft.SharePoint.SPWeb.CreateWeb(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean bCreateUniqueSubweb, Boolean bConvertIfThere)\r\n   at Microsoft.SharePoint.SPWeb.SPWebCollectionProvider.CreateWeb(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean bCreateUniqueSubweb, Boolean bConvertIfThere)\r\n   at Microsoft.SharePoint.SPWebCollection.Add(String strWebUrl, String strTitle, String strDescription, UInt32 nLCID, String strWebTemplate, Boolean useUniquePermissions, Boolean bConvertIfThere)\r\n"


 


Now you know how to create SharePoint sites via the object model!


 


 


 


 

0 Comments:

Post a Comment

<< Home