Example #1
0
        /// <summary>
        /// Injects javascript via a adding a custom action to the site
        /// </summary>
        /// <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 bool AddJsBlock(string key, string scriptBlock, ClientContext clientContext, Web web)
        {
            var jsAction = new CustomActionEntity()
            {
                Description = key,
                Location    = ScriptLocation,
                ScriptBlock = scriptBlock,
            };
            bool ret = AddCustomAction(jsAction, clientContext, web);

            return(ret);
        }
Example #2
0
        /// <summary>
        /// Adds or removes a custom action from a site
        /// </summary>
        /// <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 ok</returns>
        public bool AddCustomAction(CustomActionEntity customAction, ClientContext clientContext, Web web)
        {
            bool exist           = false;
            var  existingActions = web.UserCustomActions;

            clientContext.Load(existingActions);
            clientContext.ExecuteQuery();
            var actions = existingActions.ToArray();

            foreach (var action in actions)
            {
                if (action.Description == customAction.Description &&
                    action.Location == customAction.Location)
                {
                    exist = true;
                    action.DeleteObject();
                    clientContext.ExecuteQuery();
                }
            }

            if (customAction.Remove)
            {
                return(false);
            }

            var newAction = existingActions.Add();

            newAction.Description = customAction.Description;
            newAction.Location    = customAction.Location;
            if (customAction.Location == ScriptLocation)
            {
                newAction.ScriptBlock = customAction.ScriptBlock;
            }
            else
            {
                newAction.Sequence = customAction.Sequence;
                newAction.Url      = customAction.Url;
                newAction.Group    = customAction.Group;
                newAction.Title    = customAction.Title;
                newAction.ImageUrl = customAction.ImageUrl;
                newAction.Rights   = customAction.Rights;
            }
            newAction.Update();
            clientContext.Load(web, s => s.UserCustomActions);
            clientContext.ExecuteQuery();
            exist = true;
            return(exist);
        }
Example #3
0
        /// <summary>
        /// Adds or removes a custom action from a site
        /// </summary>
        /// <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 ok</returns>
        public bool AddCustomAction(CustomActionEntity customAction, ClientContext clientContext, Web web)
        {
            bool exist = false;
            var existingActions = web.UserCustomActions;
            clientContext.Load(existingActions);
            clientContext.ExecuteQuery();
            var actions = existingActions.ToArray();
            foreach (var action in actions)
            {
                if (action.Description == customAction.Description &&
                    action.Location == customAction.Location)
                {
                    exist = true;
                    action.DeleteObject();
                    clientContext.ExecuteQuery();
                }
            }

            if (customAction.Remove)
                return false;

            var newAction = existingActions.Add();
            newAction.Description = customAction.Description;
            newAction.Location = customAction.Location;
            if (customAction.Location == ScriptLocation)
            {
                newAction.ScriptBlock = customAction.ScriptBlock;
            }
            else
            {
                newAction.Sequence = customAction.Sequence;
                newAction.Url = customAction.Url;
                newAction.Group = customAction.Group;
                newAction.Title = customAction.Title;
                newAction.ImageUrl = customAction.ImageUrl;
                newAction.Rights = customAction.Rights;
            }
            newAction.Update();
            clientContext.Load(web, s => s.UserCustomActions);
            clientContext.ExecuteQuery();
            exist = true;
            return exist;
        }
Example #4
0
 /// <summary>
 /// Injects javascript via a adding a custom action to the site
 /// </summary>
 /// <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 bool AddJsBlock(string key, string scriptBlock, ClientContext clientContext, Web web)
 {
     var jsAction = new CustomActionEntity()
     {
         Description = key,
         Location = ScriptLocation,
         ScriptBlock = scriptBlock,
     };
     bool ret = AddCustomAction(jsAction, clientContext, web);
     return ret;
 }