示例#1
0
        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);
                    }
                }
            }

        }
示例#2
0
 /// <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;
 }
示例#3
0
 /// <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;
 }
示例#4
0
        /// <summary>
        /// Adds or removes a custom action from a site
        /// </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 ok</returns>
        public static bool AddCustomAction(this Web web, CustomActionEntity customAction)
        {
            var existingActions = web.UserCustomActions;
            web.Context.Load(existingActions);
            web.Context.ExecuteQuery();

            // first delete the action with the same name (if it exists)
            var actions = existingActions.ToArray();
            foreach (var action in actions)
            {
                if (action.Description == customAction.Description &&
                    action.Location == customAction.Location)
                {
                    action.DeleteObject();
                    web.Context.ExecuteQuery();
                }
            }

            // leave as we're just removing the custom action
            if (customAction.Remove)
                return false;

            // add a new custom action
            var newAction = existingActions.Add();
            newAction.Description = customAction.Description;
            newAction.Location = customAction.Location;
            if (customAction.Location == JavaScriptExtensions.SCRIPT_LOCATION)
            {
                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;
                if (customAction.Rights != null)
                {
                    newAction.Rights = customAction.Rights;
                }
            }
            newAction.Update();
            web.Context.Load(web, w => w.UserCustomActions);
            web.Context.ExecuteQuery();

            return true;
        }
        protected override void ExecuteCmdlet()
        {
            var permissions = new BasePermissions();
            if (Rights != null)
            {
                foreach (var kind in Rights)
                {
                    permissions.Set(kind);
                }
            }

            var ca = new CustomActionEntity
            {
                Name = Name,
                ImageUrl = ImageUrl,
                CommandUIExtension = CommandUIExtension,
                RegistrationId = RegistrationId,
                RegistrationType = RegistrationType,
                Description = Description,
                Location = Location,
                Group = Group,
                Sequence = Sequence,
                Title = Title,
                Url = Url,
                Rights = permissions
            };

            switch (Scope)
            {
                case CustomActionScope.Web:
                    SelectedWeb.AddCustomAction(ca);
                    break;

                case CustomActionScope.Site:
                    ClientContext.Site.AddCustomAction(ca);
                    break;

                case CustomActionScope.All:
                    WriteWarning("CustomActionScope 'All' is not supported for adding CustomActions");
                    break;
            }
        }
示例#6
0
        protected void btnAddCustomAction_Click(object sender, EventArgs e)
        {
            //Prepare the javascript for the open in dialog action
            StringBuilder modelDialogScript = new StringBuilder(10);
            modelDialogScript.Append("javascript:var dlg=SP.UI.ModalDialog.showModalDialog({url: '");
            modelDialogScript.Append(String.Format("{0}", SetIsDlg("1")));
            modelDialogScript.Append("', dialogReturnValueCallback:function(res, val) {} });");       
            
            //Create a custom action
            CustomActionEntity customAction = new CustomActionEntity()
            {
                Title = "Office AMS Dialog sample",                
                Description = "Shows how to launch an app inside a dialog",
                Location = "Microsoft.SharePoint.StandardMenu",
                Group = "SiteActions",
                Sequence = 10000,
                Url = modelDialogScript.ToString(),
            };

            //Add the custom action to the site
            cc.Web.AddCustomAction(customAction);
        }
示例#7
0
        protected override void ExecuteCmdlet()
        {
            var permissions = new BasePermissions();
            foreach (var kind in Rights)
            {
                permissions.Set(kind);
            }
            var ca = new CustomActionEntity { Description = Description, Location = Location, Group = Group, Sequence = Sequence, Title = Title, Url = Url, Rights = new BasePermissions() };

            foreach (var permission in Rights)
            {
                ca.Rights.Set(permission);
            }

            if (Scope == CustomActionScope.Web)
            {
                SelectedWeb.AddCustomAction(ca);
            }
            else
            {
                ClientContext.Site.AddCustomAction(ca);
            }
        }
