Try our conversational search powered by Generative AI!

Get page properties from LinkItemCollection

Vote:
 

I have a block type that has a LinkItemCollection property, with which I want to render a slider. Each slider item should render header, image and body text from the pages that the link items point to.

In order to do that I have to convert the link items to pages, so I tried the snippet from Joel Abrahamsson (http://joelabrahamsson.com/convert-a-linkitemcollection-to-a-list-of-pagedata/), but I always get an empty page/content reference. The same error occurs when I use PermanentLinkUtility instead:

public static IEnumerable ToPages(this LinkItemCollection linkItemCollection)
		{
			var pages = new List();

			if (linkItemCollection != null)
			{
				foreach (var linkItem in linkItemCollection)
				{
					var urlBuilder = new UrlBuilder(linkItem.Href);

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

					var contentReference = PermanentLinkUtility.GetContentReference(urlBuilder);
					if (ContentReference.IsNullOrEmpty(contentReference))
					{
						continue;
					}

					var page = ServiceLocator.Current.GetInstance().Get(contentReference);
					pages.Add(page);
				}
			}

			return pages;
		}

The links all have the format "/link/81242f7222424dc488f30bcdb9734c30.aspx", don't know if that has anything to do with the error?

#121485
May 12, 2015 11:40
Vote:
 

Hi, William,

I use PageReference.ParseUrl instead of PermanentLinkUtility.GetContentReference(), try out the following code:

public static List<PageData> ToPages(this LinkItemCollection linkItemCollection)
        {

            var pages = new List<PageData>();

            if (linkItemCollection != null)
            {
                foreach (var linkItem in linkItemCollection)
                {
                    string linkUrl;
                    if (!PermanentLinkMapStore.TryToMapped(linkItem.Href, out linkUrl))
                        continue;

                    if (String.IsNullOrEmpty(linkUrl))
                        continue;

                    var pageReference = PageReference.ParseUrl(linkUrl);

                    if (PageReference.IsNullOrEmpty(pageReference))
                        continue;

                    var page = DataFactory.Instance.GetPage(pageReference, LanguageSelector.AutoDetect());
                    if (page != null)
                    {
                        pages.Add(page);
                    }
                }
            }

            return pages;
        }

Let me know if this doesn't work.

#121489
May 12, 2015 13:48
Vote:
 

Hi Marija,

Thanks for your suggestion, unfortunately I still get the same error. Which version of EPi are you using that snippet for?

#121493
May 12, 2015 14:04
Vote:
 

It's 7.19, which one are you using?

I'll give it another check on latest...

#121494
May 12, 2015 14:05
Vote:
 

I'm using EPiServer 8 (8.3 I think), could it be some breaking change?

#121495
May 12, 2015 14:09
Vote:
 

Give this a try:

        public static List<PageData> ToPages(this LinkItemCollection linkItemCollection)
        {

            var pages = new List<PageData>();

            if (linkItemCollection != null)
            {
                foreach (var linkItem in linkItemCollection)
                {
                    if (String.IsNullOrEmpty(linkItem.Href))
                        continue;

                    string linkUrl;
                    if (PermanentLinkMapStore.TryToMapped(linkItem.Href, out linkUrl))
                    {
                        var urlBuilder = new UrlBuilder(linkUrl);
                        var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();

                        var page = urlResolver.Route(urlBuilder) as PageData;

                        if (page != null)
                        {
                            pages.Add(page);
                        }
                    }
                }
            }

            return pages;
        }

For me it works, tested on latest version. Make sure to test it in edit mode as well. // UPDATE: works well for me in edit mode as well, briefly tested it...

#121496
Edited, May 12, 2015 14:16
Vote:
 

Might be good not to call var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>(); in foreach :)

#121497
May 12, 2015 14:18
Vote:
 

That did the trick, thanks! :)

Yes, I will inject it instead, used ServiceLocator in my example merely as a convenience.

#121498
May 12, 2015 14:23
Vote:
 

Great Marija ! Worked for me too! 

#131358
Jul 22, 2015 9:35
Vote:
 
#131359
Jul 22, 2015 9:41
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.