Пример #1
0
 // 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();
     });
 }
Пример #2
0
        public static void CreateSite(ClientContext clientContext, string siteName)
        {
            if (String.IsNullOrEmpty(siteName))
            {
                throw new ArgumentNullException("siteName");
            }
            if (null == clientContext)
            {
                throw new ArgumentNullException("clientContext");
            }
            // Determine if we're looking for a site collection.
            var useSC = AppHelper.GetProperty(clientContext, Constants.SITECOL_PROPERTY);
            var preferSiteCollection = useSC != null && 0 == String.Compare(useSC.ToString(), "TRUE", StringComparison.OrdinalIgnoreCase);
            // Tidy the site name
            var regex       = new Regex("[^a-zA-Z0-9]");
            var siteNameUrl = regex.Replace(siteName, "");

            if (!preferSiteCollection)
            {
                // Create as subsite.
                if (!SiteExists(clientContext, siteName))
                {
                    CreateSite(clientContext, siteName, siteNameUrl);
                }
                return;
            }
            // Does the site collection already exist?
            if (!SiteCollectionExists(clientContext, siteName))
            {
                CreateSiteCollection(clientContext, siteName, siteNameUrl);
            }
            // Site collection now exists - load the custom WSP.
            var wspName = AppHelper.GetProperty(clientContext, Constants.WSPPATH_PROPERTY) as string;

            if (String.IsNullOrEmpty(wspName))
            {
                return;
            }
            var fullUrl = GetUrlFromSiteName(clientContext, siteName);

            using (var childContext = SPCommon.GetAppOnlyContext(fullUrl))
                SPTemplateHelper.UploadCustomSolution(childContext, wspName);
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }