Example #1
0
        public static IEnumerable <T> ToContentItems <T>(this LinkItemCollection links, IContentRepository repo) where T : ContentData
        {
            if (links == null)
            {
                yield break;
            }

            foreach (var link in links)
            {
                var linkUrl = new UrlBuilder(link.Href);

                if (!PermanentLinkMapStore.ToMapped(linkUrl))
                {
                    continue;
                }

                var contentLink = PermanentLinkUtility.GetContentReference(linkUrl);
                var item        = repo.Get <T>(contentLink);

                if (item != null)
                {
                    yield return(item);
                }
            }
        }
        /// <summary>
        ///     Returns ContentReference for provided LinkItem if it is EPiServer page otherwise returns EmptyReference.
        /// </summary>
        /// <param name="source">Source LinkItem for which to return content reference.</param>
        /// <returns>Returns ContentReference for provided LinkItem if it is EPiServer page otherwise returns EmptyReference.</returns>
        public static ContentReference ToContentReference(this LinkItem source)
        {
            var urlBuilder = new UrlBuilder(source.Href);

            return(PermanentLinkMapStore.ToMapped(urlBuilder)
                ? PermanentLinkUtility.GetContentReference(urlBuilder)
                : ContentReference.EmptyReference);
        }
Example #3
0
 /// <summary>
 /// Get a page reference from the url
 /// </summary>
 /// <param name="url"></param>
 /// <returns></returns>
 public static int GetReferenceFromUrl(Url url)
 {
     if (url != null)
     {
         var reference = PermanentLinkUtility.GetContentReference(new UrlBuilder(url.ToString()));
         if (reference != null && reference.ID > 0)
         {
             return(reference.ID);
         }
     }
     return(-1);
 }
        /// <summary>
        /// Prepares all links in a LinkItemCollection for output
        /// by filtering out inaccessible links and ensures all links are correct.
        /// </summary>
        /// <param name="linkItemCollection">The collection of links to prepare.</param>
        /// <param name="targetExternalLinksToNewWindow">True will set target to _blank if target is not specified for the LinkItem.</param>
        /// <returns>A prepared and filtered list of LinkItems</returns>
        public static IEnumerable <LinkItem> ToPreparedLinkItems(this LinkItemCollection linkItemCollection, bool targetExternalLinksToNewWindow)
        {
            var contentLoader = ServiceLocator.Current.GetInstance <IContentLoader>();

            if (linkItemCollection != null)
            {
                foreach (var linkItem in linkItemCollection)
                {
                    var url = new UrlBuilder(linkItem.Href);
                    if (PermanentLinkMapStore.ToMapped(url))
                    {
                        var pr = PermanentLinkUtility.GetContentReference(url);
                        if (!PageReference.IsNullOrEmpty(pr))
                        {
                            // page
                            var page = contentLoader.Get <PageData>(pr);
                            if (IsPageAccessible(page))
                            {
                                linkItem.Href = page.LinkURL;
                                yield return(linkItem);
                            }
                        }
                        else
                        {
                            // document
                            if (IsFileAccessible(linkItem.Href))
                            {
                                Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.Encoding.UTF8);
                                linkItem.Href = url.Path;
                                yield return(linkItem);
                            }
                        }
                    }
                    else if (!linkItem.Href.StartsWith("~"))
                    {
                        // external
                        if (targetExternalLinksToNewWindow && string.IsNullOrEmpty(linkItem.Target))
                        {
                            linkItem.Target = "_blank";
                        }
                        if (linkItem.Href.StartsWith("mailto:") || linkItem.Target == "null")
                        {
                            linkItem.Target = string.Empty;
                        }
                        yield return(linkItem);
                    }
                }
            }
        }
        private static string MapUrlFromRoute(RequestContext requestContext, RouteCollection routeCollection, string url)
        {
            var mappedUrl        = new UrlBuilder(HttpUtility.HtmlDecode(url));
            var contentReference = PermanentLinkUtility.GetContentReference(mappedUrl);

            if (ContentReference.IsNullOrEmpty(contentReference))
            {
                return(mappedUrl.ToString());
            }

            var language = mappedUrl.QueryCollection["epslanguage"] ?? (requestContext.GetLanguage() ?? ContentLanguage.PreferredCulture.Name);

            var  setIdAsQueryParameter = false;
            bool result;

            if (!string.IsNullOrEmpty(requestContext.HttpContext.Request.QueryString["idkeep"]) &&
                !bool.TryParse(requestContext.HttpContext.Request.QueryString["idkeep"], out result))
            {
                setIdAsQueryParameter = result;
            }

            return(routeCollection.GetVirtualPath(contentReference, language, setIdAsQueryParameter, false).GetUrl());
        }
        /// <summary>
        /// Converts a LinkItemCollection to typed pages. Any non-pages will be filtered out. (Not compatible with PageList - Use ToPageDataList)
        /// </summary>
        /// <typeparam name="T">PageType</typeparam>
        /// <param name="linkItemCollection">The collection of links to convert</param>
        /// <returns>An enumerable of typed PageData</returns>
        public static IEnumerable <T> ToPages <T>(this LinkItemCollection linkItemCollection) where T : PageData
        {
            var contentLoader = ServiceLocator.Current.GetInstance <IContentLoader>();

            if (linkItemCollection != null)
            {
                foreach (var linkItem in linkItemCollection)
                {
                    var url = new UrlBuilder(linkItem.Href);
                    if (PermanentLinkMapStore.ToMapped(url))
                    {
                        var pr = PermanentLinkUtility.GetContentReference(url);
                        if (!PageReference.IsNullOrEmpty(pr))
                        {
                            var page = contentLoader.Get <PageData>(pr);
                            if (page is T && IsPageAccessible(page))
                            {
                                yield return((T)page);
                            }
                        }
                    }
                }
            }
        }