public ActionResult CreateSite(NewSiteProperties props) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); var baseUrl = $"{spContext.SPHostUrl.Scheme}://{spContext.SPHostUrl.Host}"; // Create admin URL var adminUrl = new Uri(baseUrl.Insert(baseUrl.IndexOf("."), "-admin")); // Get the access token var accessToken = TokenHelper.GetAppOnlyAccessToken( TokenHelper.SharePointPrincipal, adminUrl.Authority, TokenHelper.GetRealmFromTargetUrl(adminUrl)).AccessToken; // Create the tenant ClientContext and create the site using (var ctx = TokenHelper.GetClientContextWithAccessToken(adminUrl.ToString(), accessToken)) { Tenant tenant = new Tenant(ctx); // Pass the false parameter for wait so we do not hold the connection open // while waiting for the site to be created. Instead we show a spinner. tenant.CreateSiteCollection($"{baseUrl}/sites/{props.Url}", props.Title, props.SiteOwnerEmail, props.SelectedWebTemplate, 1000, 800, 7, 10, 8, 1033, false, false, null); } // Change the leaf URL to the AbsoluteUri so we can provide a link to the newly created site. props.SPHostUrl = spContext.SPHostUrl.AbsoluteUri; //return View(); return(RedirectToAction("SiteStatus", props)); }
public ActionResult Scenario2() { User spUser; var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); // Get the current user's email to specify as the Site Collection owner. using (var cc = spContext.CreateUserClientContextForSPHost()) { spUser = cc.Web.CurrentUser; cc.Load(spUser); cc.ExecuteQueryRetry(); } var model = new NewSiteProperties { SiteOwnerEmail = spUser.Email }; return(View(model)); }
public ActionResult SiteStatus(NewSiteProperties props) { var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); var baseUrl = $"{spContext.SPHostUrl.Scheme}://{spContext.SPHostUrl.Host}"; // Build the admin URL var adminUrl = new Uri(baseUrl.Insert(baseUrl.IndexOf("."), "-admin")); // Get the access token string accessToken = TokenHelper.GetAppOnlyAccessToken( TokenHelper.SharePointPrincipal, adminUrl.Authority, TokenHelper.GetRealmFromTargetUrl(adminUrl)).AccessToken; // Check if the site exists and is "Active" if it does we return the View // If it is not "Active" yet, we return the "WaitingOnSite" view with a spinner using (var ctx = TokenHelper.GetClientContextWithAccessToken(adminUrl.ToString(), accessToken)) { Tenant tenant = new Tenant(ctx); // Checks to see if site is created yet. var isSiteAvailable = tenant.CheckIfSiteExists($"{baseUrl}/sites/{props.Url}", "Active"); if (!isSiteAvailable) { // This view uses JavaScript to refresh every 10 seconds to check if the site has been created return(View("WaitingOnSite")); } else { // Convert the URL to Absolute so we can provide a link to the new site collection props.Url = $"{baseUrl}/sites/{props.Url}"; return(View(props)); } } }