sharepoint3.0 通过编程的方法创建子网站

本文介绍如何使用MOSS2007的对象模型进行站点及子站点的程序化创建,并提供了详细的步骤说明与示例代码。此外,还讲解了如何将新创建的站点添加到站点目录中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上找到的文章,暂时来不及翻译,请见谅。
This article has been updated based on feedback I received from a developer on the MS SharePoint team. 
 
First change:  What I was previously referring to as a Site Collection is now referred to as a top level site in MOSS - this top level site is technically an SPWeb object, but I chose to call this a top level site because this article focuses on creating sites and sub sites, all of which are of the type SPWeb.
 
Second change:  MOSS does several things when you create a site progammatically, however MOSS does not add the newly created site to the actual Sites list in the SiteDirectory site.  I have documented all the things MOSS does when you programmatically create a site and I have also added the code you will need to add sites to the Sites list in the SiteDirectory list.
 
All screenshots have been updated.
 
Thanks for your feedback anonymous MS SharePoint developer!
 
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!
 

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 sites and sub sites via the object model, and you may also create sites via the SharePoint web service. A later document will describe how to create SharePoint sites via the SharePoint Web Services.
 
Creating Sites via the SharePoint object model
 
Creating a site is a relatively simple process that only requires a few pieces of information. 
 
First you need to know the Virtual Server the site 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 called TopLevelSite 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 sites on the Shared Services Virtual Server, and I have tested this to verify it works. To create a site 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. 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, the code looks like this:
 
SPWeb newSite = portalSiteWebs.Add(“TopLevelSite”, “TopLevelSite“, “This is a programmatically created site.”, 1033, STS#0, false, false);
 
Collectively, the code looks like this to create the site on the SharePoint (80 Virtual Server:
 
SPSite parentSite = newSPSite(“ http://moss-b2”);
SPWebCollection portalSiteWebs = parentSite.AllWebs;
SPWeb newSite = portalSiteWebs.Add(“TopLevelSite”, “TopLevelSite“, “This is a programmatically created site.”, 1033, STS#0, false, false);
 
Here is what the site looks like after the above code is executed:
 
0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}">
 
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 at the following URL: http://SharePointServerName/SiteDirectory/TopLevelSite
 
To create a sub site named SubSite under the site named TopLevelSite use the following code:
 
SPSite topLevelSite = new SPSite(“ http://SharePointServerName/SiteDirectory/TopLevelSite”);
SPWeb topLevelSiteWeb = topLevelSite.OpenWeb();
topLevelSiteWeb.AllowUnsafeUpdates = true;
SPWebCollection subSites = topLevelSiteWeb.Webs;
SPWeb newSubWeb = subSites.Add(“subsite”, “Sub Site”, “This is a programmatically created sub site.”, Convert.ToUInt32(localID), “STS#0”, false, false);
topLevelSiteWeb.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.
 
0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}">
 
Here is a screen shot of the TopLevelSite 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.
 
0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}">
 
Finally, here is a screenshot of the page on the TopLevelSite that lists the all sub sites, along with some markup, to show you where the different arguments of the Add() method are implemented in the parent site.
 
0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}">
  
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!
 
Adding newly created sites to the Site Directory List
 
When you create a new site programatically in SharePoint the following things are done for you automatically. 
 
1. The top level sites are added to the Sites top navigation menu bar.
2. The top level sites appear in the Site Map, see the screenshot below:
 
0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}">
 
3. The top level sites appear in the QuickLaunch menu for the Sites web. 
4. The top level sites appear in the All Site Content list for the Sites web, see screenshot below:
 
0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}">
 
However, sites are not automatically added to the Sites list in the Sites web.  To add a site to the Sites list, use the following code:
 
SPSite portalSite = new SPSite(" http://SharePointServerName");
SPWeb siteDirectory = portalSite.AllWebs["SiteDirectory"];
SPList siteDirectoryList = siteDirectory.Lists["Sites"];
// Create new item in the list
SPListItem item = siteDirectoryList.Items.Add();
// Set default and required values
item["Title"] = newWeb.Title;
item["URL"] = newWeb.Url;
item["Description"] = newWeb.Description;
//Save the list item to the database
item.Update();
 
Note:  This has changed ever so slightly from the last version of SharePoint.  In the prior version of SharePoint the name of the column for Title was SiteTitle and the name of the column for URL was SiteURL.
 
Once you execute the code above you will see the site listed in the Sites list in the Sites web, like this:
 
0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}">
 
Note:  Here you will see the site's approval status is set to Pending.  I tried a few different things to set this column to approved but had no luck.  If you know the proper way to set the site's status to approved via code please drop me a comment and let me know so I can enhance this article to include that step.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值