title: Create a new site programmatically and select parent site? (SharePoint)
body: <p>I am using the following code to create a new site:</p>
  
  <pre><code>newWeb = SPContext.GetContext(HttpContext.Current).Web.Webs.Add(newSiteUrl, newSiteName, null, (uint)1033, siteTemplate, true, false);
  
                  try
                  {
                      newWeb.Update();
                  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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</p>
  
  <pre><code>private void getSites()
  {
      SPSite oSiteCollection = SPContext.Current.Site;
      SPWebCollection collWebsite = oSiteCollection.AllWebs;
  
      for (int i = 0; i &lt; collWebsite.Count; i++)
      {
          ddlParentSite.Items.Add(new ListItem(collWebsite[i].Title, collWebsite[i].Url));
      }
      oSiteCollection.Dispose();
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>It's the first time I write custom web parts and the sharepoint object model is a bit overwhelming at the moment.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Edit: with updated code
  gives error: <code>Trying to use an SPWeb object that has been closed or disposed and is no longer valid.</code> </p>
  
  <pre><code>if (!siteExists(newSiteName) &amp;&amp; newSiteName.Length &gt; 0)
              {
                  using (var parent = SPContext.GetContext(HttpContext.Current).Site)
                  { 
                      using(var parentWeb = parent.OpenWeb(new Guid(parentSite)))
                      {
                      newWeb = parentWeb.Webs.Add(newSiteUrl, newSiteName, null, (uint)1033, siteTemplate, true, false);
  
                  try
                  {
                      newWeb.Update();
                  }
  
                  catch
                  {
                      lblErrorCreateSite.Text = "An error occured when trying to create a new site, please try again.";
                  }
                  finally
                  {
  
                      txtSiteName.Text = "";
  
                      // Show link to new site
                      lblNewSite.Text = "A new site was successfully created at ";
                      hplNewSite.Visible = true;
                      hplNewSite.NavigateUrl = siteURL() + newSiteName;
                      hplNewSite.Text = newSiteName;
  
                      // Dispose to reload the SharePoint content database
                      newWeb.Dispose();
  
                  }
  
                  // Set permissions
                  try
                  {
                      string site = siteURL();
                      SPSite spSite = new SPSite(site + newSiteName);
                      SPWeb web = spSite.OpenWeb();
  
                      // Assign Full Access role to the selected groups
                      string fullAccessGroup = null;
                      string fullAccessRole = null;
                      foreach (ListItem item in lbFullAccess.Items)
                      {
                          fullAccessGroup = item.Value;
                          fullAccessRole = "Full Control";
                          SPRoleAssignment roleAssignment = new SPRoleAssignment(web.SiteGroups[fullAccessGroup]);
                          SPRoleDefinitionBindingCollection roleDefinition = roleAssignment.RoleDefinitionBindings;
  
                          roleDefinition.Add(web.RoleDefinitions[fullAccessRole]);
                          web.RoleAssignments.Add(roleAssignment);
                          web.Properties[fullAccessGroup] = fullAccessRole;
                          web.Properties.Update();
                      }
  
                      // Assign Contributor role to the selected groups
                      string contributeGroup = null;
                      string contributeRole = null;
                      foreach (ListItem item in lbContributor.Items)
                      {
                          contributeGroup = item.Value.ToString();
                          contributeRole = "Contribute";
  
                          SPRoleAssignment roleAssignment = new SPRoleAssignment(web.SiteGroups[contributeGroup]);
                          SPRoleDefinitionBindingCollection roleDefinition = roleAssignment.RoleDefinitionBindings;
  
                          roleDefinition.Add(web.RoleDefinitions[contributeRole]);
                          web.RoleAssignments.Add(roleAssignment);
                          web.Properties[contributeGroup] = contributeRole;
                          web.Properties.Update();
                      }
  
                      // Assign Reader role to the selected groups
                      string readerGroup = null;
                      string readerRole = null;
                      foreach (ListItem item in lbReadOnly.Items)
                      {
                          readerGroup = item.Value.ToString();
                          readerRole = "Read";
  
                          SPRoleAssignment roleAssignment = new SPRoleAssignment(web.SiteGroups[readerGroup]);
                          SPRoleDefinitionBindingCollection roleDefinition = roleAssignment.RoleDefinitionBindings;
  
                          roleDefinition.Add(web.RoleDefinitions[readerRole]);
                          web.RoleAssignments.Add(roleAssignment);
                          web.Properties[readerGroup] = readerRole;
                          web.Properties.Update();
                      }
  
                  }
                  catch
                  {
                      lblErrorSetPermissions.Text = "Error trying to set permissions for this site, please try again.";
                  }
                  finally
                  {
  
                  }
  
              }
                  }
              }
              else
              {
                  if (siteExists(newSiteName))
                  {
                      lblErrorCreateSite.Text = "A site with that name already exists. Please select another name.&lt;br/&gt;";
                  }
  
                  if (newSiteName.Length == 0)
                  {
                      lblErrorCreateSite.Text = "A Site Name is required.&lt;br/&gt;";
                  }
  
                  hplNewSite.Visible = false;
              }
  </code></pre>
  
  <p>Edit2:
  So I use</p>
  
  <pre><code>SPSite currentContext = SPContext.GetContext(HttpContext.Current).Site;
  SPWeb parentID = currentContext.OpenWeb(new Guid(parentSiteValue));
  
  newWeb = parentID.Webs.Add(newSiteUrl, newSiteName, null, (uint)1033, siteTemplate, true, false);
  </code></pre>
  
  <p>But how can I easiest get the url for the newly created site (to display correct url in the link I create and to use when I set permissions)?</p>