Create a new site programmatically and select parent site? (SharePoint)

Posted by peter on Stack Overflow See other posts from Stack Overflow or by peter
Published on 2011-01-16T13:49:06Z Indexed on 2011/01/16 13:53 UTC
Read the original article Hit count: 153

Filed under:
|

Hi, I am using the following code to create a new site:

newWeb = SPContext.GetContext(HttpContext.Current).Web.Webs.Add(newSiteUrl, newSiteName, null, (uint)1033, siteTemplate, true, false);

                try
                {
                    newWeb.Update();
                }

NewSiteUrl and newSiteName are values from two textboxes and on whichever site I use this code (in a web part) the new site will be a subsite to this site.

I would now like to be able to select a parent site so that the new site can sit anywhere in the site collection, not just as a subsite to the site where I use the web part.

I created the following function to get all the sites in the site collection and populate a drop down with the name and url for every site

private void getSites()
{
    SPSite oSiteCollection = SPContext.Current.Site;
    SPWebCollection collWebsite = oSiteCollection.AllWebs;

    for (int i = 0; i < collWebsite.Count; i++)
    {
        ddlParentSite.Items.Add(new ListItem(collWebsite[i].Title, collWebsite[i].Url));
    }
    oSiteCollection.Dispose();
}

If the user selects a site in the dropdown, is it possible to use that URL in newSiteUrl so decide where the new site should be? I don't get it to work really and the new site still becomes a subsite to the current one. I guess it has to do with HttpContext.Current? Any ideas on how I should do it instead?

It's the first time I write custom web parts and the sharepoint object model is a bit overwhelming at the moment.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c#

Related posts about sharepoint