Esempio n. 1
0
        static void Main(string[] args)
        {
            // Update these accordingly for your environment
            string tenantName = ConfigurationManager.AppSettings["TenantName"];
            string ownwerEmail = ConfigurationManager.AppSettings["SiteColTestOwnerEmail"];

            //create site collection using the Tenant object. Notice that you will need to have valid app ID and secret for this one
            var tenantAdminUri = new Uri(String.Format("https://{0}-admin.sharepoint.com", tenantName));
            string realm = TokenHelper.GetRealmFromTargetUrl(tenantAdminUri);
            var token = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, tenantAdminUri.Authority, realm).AccessToken;
            using (var adminContext = TokenHelper.GetClientContextWithAccessToken(tenantAdminUri.ToString(), token))
            {
                // Call the creation.
                SiteCollectionRequest data = new SiteCollectionRequest() {
                    TenantName = tenantName,
                    Url = DateTime.Now.Ticks.ToString(),
                    Owner = ownwerEmail,
                    ManagedPath = "sites",
                    ProvisioningType = SiteProvisioningType.Identity,
                    TemplateId = "CT1",
                    TimeZoneId = 16,
                    StorageMaximumLevel = 110,
                    Title = "Test site collection"
                };
               
                // Process request for new site
                new SiteManager().ProcessSiteCreationRequest(adminContext, data);
            }
        }
Esempio n. 2
0
        public string ProcessSiteCreationRequest(ClientContext ctx, SiteCollectionRequest siteRequest)
        {
            // Resolve full URL 
            var webFullUrl = String.Format("https://{0}.sharepoint.com/{1}/{2}", siteRequest.TenantName, siteRequest.ManagedPath, siteRequest.Url);

            // Resolve the actual SP template to use
            string siteTemplate = SolveActualTemplate(siteRequest);

            Tenant tenant = new Tenant(ctx);
            if (tenant.SiteExists(webFullUrl))
            {
                // Abort... can't proceed, URL taken.
                throw new InvalidDataException(string.Format("site already existed with same URL as {0}. Process aborted.", webFullUrl));
            }
            else
            {
                // Create new site collection with storage limits and settings from the form
                tenant.CreateSiteCollection(webFullUrl,
                                            siteRequest.Title,
                                            siteRequest.Owner,
                                            siteTemplate,
                                            (int)siteRequest.StorageMaximumLevel,
                                            (int)(siteRequest.StorageMaximumLevel * 0.75),
                                            siteRequest.TimeZoneId,
                                            0,
                                            0,
                                            siteRequest.Lcid);

                return webFullUrl;
            }
        }
Esempio n. 3
0
        public string ProcessSiteCreationRequest(ClientContext ctx, SiteCollectionRequest siteRequest)
        {
            // Resolve full URL
            var webFullUrl = String.Format("https://{0}.sharepoint.com/{1}/{2}", siteRequest.TenantName, siteRequest.ManagedPath, siteRequest.Url);

            // Resolve the actual SP template to use
            string siteTemplate = SolveActualTemplate(siteRequest);

            Tenant tenant = new Tenant(ctx);

            if (tenant.SiteExists(webFullUrl))
            {
                // Abort... can't proceed, URL taken.
                throw new InvalidDataException(string.Format("site already existed with same URL as {0}. Process aborted.", webFullUrl));
            }
            else
            {
                // Create new site collection with storage limits and settings from the form
                tenant.CreateSiteCollection(webFullUrl,
                                            siteRequest.Title,
                                            siteRequest.Owner,
                                            siteTemplate,
                                            (int)siteRequest.StorageMaximumLevel,
                                            (int)(siteRequest.StorageMaximumLevel * 0.75),
                                            siteRequest.TimeZoneId,
                                            0,
                                            0,
                                            siteRequest.Lcid);

                return(webFullUrl);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Used to add new storage queue entry.
        /// </summary>
        /// <param name="account"></param>
        /// <param name="siteUrl"></param>
        /// <param name="storageConnectionString"></param>
        public void AddConfigRequestToQueue(SiteCollectionRequest siteRequest, string storageConnectionString)
        {
            CloudStorageAccount storageAccount =
                                CloudStorageAccount.Parse(storageConnectionString);

            // Get queue... create if does not exist.
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            CloudQueue queue =
                queueClient.GetQueueReference(SiteManager.StorageQueueName);
            queue.CreateIfNotExists();

            // Add entry to queue
            queue.AddMessage(new CloudQueueMessage(JsonConvert.SerializeObject(siteRequest)));

        }
Esempio n. 5
0
        /// <summary>
        /// Used to add new storage queue entry.
        /// </summary>
        /// <param name="account"></param>
        /// <param name="siteUrl"></param>
        /// <param name="storageConnectionString"></param>
        public void AddConfigRequestToQueue(SiteCollectionRequest siteRequest, string storageConnectionString)
        {
            CloudStorageAccount storageAccount =
                CloudStorageAccount.Parse(storageConnectionString);

            // Get queue... create if does not exist.
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            CloudQueue       queue       =
                queueClient.GetQueueReference(SiteManager.StorageQueueName);

            queue.CreateIfNotExists();

            // Add entry to queue
            queue.AddMessage(new CloudQueueMessage(JsonConvert.SerializeObject(siteRequest)));
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("*************");
            Console.WriteLine("** PnP provisioning Engine");
            Console.WriteLine("************");

            string templateSite = GetUserInput("Template site URL:");
            string targetSite = GetUserInput("Target site URL:"); 

            // Log the start time
            Console.WriteLine("Start: {0:hh.mm.ss}", DateTime.Now);

            //Get the realm for the target URL
            Uri siteUri = new Uri(targetSite);
            string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
            //Get the access token for the URL.  Requires this app to be registered with the tenant
            string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,
                                                                    siteUri.Authority, realm).AccessToken;

            //Get client context with access token
            using (var ctx = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken))
            {
                SiteCollectionRequest data = new SiteCollectionRequest()
                {
                    TenantName = "contoso",
                    Url = DateTime.Now.Ticks.ToString(),
                    Owner = "*****@*****.**",
                    ManagedPath = "sites",
                    ProvisioningType = SiteProvisioningType.TemplateSite,
                    TemplateId = templateSite,
                    TimeZoneId = 16,
                    StorageMaximumLevel = 110,
                    Title = "Test site collection"
                };

                // Execute the transformation
                new SiteManager().ApplyCustomTemplateToSite(ctx, data, @".\Resources");
            }

            // Log the end time
            Console.WriteLine("End: {0:hh.mm.ss}", DateTime.Now);
        }
Esempio n. 7
0
        /// <summary>
        /// Applies actual template on top of given site URL.
        /// </summary>
        /// <param name="webFullUrl"></param>
        /// <param name="siteRequest"></param>
        public void ApplyCustomTemplateToSite(ClientContext ctx, SiteCollectionRequest siteRequest, string resourcesPath)
        {
            // Template to be applied to site
            ProvisioningTemplate template = null;

            // Apply modification to provided site
            switch (siteRequest.ProvisioningType)
            {
            case SiteProvisioningType.Identity:

                // Get template from xml file
                XMLFileSystemTemplateProvider provider = new XMLFileSystemTemplateProvider(resourcesPath, "");
                template = provider.GetTemplate(siteRequest.TemplateId);

                break;

            case SiteProvisioningType.TemplateSite:

                // Get template from existing site
                using (ClientContext cc2 = ctx.Clone(siteRequest.TemplateId))
                {
                    // Specify null as base template since we do want "everything" in this case
                    ProvisioningTemplateCreationInformation creationInfo = new ProvisioningTemplateCreationInformation(cc2.Web);
                    creationInfo.BaseTemplate             = cc2.Web.GetBaseTemplate();
                    creationInfo.PersistComposedLookFiles = true;
                    creationInfo.FileConnector            = new FileSystemConnector(resourcesPath, "");

                    // Get template from existing site
                    template = cc2.Web.GetProvisioningTemplate(creationInfo);
                }
                break;

            default:
                break;
            }

            // Apply template to the site
            template.Connector = new FileSystemConnector(resourcesPath, "");
            ctx.Web.ApplyProvisioningTemplate(template);
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            // Update  parameters accordingly based on our environment from app.config
            // Update these accordingly for your environment
            string tenantName = ConfigurationManager.AppSettings["TenantName"];
            string ownwerEmail = ConfigurationManager.AppSettings["SiteColTestOwnerEmail"];

            // Create provisioning message objects for the storage queue
            SiteCollectionRequest data = new SiteCollectionRequest()
            {
                TenantName = tenantName,
                Url = DateTime.Now.Ticks.ToString(),
                Owner = ownwerEmail,
                ManagedPath = "sites",
                ProvisioningType = SiteProvisioningType.TemplateSite,
                TemplateId = "https://contoso.sharepoint.com/sites/templatesite",
                TimeZoneId = 16,
                StorageMaximumLevel = 110,
                Title = "Test site collection"
            };
            
            new SiteManager().AddConfigRequestToQueue(data,
                                            ConfigurationManager.AppSettings["StorageConnectionString"]);
        }
