p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   SharePoint Development (http://p2p.wrox.com/forumdisplay.php?f=213)
-   -   How to add a site programatically (http://p2p.wrox.com/showthread.php?t=68843)

millerthegorilla June 17th, 2008 05:11 AM

How to add a site programatically
 
In sharepoint you can add a site below the current site by clicking on site actions > create > sites and workspaces. I'd like to that programatically in a webpart. What I've done so far is to get the current site (SPSite currentSite = new SPSite(currentUrl);) and then get the webapplication object (SPWebApplication spwa = currentSite.WebApplication;) I then call spwa.sites.add with the appropriate parameters. However, sharepoint throws an exception that the site already exists if I give it the url of the current site plus the new site url. For instance, if the current sites url is 'http://server/personal/admin' and I want to create a site with the url of '/subsite' I've been trying to call spwa.sites.add with the url parameter set to 'http://server/personal/admin/subsite'. This is when sharepoint complains. The only way I've been able to create a site is to pass in 'sites/subsite' as the url parameter of spwa.sites.add. How do I create a site as a subsite of the current site then?

millerthegorilla June 17th, 2008 11:43 AM

Somebody answered this question on another forum. It works like this:
            SPSite currentSite = new SPSite(currentUrl);
            SPWeb currentWeb = currentSite.OpenWeb();
            SPUser currentUser = currentWeb.CurrentUser;

            currentWeb.AllowUnsafeUpdates = true;
            currentWeb.Update();
            SPWebCollection subsites = currentWeb.Webs;

            string url = newUrlText.Text;

           try
           {
               subsites.Add(url,
                            newSiteTitleText.Text,
                            siteDescriptionText.Text,
                            (uint)(1033),
                            siteTemplateList.SelectedItem.Value,
                            true,
                            false);
....
           finally
           {
               currentWeb.AllowUnsafeUpdates = false;
               currentWeb.Update();


All times are GMT -4. The time now is 07:02 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.