public void Delete() { _userCustomAction.DeleteObject(); _userCustomAction.Context.ExecuteQuery(); if (ParentCollection != null && ParentCollection is SPOUserCustomActionCollection) { ((SPOUserCustomActionCollection)ParentCollection).Remove(this); } }
private static void ChangeCustomActionRegistration() { using (var clientContext = sPContext.CreateUserClientContextForSPHost()) { var query = from action in clientContext.Web.UserCustomActions where action.Name == "6601a902-f458-4757-9000-09f23eaa5386.AddEmployeeToCorpDB" select action; IEnumerable <UserCustomAction> matchingActions = clientContext.LoadQuery(query); clientContext.ExecuteQuery(); UserCustomAction webScopedEmployeeAction = matchingActions.Single(); var queryForList = from list in clientContext.Web.Lists where list.Title == "Local Employees" select list; IEnumerable <List> matchingLists = clientContext.LoadQuery(queryForList); clientContext.ExecuteQuery(); List employeeList = matchingLists.First(); var listActions = employeeList.UserCustomActions; clientContext.Load(listActions); listActions.Clear(); var listScopedEmployeeAction = listActions.Add(); listScopedEmployeeAction.Title = webScopedEmployeeAction.Title; listScopedEmployeeAction.Location = webScopedEmployeeAction.Location; listScopedEmployeeAction.Sequence = webScopedEmployeeAction.Sequence; listScopedEmployeeAction.CommandUIExtension = webScopedEmployeeAction.CommandUIExtension; listScopedEmployeeAction.Update(); webScopedEmployeeAction.DeleteObject(); clientContext.ExecuteQuery(); } }
private static bool AddCustomActionImplementation(ClientObject clientObject, CustomActionEntity customAction) { UserCustomAction targetAction = null; UserCustomActionCollection existingActions = null; if (clientObject is Web) { var web = (Web)clientObject; existingActions = web.UserCustomActions; web.Context.Load(existingActions); web.Context.ExecuteQueryRetry(); targetAction = web.UserCustomActions.FirstOrDefault(uca => uca.Name == customAction.Name); } else { var site = (Site)clientObject; existingActions = site.UserCustomActions; site.Context.Load(existingActions); site.Context.ExecuteQueryRetry(); targetAction = site.UserCustomActions.FirstOrDefault(uca => uca.Name == customAction.Name); } if (targetAction == null) { // If we're removing the custom action then we need to leave when not found...else we're creating the custom action if (customAction.Remove) { return(true); } else { targetAction = existingActions.Add(); } } else if (customAction.Remove) { targetAction.DeleteObject(); clientObject.Context.ExecuteQueryRetry(); return(true); } targetAction.Name = customAction.Name; targetAction.Description = customAction.Description; targetAction.Location = customAction.Location; if (customAction.Location == JavaScriptExtensions.SCRIPT_LOCATION) { targetAction.ScriptBlock = customAction.ScriptBlock; targetAction.ScriptSrc = customAction.ScriptSrc; } else { targetAction.Sequence = customAction.Sequence; targetAction.Url = customAction.Url; targetAction.Group = customAction.Group; targetAction.Title = customAction.Title; targetAction.ImageUrl = customAction.ImageUrl; if (customAction.RegistrationId != null) { targetAction.RegistrationId = customAction.RegistrationId; } if (customAction.CommandUIExtension != null) { targetAction.CommandUIExtension = customAction.CommandUIExtension; } if (customAction.Rights != null) { targetAction.Rights = customAction.Rights; } if (customAction.RegistrationType.HasValue) { targetAction.RegistrationType = customAction.RegistrationType.Value; } } targetAction.Update(); if (clientObject is Web) { var web = (Web)clientObject; web.Context.Load(web, w => w.UserCustomActions); web.Context.ExecuteQueryRetry(); } else { var site = (Site)clientObject; site.Context.Load(site, s => s.UserCustomActions); site.Context.ExecuteQueryRetry(); } return(true); }
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); } } } } }