Esempio n. 9
0
        /// <summary>
        /// Applies actual template on top of given site URL. 
        /// </summary>
        /// <param name="webFullUrl"></param>
        /// <param name="siteRequest"></param>
        public void ApplyCustomTemplateToSite(ClientContext ctx, SiteCollectionRequest siteRequest, string resourcesPath)
        {
            // Template to be applied to site
            ProvisioningTemplate template = null;

            // Apply modification to provided site
            switch (siteRequest.ProvisioningType)
            {
                case SiteProvisioningType.Identity:

                    // Get template from xml file
                    XMLFileSystemTemplateProvider provider = new XMLFileSystemTemplateProvider(resourcesPath, "");
                    template = provider.GetTemplate(siteRequest.TemplateId);

                    break;
                case SiteProvisioningType.TemplateSite:

                    // Get template from existing site
                    using (ClientContext cc2 = ctx.Clone(siteRequest.TemplateId))
                    {

                        // Specify null as base template since we do want "everything" in this case
                        ProvisioningTemplateCreationInformation creationInfo = new ProvisioningTemplateCreationInformation(cc2.Web);
                        creationInfo.BaseTemplate = cc2.Web.GetBaseTemplate();
                        creationInfo.PersistComposedLookFiles = true;
                        creationInfo.FileConnector = new FileSystemConnector(resourcesPath, "");

                        // Get template from existing site
                        template = cc2.Web.GetProvisioningTemplate(creationInfo);
                    }
                    break;
                default:
                    break;
            }

            // Apply template to the site
            template.Connector = new FileSystemConnector(resourcesPath, "");
            ctx.Web.ApplyProvisioningTemplate(template);
        }
Esempio n. 10
0
 /// <summary>
 /// Solves the used template based on the request object values
 /// </summary>
 /// <param name="siteRequest"></param>
 /// <returns></returns>
 private static string SolveActualTemplate(SiteCollectionRequest siteRequest)
 {
     // Currently we do not resolve the actual selection to root template, only team site supported by this solution.
     return "STS#0";
 }
Esempio n. 11
0
 /// <summary>
 /// Solves the used template based on the request object values
 /// </summary>
 /// <param name="siteRequest"></param>
 /// <returns></returns>
 private static string SolveActualTemplate(SiteCollectionRequest siteRequest)
 {
     // Currently we do not resolve the actual selection to root template, only team site supported by this solution.
     return("STS#0");
 }
Esempio n. 12
0
        private void AddRequestToQueue(string ownerEmail)
        {
            //get the base tenant url
            var tenantName = Page.Request["SPHostUrl"].ToLower().Replace("-my", "").Substring(8);
            tenantName = tenantName.Substring(0, tenantName.IndexOf("."));

            // Create provisioning message objects for the storage queue
            SiteCollectionRequest data = new SiteCollectionRequest()
            {
                TenantName = tenantName,
                Url = txtUrl.Text,
                Owner = ownerEmail,
                ManagedPath = "sites",
                TimeZoneId = int.Parse(timeZone.SelectedValue),
                StorageMaximumLevel = int.Parse(txtStorage.Text),
                Lcid = uint.Parse(language.SelectedValue),
                Title = txtTitle.Text
            };

            if (templateSelectionType.SelectedValue == "Site")
            {
                data.ProvisioningType = SiteProvisioningType.TemplateSite;
                data.TemplateId = templateSiteLink.Text;
            }
            else
            {
                data.ProvisioningType = SiteProvisioningType.Identity;
                data.TemplateId = listTemplates.SelectedValue;
            }

            new SiteManager().AddConfigRequestToQueue(data,
                                            ConfigurationManager.AppSettings["StorageConnectionString"]);
        }