Let's cut to the chase. Here's the code below:
/// <summary>
/// Creates the child site.
/// </summary>
/// <param name="parentSiteUrl">The parent site URL.</param>
/// <param name="adminUsername">The admin username.</param>
/// <param name="adminPassword">The admin password.</param>
/// <param name="newWebsiteInfo">The new website information.</param>
/// <returns>a <see cref="bool"/> indicating if the site was created with success or not</returns>
bool CreateChildSite(string parentSiteUrl, string adminUsername, string adminPassword, WebCreationInformation newWebsiteInfo )
{
// by default assume the function will fail. Just to be on the safe side.
bool output = false;
try
{
// create a clientContext from the provided parent site
// and get the collection of all the webs below the parent site
ClientContext clientContext = new ClientContext(parentSiteUrl);
WebCollection currentListOfWebs = clientContext.Web.Webs;
// encrypt the password in a secure string and add to the current context
SecureString password = new SecureString();
foreach (char c in adminPassword.ToCharArray())
{
password.AppendChar(c);
}
// add the credentials to the clientContext. This will avoid the nasty 403: Forbidden errors.
clientContext.Credentials = new SharePointOnlineCredentials(adminUsername, password);
// create a new web object using the websiteinfo provided
// and add to the collection to make sure it is under the right parent
Web newWebsite = currentListOfWebs.Add(newWebsiteInfo);
// execute the web provisioning
clientContext.ExecuteQuery();
// if got to this point then it was executed with success
output = true;
}
catch (Exception exception)
{
// something went wrong, add to the ULS, display some message etc...
}
return output;
}
/// <summary>
/// Handles the site provisioning call
/// </summary>
protected void ProvisionSite()
{
WebCreationInformation webCreationInfo = new WebCreationInformation()
{
Title = "This is my new child site",
Description = "This is the description of my new child site",
Language = 1033,
Url = "childsite1",
UseSamePermissionsAsParentSite = true,
WebTemplate = "STS#0" // team site template
};
CreateChildSite("https://<yoursite>.sharepoint.com", "<admin>@<yoursite>.onmicrosoft.com", "<your password>", webCreationInfo);
}
by Edge Pereira
No comments:
Post a Comment