如何在已创建的新 Web 应用程序下创建多个网站集和子网站。共享点 2010

发布于 2024-12-04 02:58:58 字数 107 浏览 0 评论 0原文

我已经创建了一个新的网络应用程序。

现在我只想使用控制台应用程序以编程方式将网站集和子网站添加到 Web 应用程序。

任何帮助表示赞赏。

问候, 纳维什

I have a fresh web application already created.

Now I just want to Programmatically add Site Collections and Subsites to the Web Application using the console application.

Any Help appreciated.

Regards,
Navish

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半岛未凉 2024-12-11 02:58:58

您可以根据需要使用下一类。请对其进行调整并填充异常处理块。

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();
            }
        }
    }
}

You can use next class for your needs. Please adapt it and fill exceptions handling blocks.

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();
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文