I'm trying programmatically to create a site from a web part, sandbox solution for SharePoint Online 2010. My web part has a text box which displays error messages and a button Create, in which on click event creates a site.
If the parameters of my method are
web.Webs.Add("TestAccount", "TestAccount", String.Empty, web.Language, SPWebTemplate.WebTemplateSTS, false, **true**);
Whether the template is my custom template or SharePoint site template (Team Site), I get an error:
"Cannot convert a folder if a web template is specified."
If the parameters of my method are
web.Webs.Add("TestAccount", "TestAccount", String.Empty, web.Language, SPWebTemplate.WebTemplateSTS, false, **false**);
I get an error:
"The web site address is already in use."
Even though there is no site in the workspaces under TestAccount name and it has not been created.
using (SPSite site = SPContext.Current.Site)
{
using (SPWeb web = SPContext.Current.Web)
{
webTemplates = site.RootWeb.GetAvailableWebTemplates(1033);
foreach (SPWebTemplate webTemplate in webTemplates)
{
if (webTemplate.Title == "Engagement Site")
{
myTemplate = webTemplate;
}
}
bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
web.AllowUnsafeUpdates = true;
accountSite = web.Webs.Add("TestAccount", "TestAccount", String.Empty, web.Language, SPWebTemplate.WebTemplateSTS, false, true);
accountSite.Update();
web.AllowUnsafeUpdates = allowUnsafeUpdates;
}
}