Ejemplo n.º 1
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);
        }
        protected override void ExecuteCmdlet()
        {
            CustomActionEntity ca = new CustomActionEntity
            {
                Title    = Title,
                Location = "ClientSideExtension.ApplicationCustomizer",
                ClientSideComponentId         = ClientSideComponentId,
                ClientSideComponentProperties = ClientSideComponentProperties,
                ClientSideHostProperties      = ClientSideHostProperties
            };

            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;
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
0
        private static bool DeleteJsLinkImplementation(ClientObject clientObject, string key)
        {
            bool ret;

            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);
        }
Ejemplo n.º 5
0
        private static bool AddJsBlockImplementation(ClientObject clientObject, string key, string scriptBlock, int sequence)
        {
            bool ret;

            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);
        }
Ejemplo n.º 6
0
        protected override void ExecuteCmdlet()
        {
            var permissions = new BasePermissions();

            if (Rights != null)
            {
                foreach (var kind in Rights)
                {
                    permissions.Set(kind);
                }
            }
            CustomActionEntity ca = null;

            if (ParameterSetName == "Default")
            {
                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,
                };
            }
            else
            {
#if !ONPREMISES
                ca = new CustomActionEntity()
                {
                    Name     = Name,
                    Title    = Title,
                    Location = Location,
                    ClientSideComponentId         = ClientSideComponentId.Id,
                    ClientSideComponentProperties = ClientSideComponentProperties
                };
#endif
            }

            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;
            }
        }
Ejemplo n.º 7
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);
                    }
                }
            }
        }
Ejemplo n.º 8
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;
                targetAction.RegistrationId     = customAction.RegistrationId;
                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);
        }
Ejemplo n.º 9
0
        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 !ONPREMISES
            if (ClientSideComponentId.Id != Guid.Empty)
            {
                ca.ClientSideComponentId = ClientSideComponentId.Id;
            }
            if (!string.IsNullOrEmpty(ClientSideComponentProperties))
            {
                ca.ClientSideComponentProperties = ClientSideComponentProperties;
            }
#endif

            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;
            }
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
        public static void AddSPFXExtension()
        {
            using (binderSiteClientContext = new ClientContext(binderSiteUrl))
            {
                binderSiteClientContext.Credentials = new SharePointOnlineCredentials("*****@*****.**", passWord);
                
                //Guid spfxExtension_GlobalHeaderID = new Guid("1e3d3ef7-0983-4d40-9dbb-9c6d4539639a");
                //string spfxExtName = "react-logo-festoon-client-side-solution";
                //string spfxExtTitle = "LogoFestoonApplicationCustomizer";

                //string spfxExtDescription = "Logo Festoon Application Customizer";
                //string spfxExtLocation = "ClientSideExtension.ApplicationCustomizer";
                ////string spfxExtProps = "";  // add properties if any, else remove this

                //UserCustomAction userCustomAction = binderSiteClientContext.Site.UserCustomActions.Add();
                //userCustomAction.Name = spfxExtName;
                //userCustomAction.Title = spfxExtTitle;
                //userCustomAction.Description = spfxExtDescription;
                //userCustomAction.Location = spfxExtLocation;
                //userCustomAction.ClientSideComponentId = spfxExtension_GlobalHeaderID;
                ////userCustomAction.ClientSideComponentProperties = spfxExtProps;

                //binderSiteClientContext.ExecuteQuery();
                //using (binderSiteClientContext = new ClientContext(binderSiteUrl))
                //{

                    var appManager = new AppManager(binderSiteClientContext); 
                    var apps = appManager.GetAvailable(); 
                    var chartsApp = apps.Where(a => a.Title == "react-logo-festoon-client-side-solution").FirstOrDefault(); 
                    var installApp = appManager.Install(chartsApp);
                    if (installApp)
                    {
                        Guid spfxExtension_GlobalHeaderID1 = chartsApp.Id; 
                        string spfxExtName1 = chartsApp.Title; 
                        string spfxExtTitle1 = chartsApp.Title; 
                        //string spfxExtGroup1 = ""; 
                        string spfxExtDescription1= "Logo Festoon Application Customizer"; 
                        string spfxExtLocation1 = "ClientSideExtension.ApplicationCustomizer";
                        CustomActionEntity ca = new CustomActionEntity
                        {
                            Name = spfxExtName1,
                            Title = spfxExtTitle1,
                            //Group = spfxExtGroup1,
                            Description = spfxExtDescription1,
                            Location = spfxExtLocation1,
                            ClientSideComponentId = spfxExtension_GlobalHeaderID1
                        };

                        binderSiteClientContext.Web.AddCustomAction(ca);
                        binderSiteClientContext.ExecuteQueryRetry();
                    }
                }
            }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
        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;
            }
        }
