Ejemplo n.º 1
0
        public Configurations GetConfigurations(ClientContext clientContext, out UserCustomActionCollection customActions)
        {
            Configurations configurations = new Configurations();

            customActions = clientContext.Site.UserCustomActions;
            clientContext.Load(customActions);
            clientContext.ExecuteQuery();

            configurations.ScriptLinks = new List<ScriptLinkAction>();
            configurations.CSSLinks = new List<ScriptLinkAction>();

            foreach (UserCustomAction customAction in customActions)
            {
                if (customAction.Title == HEADER)
                {
                   configurations.HeaderScripts = customAction.ScriptBlock;
                }
                else if (customAction.Title != LOAD_SCRIPT)
                {
                    if (customAction.Title.StartsWith(GENESIS_CSS) &&
                         !String.IsNullOrEmpty(customAction.ScriptBlock))
                    {

                        string scriptSrc = customAction.ScriptBlock.Split(new string[] { "LoadCSS('" }, StringSplitOptions.None)[1];
                        string exclude = "";

                        if (scriptSrc.Contains(","))
                        {
                            //means we have an Exclude
                            var scriptScrSplit = scriptSrc.Split(new string[] { "'," }, StringSplitOptions.None);
                            scriptSrc = scriptScrSplit[0];
                            exclude = scriptScrSplit[1].Split(new string[] { "');" }, StringSplitOptions.None)[0].Replace("'", "");
                        }
                        else
                        {
                            scriptSrc = scriptSrc.Split(new string[] { "');" }, StringSplitOptions.None)[0];
                        }

                        configurations.CSSLinks.Add(new ScriptLinkAction
                        {
                            Id = customAction.Id.ToString(),
                            Title = customAction.Title.Replace(GENESIS_CSS, ""),
                            ScriptSrc = scriptSrc,
                            Excludes = exclude,
                            Sequence = customAction.Sequence.ToString(),
                            Type = CSS
                        });
                    }
                    else if (customAction.Title.StartsWith(GENESIS_JS) &&
                            !String.IsNullOrEmpty(customAction.ScriptBlock))
                    {

                        string scriptSrc = customAction.ScriptBlock.Split(new string[] { "LoadScript('" }, StringSplitOptions.None)[1];
                        string exclude = "";
                        string dependency = "";
                        var useSod = false;

                        if (scriptSrc.Contains(","))
                        {
                            //means we have an Exclude and/or a Dependency
                            var scriptScrSplit = scriptSrc.Split(new string[] { "'," }, StringSplitOptions.None);
                            scriptSrc = scriptScrSplit[0];

                            if (scriptScrSplit.Length == 2) {
                                exclude = scriptScrSplit[1].Split(new string[] { "');" }, StringSplitOptions.None)[0].Replace("'", "");
                            } else if (scriptScrSplit.Length == 3) {
                                exclude = scriptScrSplit[1].Replace("'", "");
                                dependency = scriptScrSplit[2].Split(new string[] { "');" }, StringSplitOptions.None)[0].Replace("'", "");
                            } else if (scriptScrSplit.Length == 5)
                            {
                                exclude = scriptScrSplit[1].Replace("'", "");
                                dependency = scriptScrSplit[2].Replace("'", "");
                                var title = scriptScrSplit[3].Replace("'", "");
                                var sod = scriptScrSplit[4].Split(new string[] { ");" }, StringSplitOptions.None)[0];
                                useSod = sod == "true";
                            }
                        }
                        else
                        {
                            scriptSrc = scriptSrc.Split(new string[] { "');" }, StringSplitOptions.None)[0];
                        }

                        configurations.ScriptLinks.Add(new ScriptLinkAction
                        {
                            Id = customAction.Id.ToString(),
                            Title = customAction.Title.Replace(GENESIS_JS, ""),
                            ScriptSrc = scriptSrc,
                            SOD = useSod,
                            Excludes = exclude,
                            Dependency = dependency,
                            Sequence = customAction.Sequence.ToString(),
                            Type = JS
                        });
                    }
                }
            }

            return configurations;
        }
Ejemplo n.º 2
0
        public dynamic GetConfigurations([FromBody] dynamic config)
        {
            try
            {
                Configurations configurations = new Configurations();
                using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(config["SPHostUrl"].ToString(), config["SPAppToken"].ToString()))
                {
                    UserCustomActionCollection customActions = clientContext.Site.UserCustomActions;
                    configurations = GetConfigurations(clientContext,  out customActions);
                }

                return Request.CreateResponse(HttpStatusCode.OK, configurations);
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, APIHelper.WriteErrorInfo(ex));
            }
        }