我已经创建了一个新的 Web 应用程序。
现在我只想使用控制台应用程序以编程方式将站点集合和子站点添加到 Web 应用程序。
任何帮助表示赞赏。
问候, 纳维什
我已经创建了一个新的 Web 应用程序。
现在我只想使用控制台应用程序以编程方式将站点集合和子站点添加到 Web 应用程序。
任何帮助表示赞赏。
问候, 纳维什
您可以根据需要使用下一堂课。请调整它并填充异常处理块。
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace TestApp
{
/// <summary>
/// Allows to create site collections and web sites.
/// </summary>
public class SiteCreation
{
/// <summary>
/// Creates new site collection.
/// </summary>
/// <param name="webApplicationsUrl">Web application url, which will host new site collection.</param>
/// <param name="siteUrl">Created site collection URL.</param>
/// <param name="siteTitle">Created site collection title.</param>
/// <param name="siteDescription">Created site collection description.</param>
/// <param name="siteTemplateName">Template name (STS#0 for example).</param>
/// <param name="lcid">LCID (1033 for English).</param>
/// <param name="ownerLogin">Owner login name.</param>
/// <returns>
/// Created site collection.
/// </returns>
public SPSite CreateSiteUnderWebApplication(string webApplicationsUrl, string siteUrl, string siteTitle, string siteDescription, string siteTemplateName, uint lcid, string ownerLogin)
{
var webApp = SPWebApplication.Lookup(new Uri(webApplicationsUrl));
if (webApp != null)
{
using (var site = webApp.Sites[siteUrl])
{
if (site == null)
{
try
{
var siteNew = webApp.Sites.Add(siteUrl, siteTitle, siteDescription, lcid, siteTemplateName, ownerLogin, ownerLogin, string.Empty);
return siteNew;
}
catch (Exception exc)
{
//error can't create site
//if (Properties.Settings.Default.logErrorMessages)
// eventLog.WriteEntry(exc.Message, EventLogEntryType.Error);
return null;
}
}
else
{
// warning site already exist
//if (Properties.Settings.Default.logWarningMessages)
// eventLog.WriteEntry("Site: " + strSiteUrl + " under webapp: " + strWebApplicationsUrl + " alredy exist.", EventLogEntryType.Warning);
return null;
}
}
}
else
{
// error can't access webapp
//if (Properties.Settings.Default.logErrorMessages)
// eventLog.WriteEntry("Can't access " + strWebApplicationsUrl + " webapp", EventLogEntryType.Error);
return null;
}
}
/// <summary>
/// Create web site under site collection.
/// </summary>
/// <param name="siteCollectionUrl">URL of parent site collection.</param>
/// <param name="webSiteUrl">Url of created web site.</param>
/// <param name="webSiteTitle">Title of created web site.</param>
/// <param name="webSiteDescription">Description of created web site.</param>
/// <param name="webSiteTemplateName">Name of web site template.</param>
/// <param name="lcid">LCID of web site.</param>
/// <param name="isCustomTemplate">True if webSiteTemplateName is name of custom template.</param>
/// <returns>Created web site.</returns>
public SPWeb CreateWebSiteUnderSiteCollection(string siteCollectionUrl, string webSiteUrl, string webSiteTitle, string webSiteDescription, string webSiteTemplateName, uint lcid, bool isCustomTemplate)
{
SPSite siteCollection = null;
SPWeb web = null;
try
{
siteCollection = new SPSite(siteCollectionUrl);
web = siteCollection.AllWebs[webSiteUrl];
if (web.Exists)
{
// warning site already exist
//if (Properties.Settings.Default.logWarningMessages)
// eventLog.WriteEntry("Site: " + strSiteUrl + " under site collection: " + strSiteCollectionUrl + " alredy exist.", EventLogEntryType.Warning);
return null;
}
else
{
web.Dispose();
web = siteCollection.OpenWeb();
siteCollection.AllowUnsafeUpdates = true;
SPWeb webNew = null;
if (isCustomTemplate)
{
var siteTemplate = siteCollection.GetCustomWebTemplates(lcid)[webSiteTemplateName];
webNew = web.Webs.Add(webSiteUrl, webSiteTitle, webSiteDescription, lcid, siteTemplate, false, false);
}
else
{
webNew = web.Webs.Add(webSiteUrl, webSiteTitle, webSiteDescription, lcid, webSiteTemplateName, false, false);
}
siteCollection.AllowUnsafeUpdates = false;
return webNew;
}
}
catch (Exception exc)
{
//error can't create site
//if (Properties.Settings.Default.logErrorMessages)
// eventLog.WriteEntry(exc.Message, EventLogEntryType.Error);
return null;
}
finally
{
if (web != null)
web.Dispose();
if (siteCollection != null)
siteCollection.Dispose();
}
}
}
}