// This method relies on the April 2014 CU in SP2013.
 // http://blogs.msdn.com/b/vesku/archive/2014/06/09/provisioning-site-collections-using-sp-app-model-in-on-premises-with-just-csom.aspx
 private static void CreateSiteCollection(ClientContext clientContext, string siteName, string siteNameUrl)
 {
     if (null == clientContext)
     {
         throw new ArgumentNullException("clientContext");
     }
     if (String.IsNullOrEmpty(siteName))
     {
         throw new ArgumentNullException("siteName");
     }
     if (String.IsNullOrEmpty(siteNameUrl))
     {
         throw new ArgumentNullException("siteNameUrl");
     }
     // Create site collection.
     SPCommon.LoadTenant(clientContext, (tenant, adminContext) =>
     {
         var webUrl = GetUrlFromSiteName(clientContext, siteName);
         // Ensure the user
         var username = AppHelper.GetProperty(clientContext, Constants.SITEOWNER_PROPERTY) as string;
         if (String.IsNullOrEmpty(username))
         {
             throw new Exception("Default site owner not set");
         }
         clientContext.Load(clientContext.Web);
         clientContext.ExecuteQuery();
         var user = clientContext.Web.EnsureUser(username);
         if (null == user)
         {
             throw new Exception(String.Format("User {0} not found", username));
         }
         clientContext.Load(user);
         clientContext.ExecuteQuery();
         var properties = new SiteCreationProperties
         {
             Url   = webUrl,
             Owner = user.LoginName,
             Title = siteName,
             // Use a blank site template until we can add customizations to the created site collection
             // On-Prem won't allow creation of site collection w/o valid template.
             Template = "STS#1"
         };
         // Start the SPO operation to create the site
         // Note in O365 this operation is asynchronous whereas on prem it synchronous.
         var op = tenant.CreateSite(properties);
         adminContext.Load(op, i => i.IsComplete);
         adminContext.ExecuteQuery();
     });
 }
        private static bool SiteCollectionExists(ClientContext clientContext, string siteName)
        {
            var result = false;

            if (null == clientContext)
            {
                throw new ArgumentNullException("clientContext");
            }
            if (String.IsNullOrEmpty(siteName))
            {
                throw new ArgumentNullException("siteName");
            }
            SPCommon.LoadTenant(clientContext, (tenant, adminContext) =>
            {
                var webUrl = GetUrlFromSiteName(clientContext, siteName);
                var spp    = tenant.GetSitePropertiesByUrl(webUrl, true);
                adminContext.Load(spp);
                adminContext.ExecuteQuery();
                result = true;
            });
            return(result);
        }
        public static bool ValidateTemplate(ClientContext clientContext, string templateName)
        {
            if (null == clientContext)
            {
                throw new ArgumentNullException("clientContext");
            }
            if (String.IsNullOrEmpty(templateName))
            {
                throw new ArgumentNullException("templateName");
            }
            var result = false;

            SPCommon.LoadTenant(clientContext, (tenant, adminContext) =>
            {
                var templates = tenant.GetSPOTenantWebTemplates(1033, 15);
                adminContext.Load(templates);
                adminContext.ExecuteQuery();
                result = (Enumerable.Any(templates, template =>
                                         template.Name == templateName ||
                                         template.Title == templateName));
            });
            return(result);
        }