示例#8
0
        protected override void ExecuteCmdlet()
        {
            BasePermissions permissions = new BasePermissions();
            foreach (PermissionKind kind in Rights)
            {
                permissions.Set(kind);
            }
            CustomActionEntity ca = new CustomActionEntity();
            ca.Description = Description;
            ca.Location = Location;
            ca.Group = Group;
            ca.Sequence = Sequence;
            ca.Title = Title;
            ca.Url = Url;
            ca.Rights = new BasePermissions();

            foreach(var permission in Rights)
            {
                ca.Rights.Set(permission);
            }

            this.SelectedWeb.AddCustomAction(ca);
        }
        protected override void ExecuteCmdlet()
        {
            var permissions = new BasePermissions();
            if (Rights != null)
            {
                foreach (var kind in Rights)
                {
                    permissions.Set(kind);
                }
            }

            var ca = new CustomActionEntity
            {
                Name = Name,
                ImageUrl = ImageUrl,
                CommandUIExtension = CommandUIExtension,
                RegistrationId = RegistrationId,
                RegistrationType = RegistrationType,
                Description = Description,
                Location = Location,
                Group = Group,
                Sequence = Sequence,
                Title = Title,
                Url = Url,
                Rights = permissions
            };

            if (Scope == CustomActionScope.Web)
            {
                SelectedWeb.AddCustomAction(ca);
            }
            else
            {
                ClientContext.Site.AddCustomAction(ca);
            }
        }
        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;
            targetAction.Sequence = customAction.Sequence;

            if (customAction.Location == JavaScriptExtensions.SCRIPT_LOCATION)
            {
                targetAction.ScriptBlock = customAction.ScriptBlock;
                targetAction.ScriptSrc = customAction.ScriptSrc;
            }
            else
            {
                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 (!string.IsNullOrEmpty(customAction.CommandUIExtension))
                {
                    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)
        {
            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 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()
                    {
                        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);
                        }
                    }
                }
            }
        }
 /// <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);
 /// web.AddCustomAction(editAction);
 /// </example>
 /// <returns>True if action was successfull</returns>
 public static bool AddCustomAction(this Web web, CustomActionEntity customAction)
 {
     return AddCustomActionImplementation(web, customAction);
 }
 /// <summary>
 /// Adds custom action to a site collection. If the CustomAction exists the item will be updated.
 /// Setting CustomActionEntity.Remove == true will delete the CustomAction.
 /// </summary>
 /// <param name="site">Site collection to be processed</param>
 /// <param name="customAction">Information about the custom action be added or deleted</param>
 /// <returns>True if action was successfull</returns>
 public static bool AddCustomAction(this Site site, CustomActionEntity customAction)
 {
     return AddCustomActionImplementation(site, customAction);
 }
 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;
 }
示例#15
0
        /// <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)
        {
            var existingActions = web.UserCustomActions;
            web.Context.Load(existingActions);
            web.Context.ExecuteQuery();

            var targetAction = web.UserCustomActions.FirstOrDefault(_uca => _uca.Name == customAction.Name);

            if (targetAction == null) 
            {
                targetAction = existingActions.Add();
            }
            else if (customAction.Remove) 
            {
                targetAction.DeleteObject();
                web.Context.ExecuteQuery();
                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();
            web.Context.Load(web, w => w.UserCustomActions);
            web.Context.ExecuteQuery();
            
            return true;
        }
        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 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();
                        }
                    }
                }
            }
        }
示例#18
0
        /// <summary>
        /// Adds a custom Action to a Site Collection
        /// </summary>
        /// <param name="ctx">The Authenticaed client context.</param>
        /// <param name="hostUrl">The Provider hosted URL for the Application</param>
        static void AddCustomAction(ClientContext ctx, string hostUrl)
        {
            var _web = ctx.Web;
            ctx.Load(_web);
            ctx.ExecuteQuery();

            //we only want the action to show up if you have manage web permissions
            BasePermissions _manageWebPermission = new BasePermissions();
            _manageWebPermission.Set(PermissionKind.ManageWeb);

            CustomActionEntity _entity = new CustomActionEntity()
            {
                Group = "SiteTasks",
                Location = "Microsoft.SharePoint.SiteSettings",
                Title = "Site Classification",
                Sequence = 1000,
                Url = string.Format(hostUrl, ctx.Url),
                Rights = _manageWebPermission,
            };

            CustomActionEntity _siteActionSC = new CustomActionEntity()
            {
                Group = "SiteActions",
                Location = "Microsoft.SharePoint.StandardMenu",
                Title = "Site Classification",
                Sequence = 1000,
                Url = string.Format(hostUrl, ctx.Url),
                Rights = _manageWebPermission
            };
            _web.AddCustomAction(_entity);
            _web.AddCustomAction(_siteActionSC);
        }
示例#19
0
        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();
                        }
                    }
                }
            }
        }
示例#20
0
        protected void btnRemoveCustomAction_Click(object sender, EventArgs e)
        {
            //Remove the custom action. Lookup of an existing action is done based on the description and location fields. When an action is 
            //added we always remove the old one and then add a new one. If Remove=true is set then the method bails out after the removal part
            CustomActionEntity customAction = new CustomActionEntity()
            {
                Description = "Shows how to launch an app inside a dialog",
                Location = "Microsoft.SharePoint.StandardMenu",
                Remove = true,
            };
            cc.Web.AddCustomAction(customAction);

        }