Ejemplo n.º 16
0
        public static void AddCustomActionToGearMenu(ClientContext ctx)
        {
            CustomActionEntity caSiteActions = new CustomActionEntity();

            // see https://msdn.microsoft.com/en-us/library/office/bb802730.aspx for locations and groups
            caSiteActions.Location = "Microsoft.SharePoint.StandardMenu";
            caSiteActions.Group    = "SiteActions";
            caSiteActions.Title    = "Press me";
            caSiteActions.Url      = "javascript:alert('yo')";
            // caSiteActions.ScriptBlock = "<script>alert('yo')</script>";
            caSiteActions.Sequence = 50;
            caSiteActions.Name     = "clickmeAction";
            BasePermissions bp = new BasePermissions();

            bp.Set(PermissionKind.ManageWeb); // user can admin site
            caSiteActions.Rights = bp;

            ctx.Web.AddCustomAction(caSiteActions);
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
0
        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);
            }
        }
Ejemplo n.º 20
0
 public static bool AddCustomAction(this Site site, CustomActionEntity customAction)
 {
     return AddCustomActionImplementation(site, customAction);
 }
Ejemplo n.º 21
0
        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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 22
0
 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;
 }
Ejemplo n.º 23
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);
                    }
                }
            }
        }
Ejemplo n.º 24
0
        private static bool AddJsLinkImplementation(ClientObject clientObject, string key, IEnumerable <string> scriptLinks, int sequence)
        {
            bool ret;

            if (clientObject is Web || clientObject is Site)
            {
                var scriptLinksEnumerable = scriptLinks as string[] ?? scriptLinks.ToArray();
                if (!scriptLinksEnumerable.Any())
                {
                    throw new ArgumentException(nameof(scriptLinks));
                }

                if (scriptLinksEnumerable.Length == 1)
                {
                    var scriptSrc = scriptLinksEnumerable[0];
                    if (!scriptSrc.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var serverUri = new Uri(clientObject.Context.Url);
                        if (scriptSrc.StartsWith("/"))
                        {
                            scriptSrc = $"{serverUri.Scheme}://{serverUri.Authority}{scriptSrc}";
                        }
                        else
                        {
                            var serverRelativeUrl = string.Empty;
                            if (clientObject is Web)
                            {
                                serverRelativeUrl = ((Web)clientObject).EnsureProperty(w => w.ServerRelativeUrl);
                            }
                            else
                            {
                                serverRelativeUrl = ((Site)clientObject).RootWeb.EnsureProperty(w => w.ServerRelativeUrl);
                            }
                            scriptSrc = $"{serverUri.Scheme}://{serverUri.Authority}{serverRelativeUrl}/{scriptSrc}";
                        }
                    }

                    var customAction = new CustomActionEntity
                    {
                        Name      = key,
                        ScriptSrc = scriptSrc,
                        Location  = SCRIPT_LOCATION,
                        Sequence  = sequence
                    };
                    if (clientObject is Web)
                    {
                        ret = ((Web)clientObject).AddCustomAction(customAction);
                    }
                    else
                    {
                        ret = ((Site)clientObject).AddCustomAction(customAction);
                    }
                }
                else
                {
                    var scripts = new StringBuilder(@" var headID = document.getElementsByTagName('head')[0]; 
var scripts = document.getElementsByTagName('script');
var scriptsSrc = [];
for(var i = 0; i < scripts.length; i++) {
    if(scripts[i].type === 'text/javascript'){
        scriptsSrc.push(scripts[i].src);
    }
}
");
                    foreach (var link in scriptLinksEnumerable)
                    {
                        if (!string.IsNullOrEmpty(link))
                        {
                            scripts.Append(@"
if (scriptsSrc.indexOf('{1}') === -1)  {  
    var newScript = document.createElement('script');
    newScript.id = '{0}';
    newScript.type = 'text/javascript';
    newScript.src = '{1}';
    headID.appendChild(newScript);
    scriptsSrc.push('{1}');
}".Replace("{0}", key).Replace("{1}", link));
                        }
                    }

                    ret = AddJsBlockImplementation(clientObject, key, scripts.ToString(), sequence);
                }
            }
            else
            {
                throw new ArgumentException("Only Site or Web supported as clientObject");
            }
            return(ret);
        }
Ejemplo n.º 25
0
        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;
        }
Ejemplo n.º 26
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)
 {
     return(AddCustomActionImplementation(web, customAction));
 }
Ejemplo n.º 27
0
        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);
        }
Ejemplo n.º 28
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)
 {
     return AddCustomActionImplementation(web, customAction);
 }
Ejemplo n.º 29
0
 public static bool AddCustomAction(this Site site, CustomActionEntity customAction)
 {
     return(AddCustomActionImplementation(site, customAction));
 }
Ejemplo n.º 30
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();
                        }
                    }
                }
            }
        }
        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();
                        }
                    }
                }
            }
        }
Ejemplo n.º 32
0
        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;
        }