Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SharePoint > SharePoint Development
SharePoint Development Programming and development for SharePoint Portal Server and Windows SharePoint Services
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SharePoint Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 17th, 2008, 05:11 AM
Registered User
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?
Old June 17th, 2008, 11:43 AM
Registered User
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts

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;
            SPWebCollection subsites = currentWeb.Webs;

            string url = newUrlText.Text;

               currentWeb.AllowUnsafeUpdates = false;

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add task in Scheduled Task programatically amitjoshipune C# 2 July 31st, 2008 05:42 AM
MaxDataAxisValue programatically ganans Crystal Reports 0 May 6th, 2006 06:30 PM
how to add breadcrumb navigation to a LAMP site avail1now PHP How-To 0 February 17th, 2006 05:03 PM
Using data programatically jasongorringe ASP.NET 2.0 Basics 1 February 10th, 2006 02:42 PM
how do i change the site icon on a geocitiies site uprocker2 HTML Code Clinic 2 March 4th, 2005 07:20 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.