private void ProvisionCustomActionImplementation(object parent, List <CustomAction> customActions) { TokenParser parser = null; Web web = null; Site site = null; if (parent is Site) { site = parent as Site; parser = new TokenParser(site.RootWeb); } else { web = parent as Web; parser = new TokenParser(web); } foreach (var customAction in customActions) { var caExists = false; if (site != null) { caExists = site.CustomActionExists(customAction.Name); } else { caExists = web.CustomActionExists(customAction.Name); } if (!caExists) { var customActionEntity = new CustomActionEntity(); customActionEntity.CommandUIExtension = customAction.CommandUIExtension; customActionEntity.Description = customAction.Description; customActionEntity.Group = customAction.Group; customActionEntity.ImageUrl = parser.Parse(customAction.ImageUrl); customActionEntity.Location = customAction.Location; customActionEntity.Name = customAction.Name; customActionEntity.RegistrationId = customAction.RegistrationId; customActionEntity.RegistrationType = customAction.RegistrationType; customActionEntity.Remove = customAction.Remove; customActionEntity.Rights = customAction.Rights; customActionEntity.ScriptBlock = customAction.ScriptBlock; customActionEntity.ScriptSrc = parser.Parse(customAction.ScriptSrc); customActionEntity.Sequence = customAction.Sequence; customActionEntity.Title = customAction.Title; customActionEntity.Url = parser.Parse(customAction.Url); if (site != null) { site.AddCustomAction(customActionEntity); } else { web.AddCustomAction(customActionEntity); } } } }
/// <summary> /// Injects javascript 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="scriptBlock">Javascript to be injected</param> /// <returns>True if action was ok</returns> public static bool AddJsBlock(this Web web, string key, string scriptBlock) { var jsAction = new CustomActionEntity() { Description = key, Location = SCRIPT_LOCATION, ScriptBlock = scriptBlock, }; bool ret = web.AddCustomAction(jsAction); return(ret); }
/// <summary> /// Removes the custom action that triggers the execution of a javascript link /// </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 deleted</param> /// <returns>True if action was ok</returns> public static bool DeleteJsLink(this Web web, string key) { var jsAction = new CustomActionEntity() { Description = key, Location = SCRIPT_LOCATION, Remove = true }; bool ret = web.AddCustomAction(jsAction); return(ret); }
private void ProvisionCustomActionImplementation(object parent, List <CustomAction> customActions, TokenParser parser, PnPMonitoredScope scope) { Web web = null; Site site = null; if (parent is Site) { site = parent as Site; // Switch parser context; parser.Rebase(site.RootWeb); } else { web = parent as Web; // Switch parser context parser.Rebase(web); } foreach (var customAction in customActions) { var caExists = false; if (site != null) { caExists = site.CustomActionExists(customAction.Name); } else { caExists = web.CustomActionExists(customAction.Name); } if (!caExists) { var customActionEntity = new CustomActionEntity() { CommandUIExtension = customAction.CommandUIExtension != null?parser.ParseString(customAction.CommandUIExtension.ToString()) : string.Empty, Description = customAction.Description, Group = customAction.Group, ImageUrl = parser.ParseString(customAction.ImageUrl), Location = customAction.Location, Name = customAction.Name, RegistrationId = customAction.RegistrationId, RegistrationType = customAction.RegistrationType, Remove = customAction.Remove, Rights = customAction.Rights, ScriptBlock = parser.ParseString(customAction.ScriptBlock), ScriptSrc = parser.ParseString(customAction.ScriptSrc, "~site", "~sitecollection"), Sequence = customAction.Sequence, Title = customAction.Title, Url = parser.ParseString(customAction.Url) }; if (site != null) { scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_CustomActions_Adding_custom_action___0___to_scope_Site, customActionEntity.Name); site.AddCustomAction(customActionEntity); } else { scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_CustomActions_Adding_custom_action___0___to_scope_Web, customActionEntity.Name); web.AddCustomAction(customActionEntity); } } else { UserCustomAction existingCustomAction = null; if (site != null) { existingCustomAction = site.GetCustomActions().FirstOrDefault(c => c.Name == customAction.Name); } else { existingCustomAction = web.GetCustomActions().FirstOrDefault(c => c.Name == customAction.Name); } if (existingCustomAction != null) { var isDirty = false; if (customAction.CommandUIExtension != null) { if (existingCustomAction.CommandUIExtension != parser.ParseString(customAction.CommandUIExtension.ToString())) { scope.LogPropertyUpdate("CommandUIExtension"); existingCustomAction.CommandUIExtension = parser.ParseString(customAction.CommandUIExtension.ToString()); isDirty = true; } } if (existingCustomAction.Description != customAction.Description) { scope.LogPropertyUpdate("Description"); existingCustomAction.Description = customAction.Description; isDirty = true; } if (existingCustomAction.Group != customAction.Group) { scope.LogPropertyUpdate("Group"); existingCustomAction.Group = customAction.Group; isDirty = true; } if (existingCustomAction.ImageUrl != parser.ParseString(customAction.ImageUrl)) { scope.LogPropertyUpdate("ImageUrl"); existingCustomAction.ImageUrl = parser.ParseString(customAction.ImageUrl); isDirty = true; } if (existingCustomAction.Location != customAction.Location) { scope.LogPropertyUpdate("Location"); existingCustomAction.Location = customAction.Location; isDirty = true; } if (existingCustomAction.RegistrationId != customAction.RegistrationId) { scope.LogPropertyUpdate("RegistrationId"); existingCustomAction.RegistrationId = customAction.RegistrationId; isDirty = true; } if (existingCustomAction.RegistrationType != customAction.RegistrationType) { scope.LogPropertyUpdate("RegistrationType"); existingCustomAction.RegistrationType = customAction.RegistrationType; isDirty = true; } if (existingCustomAction.ScriptBlock != parser.ParseString(customAction.ScriptBlock)) { scope.LogPropertyUpdate("ScriptBlock"); existingCustomAction.ScriptBlock = parser.ParseString(customAction.ScriptBlock); isDirty = true; } if (existingCustomAction.ScriptSrc != parser.ParseString(customAction.ScriptSrc, "~site", "~sitecollection")) { scope.LogPropertyUpdate("ScriptSrc"); existingCustomAction.ScriptSrc = parser.ParseString(customAction.ScriptSrc, "~site", "~sitecollection"); isDirty = true; } if (existingCustomAction.Title != parser.ParseString(customAction.Title)) { scope.LogPropertyUpdate("Title"); existingCustomAction.Title = parser.ParseString(customAction.Title); isDirty = true; } if (existingCustomAction.Url != parser.ParseString(customAction.Url)) { scope.LogPropertyUpdate("Url"); existingCustomAction.Url = parser.ParseString(customAction.Url); isDirty = true; } if (isDirty) { existingCustomAction.Update(); existingCustomAction.Context.ExecuteQueryRetry(); } } } } }
private void ProvisionCustomActionImplementation(object parent, CustomActionCollection customActions, TokenParser parser, PnPMonitoredScope scope, bool isNoScriptSite = false) { Web web = null; Site site = null; if (parent is Site) { site = parent as Site; // Switch parser context; parser.Rebase(site.RootWeb); } else { web = parent as Web; // Switch parser context parser.Rebase(web); } foreach (var customAction in customActions) { if (isNoScriptSite && Guid.Empty == customAction.ClientSideComponentId) { scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_CustomActions_SkippingAddUpdateDueToNoScript, customAction.Name); continue; } var caExists = false; if (site != null) { caExists = site.CustomActionExists(customAction.Name); } else { caExists = web.CustomActionExists(customAction.Name); } // If the CustomAction does not exist, we don't have to remove it, and it is enabled if (!caExists && !customAction.Remove && customAction.Enabled) { // Then we add it to the target var customActionEntity = new CustomActionEntity() { #if !ONPREMISES ClientSideComponentId = customAction.ClientSideComponentId, ClientSideComponentProperties = customAction.ClientSideComponentProperties, #endif CommandUIExtension = customAction.CommandUIExtension != null?parser.ParseString(customAction.CommandUIExtension.ToString()) : string.Empty, Description = parser.ParseString(customAction.Description), Group = customAction.Group, ImageUrl = parser.ParseString(customAction.ImageUrl), Location = customAction.Location, Name = customAction.Name, RegistrationId = customAction.RegistrationId, RegistrationType = customAction.RegistrationType, Remove = customAction.Remove, Rights = customAction.Rights, ScriptBlock = parser.ParseString(customAction.ScriptBlock), ScriptSrc = parser.ParseString(customAction.ScriptSrc, "~site", "~sitecollection"), Sequence = customAction.Sequence, Title = parser.ParseString(customAction.Title), Url = parser.ParseString(customAction.Url) }; if (site != null) { scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_CustomActions_Adding_custom_action___0___to_scope_Site, customActionEntity.Name); site.AddCustomAction(customActionEntity); #if !ONPREMISES if ((!string.IsNullOrEmpty(customAction.Title) && customAction.Title.ContainsResourceToken()) || (!string.IsNullOrEmpty(customAction.Description) && customAction.Description.ContainsResourceToken())) { var uca = site.GetCustomActions().Where(uc => uc.Name == customAction.Name).FirstOrDefault(); SetCustomActionResourceValues(parser, customAction, uca); } #endif } else { scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_CustomActions_Adding_custom_action___0___to_scope_Web, customActionEntity.Name); web.AddCustomAction(customActionEntity); #if !ONPREMISES if (customAction.Title.ContainsResourceToken() || customAction.Description.ContainsResourceToken()) { var uca = web.GetCustomActions().Where(uc => uc.Name == customAction.Name).FirstOrDefault(); SetCustomActionResourceValues(parser, customAction, uca); } #endif } } else { UserCustomAction existingCustomAction = null; if (site != null) { existingCustomAction = site.GetCustomActions().FirstOrDefault(c => c.Name == customAction.Name); } else { existingCustomAction = web.GetCustomActions().FirstOrDefault(c => c.Name == customAction.Name); } if (existingCustomAction != null) { // If we have to remove the existing CustomAction if (customAction.Remove) { // We simply remove it existingCustomAction.DeleteObject(); existingCustomAction.Context.ExecuteQueryRetry(); } else { UpdateCustomAction(parser, scope, customAction, existingCustomAction, isNoScriptSite); } } } } }
private void ProvisionCustomActionImplementation(object parent, List <CustomAction> customActions) { Web web = null; Site site = null; if (parent is Site) { site = parent as Site; // Switch parser context; TokenParser.Rebase(site.RootWeb); } else { web = parent as Web; // Switch parser context TokenParser.Rebase(web); } foreach (var customAction in customActions) { var caExists = false; if (site != null) { caExists = site.CustomActionExists(customAction.Name); } else { caExists = web.CustomActionExists(customAction.Name); } if (!caExists) { var customActionEntity = new CustomActionEntity() { CommandUIExtension = customAction.CommandUIExtension != null?customAction.CommandUIExtension.ToString().ToParsedString() : string.Empty, Description = customAction.Description, Group = customAction.Group, ImageUrl = customAction.ImageUrl.ToParsedString(), Location = customAction.Location, Name = customAction.Name, RegistrationId = customAction.RegistrationId, RegistrationType = customAction.RegistrationType, Remove = customAction.Remove, Rights = customAction.Rights, ScriptBlock = customAction.ScriptBlock.ToParsedString(), ScriptSrc = customAction.ScriptSrc.ToParsedString("~site", "~sitecollection"), Sequence = customAction.Sequence, Title = customAction.Title, Url = customAction.Url.ToParsedString() }; if (site != null) { site.AddCustomAction(customActionEntity); } else { web.AddCustomAction(customActionEntity); } } else { UserCustomAction existingCustomAction = null; if (site != null) { existingCustomAction = site.GetCustomActions().FirstOrDefault(c => c.Name == customAction.Name); } else { existingCustomAction = web.GetCustomActions().FirstOrDefault(c => c.Name == customAction.Name); } if (existingCustomAction != null) { var isDirty = false; if (customAction.CommandUIExtension != null) { if (existingCustomAction.CommandUIExtension != customAction.CommandUIExtension.ToString().ToParsedString()) { existingCustomAction.CommandUIExtension = customAction.CommandUIExtension.ToString().ToParsedString(); isDirty = true; } } if (existingCustomAction.Description != customAction.Description) { existingCustomAction.Description = customAction.Description; isDirty = true; } if (existingCustomAction.Group != customAction.Group) { existingCustomAction.Group = customAction.Group; isDirty = true; } if (existingCustomAction.ImageUrl != customAction.ImageUrl.ToParsedString()) { existingCustomAction.ImageUrl = customAction.ImageUrl.ToParsedString(); isDirty = true; } if (existingCustomAction.Location != customAction.Location) { existingCustomAction.Location = customAction.Location; isDirty = true; } if (existingCustomAction.RegistrationId != customAction.RegistrationId) { existingCustomAction.RegistrationId = customAction.RegistrationId; isDirty = true; } if (existingCustomAction.RegistrationType != customAction.RegistrationType) { existingCustomAction.RegistrationType = customAction.RegistrationType; isDirty = true; } if (existingCustomAction.ScriptBlock != customAction.ScriptBlock.ToParsedString()) { existingCustomAction.ScriptBlock = customAction.ScriptBlock.ToParsedString(); isDirty = true; } if (existingCustomAction.ScriptSrc != customAction.ScriptSrc.ToParsedString("~site", "~sitecollection")) { existingCustomAction.ScriptSrc = customAction.ScriptSrc.ToParsedString("~site", "~sitecollection"); isDirty = true; } if (existingCustomAction.Title != customAction.Title.ToParsedString()) { existingCustomAction.Title = customAction.Title.ToParsedString(); isDirty = true; } if (existingCustomAction.Url != customAction.Url.ToParsedString()) { existingCustomAction.Url = customAction.Url.ToParsedString(); isDirty = true; } if (isDirty) { existingCustomAction.Update(); existingCustomAction.Context.ExecuteQueryRetry(); } } } } }