以编程方式创建Web应用程序和子站点

时间:2023-01-19 23:35:05

I am using the following code to programatically create a web application & sub site,

我使用以下代码以编程方式创建Web应用程序和子站点,

            try
            {

                SPWebApplicationBuilder webAppBuilder = new SPWebApplicationBuilder(SPFarm.Local);

                SPWebApplication newApplication;
                int myPort = 3333;
                webAppBuilder.Port = myPort; //Port No.
                webAppBuilder.RootDirectory = new System.IO.DirectoryInfo("C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\" + myPort);
                webAppBuilder.CreateNewDatabase = true; // Create new database

                webAppBuilder.DatabaseName = "WSS_Content_372772890a5a4a04abb460cd9ea3bd22";    // Database name
                webAppBuilder.DatabaseServer = webAppBuilder.DefaultZoneUri.Host;  // DatabaseServer Host name/computer name          

                webAppBuilder.UseNTLMExclusively = true;  // Use NTLM authentication

                newApplication = webAppBuilder.Create(); // Create new web application
                newApplication.Provision();           // Provision it into web farm           

                using (var mySiteCollection = newApplication.Sites.Add("/", "Site 3", "Site 3 Decscription", 1033, "STS#1", "In-Wai-Svr2\\Administrator", "Administrator", "tjagtap@waiin.com"))
                {
                    using (var rootWeb = mySiteCollection.RootWeb)
                    {                        
                        // Code customizing root site goes here                        

                        using (var subSite = rootWeb.Webs.Add("Site3.1", "Site 3.1", "Site 3.1 Description", 1033, "STS#1", true, false))
                        {                            
                            // Code customizing sub site goes here
                        }                        
                    }
                }
            }
            catch(Exception ex)
            {
                ;
            }        

It works fine i.e. it creates the web application on the specified port with sub site but this sub site will not be shown under Sites menu of Quick launch bar also it will not be shown as a separate tab on the Home Page. Is it possible to do it programatically?

它工作正常,即它在具有子站点的指定端口上创建Web应用程序,但此子站点不会显示在快速启动栏的“站点”菜单下,也不会在主页上显示为单独的选项卡。是否有可能以编程方式进行?

2 个解决方案

#1


"web application but the sub site does not appear as a separate tab on the home page"

“Web应用程序,但子站点不会在主页上显示为单独的选项卡”

I think that what you are trying to acomplish is to create a new website, inside the same application, for that you can't create a new application because it will create a new pool of sites. You have to create a new "sub"website inside the same application your home site is.

我认为你想要实现的是在同一个应用程序中创建一个新网站,因为它无法创建新的应用程序,因为它将创建一个新的网站池。您必须在您的主站点所在的同一应用程序中创建一个新的“子”网站。

hope this helps: example

希望这会有所帮助:例子

#2


SharepointSiteService.Sites siteWS = new SharepointSiteService.Sites();
                                siteWS.Credentials = new System.Net.NetworkCredential("user1", "password", "domain");
try 
{

                                            SharepointSiteService.Template[] templates;
                                            siteWS.GetSiteTemplates(1033, out templates);
                                            SharepointSiteService.Template template = (from SharepointSiteService.Template t
                                            in templates
                                                                                       where t.Title == "Test Template"
                                                                                       select t).FirstOrDefault();

siteWS.CreateWeb("<parent web name>" + "/" + <sub web name>" + "/" + <sub web name>", "Test web", "Test Web", template.Name, 1033, true, 1033, true, 1033, true, false, false, false, false, false, false); 
}
catch (Microsoft.SharePoint.SoapServer.SoapServerException e)
{
                                        System.Diagnostics.EventLog.WriteEntry("SharePoint Foundation", "soap exception" + e.Detail + e.Message + e.Source + e.Node);
}
catch (Exception ex)
{
                                        System.Diagnostics.EventLog.WriteEntry("SharePoint Foundation", "Site Created");
                                        System.Diagnostics.EventLog.WriteEntry("SharePoint Foundation", ex.Message + ex.Source + ex.StackTrace);
 }`enter code here`

#1


"web application but the sub site does not appear as a separate tab on the home page"

“Web应用程序,但子站点不会在主页上显示为单独的选项卡”

I think that what you are trying to acomplish is to create a new website, inside the same application, for that you can't create a new application because it will create a new pool of sites. You have to create a new "sub"website inside the same application your home site is.

我认为你想要实现的是在同一个应用程序中创建一个新网站,因为它无法创建新的应用程序,因为它将创建一个新的网站池。您必须在您的主站点所在的同一应用程序中创建一个新的“子”网站。

hope this helps: example

希望这会有所帮助:例子

#2


SharepointSiteService.Sites siteWS = new SharepointSiteService.Sites();
                                siteWS.Credentials = new System.Net.NetworkCredential("user1", "password", "domain");
try 
{

                                            SharepointSiteService.Template[] templates;
                                            siteWS.GetSiteTemplates(1033, out templates);
                                            SharepointSiteService.Template template = (from SharepointSiteService.Template t
                                            in templates
                                                                                       where t.Title == "Test Template"
                                                                                       select t).FirstOrDefault();

siteWS.CreateWeb("<parent web name>" + "/" + <sub web name>" + "/" + <sub web name>", "Test web", "Test Web", template.Name, 1033, true, 1033, true, 1033, true, false, false, false, false, false, false); 
}
catch (Microsoft.SharePoint.SoapServer.SoapServerException e)
{
                                        System.Diagnostics.EventLog.WriteEntry("SharePoint Foundation", "soap exception" + e.Detail + e.Message + e.Source + e.Node);
}
catch (Exception ex)
{
                                        System.Diagnostics.EventLog.WriteEntry("SharePoint Foundation", "Site Created");
                                        System.Diagnostics.EventLog.WriteEntry("SharePoint Foundation", ex.Message + ex.Source + ex.StackTrace);
 }`enter code here`