Esempio n. 1
0
        public static ItemInfo FromItem(Item item, IEnumerable <string> websites = null, int?maxDepth = null, Language language = null)
        {
            var info = new ItemInfo();

            info.Id          = item.ID.Guid;
            info.Name        = item.Name;
            info.DisplayName = item.DisplayName;
            info.Version     = item.Version.Number;
            info.Language    = item.Language.Name;
            info.Path        = item.Paths.FullPath;
            info.ParentId    = item.ParentID.Guid;

            info.TemplateId   = item.TemplateID.Guid;
            info.TemplateName = info.TemplateName;

            info.HasLayout = item.Fields[FieldIDs.LayoutField] != null &&
                             !string.IsNullOrEmpty(item.Fields[FieldIDs.LayoutField].Value);

            foreach (Field field in item.Fields)
            {
                info.Fields[field.Name] = field.Value;
            }

            if (!maxDepth.HasValue || maxDepth > 0)
            {
                info.Children = item.Children.Select(child => FromItem(child, websites, maxDepth.HasValue ? maxDepth - 1 : null)).ToList();
            }

            if (websites != null)
            {
                var current = Context.GetSiteName();
                foreach (var website in websites)
                {
                    Context.SetActiveSite(website);
                    var options = LinkManager.GetDefaultUrlBuilderOptions();
                    options.AlwaysIncludeServerUrl = true;
                    if (language != null)
                    {
                        options.Language = language;
                    }

                    info.SiteUrls[website] = new ItemUrl
                    {
                        Url           = LinkManager.GetItemUrl(item, options),
                        InSiteContext = item.Paths.FullPath.StartsWith(Context.Site.RootPath + Context.Site.StartItem)
                    };
                }
                if (!string.IsNullOrEmpty(current))
                {
                    Context.SetActiveSite(current);
                }
            }


            return(info);
        }
Esempio n. 2
0
        public void Process(GetLayoutServiceContextArgs args)
        {
            Assert.ArgumentNotNull((object)args, nameof(args));
            var options = LinkManager.GetDefaultUrlBuilderOptions();

            options.AlwaysIncludeServerUrl = true;
            string link = LinkManager.GetItemUrl(Context.Item, options);

            args.ContextData.Add(Key, link);
        }
        private static string GetItemUrl(Item landingPageItem)
        {
            var options = LinkManager.GetDefaultUrlBuilderOptions();

            options.AlwaysIncludeServerUrl = true;

            var uri = new Uri(LinkManager.GetItemUrl(landingPageItem, options));

            return(uri.PathAndQuery);
        }
Esempio n. 4
0
        private static string GetItemUrl(Item item, SiteInfo site)
        {
            using (new SiteContextSwitcher(SiteContext.GetSite(site.Name)))
            {
                var urlOptions = LinkManager.GetDefaultUrlBuilderOptions();

                urlOptions.AlwaysIncludeServerUrl = true;
                urlOptions.ShortenUrls            = true;
                urlOptions.SiteResolving          = true;

                return(LinkManager.GetItemUrl(item, urlOptions));
            }
        }
        private void CheckForRegExMatch(Database db, string requestedUrl, string requestedPathAndQuery, HttpRequestArgs args)
        {
            // Loop through the pattern match items to find a match
            foreach (Item possibleRedirectPattern in GetRedirects(db, Constants.Templates.RedirectPattern, Constants.Templates.VersionedRedirectPattern, Sitecore.Configuration.Settings.GetSetting(Constants.Settings.QueryExactMatch)))
            {
                var redirectPath = string.Empty;
                if (Regex.IsMatch(requestedUrl, possibleRedirectPattern[Constants.Fields.RequestedExpression], RegexOptions.IgnoreCase))
                {
                    redirectPath = Regex.Replace(requestedUrl, possibleRedirectPattern[Constants.Fields.RequestedExpression],
                                                 possibleRedirectPattern[Constants.Fields.SourceItem], RegexOptions.IgnoreCase);
                }
                else if (Regex.IsMatch(requestedPathAndQuery, possibleRedirectPattern[Constants.Fields.RequestedExpression], RegexOptions.IgnoreCase))
                {
                    redirectPath = Regex.Replace(requestedPathAndQuery,
                                                 possibleRedirectPattern[Constants.Fields.RequestedExpression],
                                                 possibleRedirectPattern[Constants.Fields.SourceItem], RegexOptions.IgnoreCase);
                }
                if (string.IsNullOrEmpty(redirectPath))
                {
                    continue;
                }

                // Query portion gets in the way of getting the sitecore item.
                var pathAndQuery   = redirectPath.Split('?');
                var path           = pathAndQuery[0];
                var redirectToItem = db.GetItem(path);
                if (redirectToItem == null)
                {
                    if (LinkManager.GetDefaultUrlBuilderOptions() != null && (bool)LinkManager.GetDefaultUrlBuilderOptions().EncodeNames)
                    {
                        path = Sitecore.MainUtil.DecodeName(path);
                    }
                    redirectToItem = db.GetItem(path);
                }
                if (redirectToItem != null)
                {
                    var query          = pathAndQuery.Length > 1 ? "?" + pathAndQuery[1] : "";
                    var responseStatus = GetResponseStatus(possibleRedirectPattern);

                    SendResponse(redirectToItem, query, responseStatus, args);
                }
            }
        }