Пример #1
0
        private ScriptLinkAction SaveCustomActionScriptLink(ClientContext clientContext, ScriptLinkAction scriptLinkAction)
        {
            RegisterHelpers(clientContext);

            UserCustomActionCollection customActions = clientContext.Site.UserCustomActions;

            UserCustomAction customAction = null;
            Guid id;

            if (!scriptLinkAction.IsNew)
            {
                //update
                id = new Guid(scriptLinkAction.Id);
                clientContext.Load(customActions);
                clientContext.ExecuteQuery();
                customAction = customActions.FirstOrDefault(x => x.Id.Equals(id));
            }
            else
            {
                //insert
                id = Guid.NewGuid();
                customAction = customActions.Add();
            }

            customAction.Location = "ScriptLink";
            customAction.Title = scriptLinkAction.Title;

            if(scriptLinkAction.Type == JS) {
                var excludes = !string.IsNullOrEmpty(scriptLinkAction.Excludes) ? scriptLinkAction.Excludes : "";
                var dependency = !string.IsNullOrEmpty(scriptLinkAction.Dependency) ? scriptLinkAction.Dependency : "";

                customAction.ScriptBlock = "LoadScript('" + scriptLinkAction.ScriptSrc + "','" + excludes + "','" +  dependency + "','" +customAction.Title + "'," + scriptLinkAction.SOD.ToString().ToLower() + ");";
                customAction.Title = GENESIS_JS + customAction.Title;
            }else {
                var excludes = !string.IsNullOrEmpty(scriptLinkAction.Excludes) ? scriptLinkAction.Excludes : "";

                customAction.ScriptBlock = "LoadCSS('" + scriptLinkAction.ScriptSrc + "','" + excludes + "');";
                customAction.Title = GENESIS_CSS + customAction.Title;
            }

            int sequenceNum = 0;
            int.TryParse(scriptLinkAction.Sequence, out sequenceNum);
            customAction.Sequence = sequenceNum;

            if (sequenceNum == 0) {
                if (!scriptLinkAction.SOD) {
                    sequenceNum = 1000; //make sure our custom scripts run after the SOD registrations
                }
            }

            customAction.Update();

            clientContext.Load(customAction);
            clientContext.ExecuteQuery();

            return new ScriptLinkAction {
                Title = customAction.Title,
                ScriptSrc = customAction.ScriptSrc,
                Sequence = customAction.Sequence.ToString(),
                Id = customAction.Id.ToString()
            };
        }
Пример #2
0
        private void DeleteCustomActionScriptLink(ClientContext clientContext, ScriptLinkAction scriptLink)
        {
            UserCustomActionCollection customActions = clientContext.Site.UserCustomActions;
            clientContext.Load(customActions);
            clientContext.ExecuteQuery();

            var headerScriptAction = customActions.FirstOrDefault(x => x.Id.Equals(new Guid(scriptLink.Id)));

            if (headerScriptAction != null)
            {
                headerScriptAction.DeleteObject();
            }

            clientContext.Load(headerScriptAction);
            clientContext.ExecuteQuery();
        }