private static bool DeleteJsLinkImplementation(ClientObject clientObject, string key)
        {
            var ret = false;
            if (clientObject is Web || clientObject is Site)
            {
                var jsAction = new CustomActionEntity()
                {
                    Name = key,
                    Location = SCRIPT_LOCATION,
                    Remove = true,
                };
                if (clientObject is Web)
                {
                    ret = ((Web) clientObject).AddCustomAction(jsAction);
                }
                else
                {
                    ret = ((Site) clientObject).AddCustomAction(jsAction);
                }

            }
            else
            {
                throw new ArgumentException("Only Site or Web supported as clientObject");
            }
            return ret;
        }
 private static bool AddJsBlockImplementation(ClientObject clientObject, string key, string scriptBlock, int sequence)
 {
     var ret = false;
     if (clientObject is Web || clientObject is Site)
     {
         var jsAction = new CustomActionEntity()
         {
             Name = key,
             Location = SCRIPT_LOCATION,
             ScriptBlock = scriptBlock,
             Sequence = sequence
         };
         if (clientObject is Web)
         {
             ret = ((Web) clientObject).AddCustomAction(jsAction);
         }
         else
         {
             ret = ((Site) clientObject).AddCustomAction(jsAction);
         }
     }
     else
     {
         throw new ArgumentException("Only Site or Web supported as clientObject");
     }
     return ret;
 }
        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);
                    }
                }
            }
        }
 public static bool AddCustomAction(this Site site, CustomActionEntity customAction)
 {
     return AddCustomActionImplementation(site, customAction);
 }
 /// <summary>
 /// Adds custom action to a web. If the CustomAction exists the item will be updated.
 /// Setting CustomActionEntity.Remove == true will delete the CustomAction.
 /// </summary>
 /// <param name="web">Site to be processed - can be root web or sub site</param>
 /// <param name="customAction">Information about the custom action be added or deleted</param>
 /// <example>
 /// var editAction = new CustomActionEntity()
 /// {
 ///    Title = "Edit Site Classification",
 ///    Description = "Manage business impact information for site collection or sub sites.",
 ///    Sequence = 1000,
 ///    Group = "SiteActions",
 ///    Location = "Microsoft.SharePoint.StandardMenu",
 ///    Url = EditFormUrl,
 ///    ImageUrl = EditFormImageUrl,
 ///    Rights = new BasePermissions(),
 /// };
 /// editAction.Rights.Set(PermissionKind.ManageWeb);
 /// AddCustomAction(editAction, new Uri(site.Properties.Url));
 /// </example>
 /// <returns>True if action was successfull</returns>
 public static bool AddCustomAction(this Web web, CustomActionEntity customAction)
 {
     return AddCustomActionImplementation(web, customAction);
 }
        private static bool AddCustomActionImplementation(ClientObject clientObject, CustomActionEntity customAction)
        {
            UserCustomAction targetAction;
            UserCustomActionCollection existingActions;
            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;
                }
                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;
        }