コード例 #1
0
ファイル: Default.aspx.cs プロジェクト: SpikesBE/OfficeDevPnP
        /// <summary>
        /// Does the dynamic configuration fro the WCF end point using code
        /// </summary>
        /// <returns>Needed proxy client with impersonation information.</returns>
        private SiteManager.SiteManagerClient GetSiteManagerClient()
        {
            BasicHttpBinding binding = new BasicHttpBinding();

            if (ConfigurationManager.AppSettings["WebApplicationUrl"].Contains("https://"))
            {
                binding.Security.Mode = BasicHttpSecurityMode.Transport;
            }
            else
            {
                binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            }
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

            EndpointAddress endPoint = new EndpointAddress(ConfigurationManager.AppSettings["WebApplicationUrl"] + "/_vti_bin/provisioning.services.sitemanager/sitemanager.svc");

            //Set time outs, since site collection creation could take a while. Also set on server side.
            binding.ReceiveTimeout = TimeSpan.FromMinutes(15);
            binding.CloseTimeout   = TimeSpan.FromMinutes(15);
            binding.OpenTimeout    = TimeSpan.FromMinutes(15);
            binding.SendTimeout    = TimeSpan.FromMinutes(15);

            //Create proxy instance
            SiteManager.SiteManagerClient managerClient = new SiteManager.SiteManagerClient(binding, endPoint);
            managerClient.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            // Set impersonation information. This account is needed to have the web application permissions in SP side
            var impersonator = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Service_UserId"],
                                                                ConfigurationManager.AppSettings["Service_Pwd"],
                                                                ConfigurationManager.AppSettings["Service_Domain"]);

            managerClient.ClientCredentials.Windows.ClientCredential = impersonator;

            return(managerClient);
        }
コード例 #2
0
ファイル: Default.aspx.cs プロジェクト: SpikesBE/OfficeDevPnP
        protected void Create_Click(object sender, EventArgs e)
        {
            // Let's first create the site collection using impersonation
            SiteManager.SiteManagerClient managerClient = GetSiteManagerClient();
            SiteManager.SiteData          newSite       = new SiteManager.SiteData()
            {
                Description           = txtDescription.Text,
                LcId                  = "1033",
                OwnerLogin            = txtAdminPrimary.Text,
                SecondaryContactLogin = txtAdminSecondary.Text,
                Title                 = txtTitle.Text,
                Url         = string.Format("sites/" + txtUrl.Text),
                WebTemplate = listSites.SelectedValue
            };
            // Create the site collection by calling the WCF end point in SP farm. Starting from April CU (2014), this is supported also with CSOM
            string newSiteUrl = managerClient.CreateSiteCollection(newSite);

            // Let's also set the site regiional settings to en-UK using the WCF end point, since this is not exposed usign CSOM
            managerClient.SetSiteLocale(newSiteUrl, "fi-fi");

            // Let's also brand the just created site collection properly using app identity
            // Using app identity, since we don't know if the requestor account has permissions to just created site collection
            Uri    targetSite = new Uri(newSiteUrl);
            string realm      = TokenHelper.GetRealmFromTargetUrl(targetSite);
            var    token      = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, targetSite.Authority, realm).AccessToken;

            using (var ctx = TokenHelper.GetClientContextWithAccessToken(targetSite.ToString(), token))
            {
                // Deploy theme to web, so that we can set that for the site
                Web web = ctx.Web;
                ctx.Load(web);
                ctx.ExecuteQuery();
                DeployThemeToWeb(ctx, web);

                //Set the properties for applying custom theme which was jus uplaoded
                string spColorURL      = URLCombine(web.ServerRelativeUrl, "/_catalogs/theme/15/contoso.spcolor");
                string spFontURL       = URLCombine(web.ServerRelativeUrl, "/_catalogs/theme/15/contoso.spfont");
                string backGroundImage = URLCombine(web.ServerRelativeUrl, "/_catalogs/theme/15/contosobg.jpg");

                // Use the Red theme for demonstration
                web.ApplyTheme(spColorURL,
                               spFontURL,
                               backGroundImage,
                               false);
                ctx.ExecuteQuery();

                // Redirect to just created site
                Response.Redirect(newSiteUrl);
            }
        }