/// <summary> /// Injects links to javascript files via a adding a custom action to the site /// </summary> /// <param name="web">Site to be processed - can be root web or sub site</param> /// <param name="key">Identifier (key) for the custom action that will be created</param> /// <param name="scriptLinks">semi colon delimited list of links to javascript files</param> /// <returns>True if action was ok</returns> public static bool AddJsLink(this Web web, string key, string scriptLinks) { return(web.AddJsLink(key, new List <string>(scriptLinks.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)))); }
public static void ApplyBrandingOnWeb(Web targetWeb, BrandingSettings brandingSettings, ProvisioningTemplate template) { targetWeb.EnsureProperties(w => w.MasterUrl, w => w.Url); // Configure proper settings for the provisioning engine ProvisioningTemplateApplyingInformation ptai = new ProvisioningTemplateApplyingInformation(); // Write provisioning steps on console log ptai.MessagesDelegate += delegate(string message, ProvisioningMessageType messageType) { Console.WriteLine("{0} - {1}", messageType, messageType); }; ptai.ProgressDelegate += delegate(string message, int step, int total) { Console.WriteLine("{0:00}/{1:00} - {2}", step, total, message); }; // Include only required handlers ptai.HandlersToProcess = Core.Framework.Provisioning.Model.Handlers.ComposedLook | Core.Framework.Provisioning.Model.Handlers.Files | Core.Framework.Provisioning.Model.Handlers.WebSettings; // Check if we really need to apply/update the branding var siteBrandingUpdatedOn = targetWeb.GetPropertyBagValueString( PnPPartnerPackConstants.PropertyBag_Branding_AppliedOn, null); // If the branding updated on date and time are missing // or older than the branding update date and time if (String.IsNullOrEmpty(siteBrandingUpdatedOn) || DateTime.Parse(siteBrandingUpdatedOn) < brandingSettings.UpdatedOn.Value.ToUniversalTime()) { Console.WriteLine($"Appling branding to site: {targetWeb.Url}"); // Confirm the master page URL template.WebSettings.MasterPageUrl = targetWeb.MasterUrl; // Apply the template targetWeb.ApplyProvisioningTemplate(template, ptai); // Apply a custom JSLink, if any if (!String.IsNullOrEmpty(brandingSettings.UICustomActionsUrl)) { targetWeb.AddJsLink( PnPPartnerPackConstants.BRANDING_SCRIPT_LINK_KEY, brandingSettings.UICustomActionsUrl); } // Store Property Bag to set the last date and time when we applied the branding targetWeb.SetPropertyBagValue( PnPPartnerPackConstants.PropertyBag_Branding_AppliedOn, DateTime.Now.ToUniversalTime().ToString()); Console.WriteLine($"Applied branding to site: {targetWeb.Url}"); } // Apply branding (recursively) on all the subwebs of the current web targetWeb.EnsureProperty(w => w.Webs); foreach (var subweb in targetWeb.Webs) { ApplyBrandingOnWeb(subweb, brandingSettings, template); } }
protected void Page_Load(object sender, EventArgs e) { var spContext = SharePointContextProvider.Current.GetSharePointContext(Context); using (ClientContext clientContext = spContext.CreateUserClientContextForSPHost()) { // Get user profile ProfileLoader loader = Microsoft.SharePoint.Client.UserProfiles.ProfileLoader.GetProfileLoader(clientContext); UserProfile profile = loader.GetUserProfile(); Microsoft.SharePoint.Client.Site personalSite = profile.PersonalSite; clientContext.Load(personalSite); clientContext.ExecuteQuery(); // Let's check if the site already exists if (personalSite.ServerObjectIsNull.Value) { // Let's queue the personal site creation using oob timer job based approach // Using async mode, since end user could go away from browser, you could do this using oob web part as well profile.CreatePersonalSiteEnque(true); clientContext.ExecuteQuery(); WriteDebugInformationIfNeeded("OneDrive for Business site was not present, queued for provisioning now."); } else { // Site already exists, let's modify the branding by applying a theme... just as well you could upload // master page and set that to be shown. Notice that you can also modify this code to change the branding // later and updates would be reflected whenever user visits OneDrive host... or any other location where this // app part is located. You could place this also to front page of the intranet for ensuring that it's applied. Web rootWeb = personalSite.RootWeb; clientContext.Load(rootWeb); clientContext.ExecuteQuery(); //Let's set the theme only if needed, note that you can easily check for example specific version here as well if (rootWeb.GetPropertyBagValueInt(OneDriveCustomizer.OneDriveMarkerBagID, 0) < 2) { // Let's first upload the contoso theme to host web, if it does not exist there var colorFile = rootWeb.UploadThemeFile(HostingEnvironment.MapPath(string.Format("~/{0}", "Resources/Themes/SPC/SPCTheme.spcolor"))); var backgroundFile = rootWeb.UploadThemeFile(HostingEnvironment.MapPath(string.Format("~/{0}", "Resources/Themes/SPC/SPCbg.jpg"))); rootWeb.CreateComposedLookByUrl("SPC", colorFile.ServerRelativeUrl, null, backgroundFile.ServerRelativeUrl, string.Empty); // Setting the Contoos theme to host web rootWeb.SetComposedLookByUrl("SPC"); // Add additional JS injection with the policy statement to the site rootWeb.AddJsLink("OneDriveCustomJS", BuildJavaScriptUrl()); // Let's set the site processed, so that we don't update that all the time. Currently set as "version" 1 of branding rootWeb.SetPropertyBagValue(OneDriveCustomizer.OneDriveMarkerBagID, 2); // Write output if enabled WriteDebugInformationIfNeeded(string.Format("OneDrive for Business site existed at {0}. Custom branding applied.", personalSite.Url)); } else { // Just to output status if enabled in the app part WriteDebugInformationIfNeeded(string.Format("OneDrive for Business site existed at {0} and had right branding.", personalSite.Url)); } } } }
public static void ApplyBrandingOnWeb(Web targetWeb, BrandingSettings brandingSettings, ProvisioningTemplate template) { targetWeb.EnsureProperties(w => w.MasterUrl, w => w.Url); // Configure proper settings for the provisioning engine ProvisioningTemplateApplyingInformation ptai = new ProvisioningTemplateApplyingInformation(); // Write provisioning steps on console log ptai.MessagesDelegate += delegate (string message, ProvisioningMessageType messageType) { Console.WriteLine("{0} - {1}", messageType, messageType); }; ptai.ProgressDelegate += delegate (string message, int step, int total) { Console.WriteLine("{0:00}/{1:00} - {2}", step, total, message); }; // Include only required handlers ptai.HandlersToProcess = Core.Framework.Provisioning.Model.Handlers.ComposedLook | Core.Framework.Provisioning.Model.Handlers.Files | Core.Framework.Provisioning.Model.Handlers.WebSettings; // Check if we really need to apply/update the branding var siteBrandingUpdatedOn = targetWeb.GetPropertyBagValueString( PnPPartnerPackConstants.PropertyBag_Branding_AppliedOn, null); // If the branding updated on date and time are missing // or older than the branding update date and time if (String.IsNullOrEmpty(siteBrandingUpdatedOn) || DateTime.Parse(siteBrandingUpdatedOn) < brandingSettings.UpdatedOn.Value.ToUniversalTime()) { Console.WriteLine($"Appling branding to site: {targetWeb.Url}"); // Confirm the master page URL template.WebSettings.MasterPageUrl = targetWeb.MasterUrl; // Apply the template targetWeb.ApplyProvisioningTemplate(template, ptai); // Apply a custom JSLink, if any if (!String.IsNullOrEmpty(brandingSettings.UICustomActionsUrl)) { targetWeb.AddJsLink( PnPPartnerPackConstants.BRANDING_SCRIPT_LINK_KEY, brandingSettings.UICustomActionsUrl); } // Store Property Bag to set the last date and time when we applied the branding targetWeb.SetPropertyBagValue( PnPPartnerPackConstants.PropertyBag_Branding_AppliedOn, DateTime.Now.ToUniversalTime().ToString()); Console.WriteLine($"Applied branding to site: {targetWeb.Url}"); } // Apply branding (recursively) on all the subwebs of the current web targetWeb.EnsureProperty(w => w.Webs); foreach (var subweb in targetWeb.Webs) { ApplyBrandingOnWeb(subweb, brandingSettings, template); } }