Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Getting block data from ContentRepository

Vote:
 

I've inherented an Episerver website, making me a newbie once again.  My current task is to move a slider type content area off of a page template into a "block with blocks" configuration, in order to have more granular content scheduling for that page.

I'm stuck trying to move the property that points to the page data:

protected HomePage HomePageData
{
get
{
return ContentRepository.Get(PageReference.StartPage);
}
}

Into something that points to the containing block data, for example:

protected UpcomingEventsBlock UpcomingEventsData
{
get
{
return ContentRepository.Get(contentLink);
}
}

Thanks,

Mark

#145144
Feb 25, 2016 18:43
Vote:
 

Oops, here's the code formatted: 

        protected HomePage HomePageData
        {
            get
            {
                return ContentRepository.Get<HomePage>(PageReference.StartPage);
            }
        }

To something like:

        protected UpcomingEventsBlock UpcomingEventsData
        {
            get
            {  
                return ContentRepository.Get<UpcomingEventsBlock>(contentLink);
            }
        }
#145146
Feb 25, 2016 20:04
Vote:
 

What is the question?

#145153
Feb 25, 2016 21:28
Vote:
 

Sorry Bob, I realize I've not been clear on that.

I've not been able to find the correct parameter for the ContentRepository.Get method.  There is no Block equivalent to the PageReference class, that I can find.  I've tried a large number of variations but I'm mostly flailing in the dark.  

If you could point me to relevant documentation or examples that would be a huge help.   

Thanks

#145157
Feb 25, 2016 22:06
Vote:
 

Try ContentReference, which PageReference inherits from.

#145158
Feb 25, 2016 22:15
Vote:
 

Thanks Johan, that sounds good, but I was not able to get it to work with any of ContentReference's methods, such as StartPage or RootPage.

This makes me realize that the original home page code is not a good model to follow. PageReference.StartPage, as far as I understand it, is a global kind of reference, a 'hard coded' pointer to the home page.  The code I'm trying to move over to a block is doing a data bind for the child blocks:

 rptSlider.ItemDataBound += BindEvents;

            if (UpcomingEventsData.ThisWeekEvents != null && UpcomingEventsData.ThisWeekEvents.Count > 0)
            {
                var allEvents = UpcomingEventsData.ThisWeekEvents.FilteredItems
                    .Select(content => ContentRepository.Get<HomePageEventBlock>(content.ContentLink))
                    .Cast<IFeaturedEvent>().ToArray();
                var groupings = new List<FeaturedEventGrouping>();
                var curGrouping = new FeaturedEventGrouping();

                for (int curIndex = 0; curIndex < allEvents.Count(); curIndex++)
                {
                    var curEvent = allEvents[curIndex];

                    curGrouping.Events.Add(curEvent);

                    if (curGrouping.IsFull || curIndex == allEvents.Count() - 1)
                    {
                        groupings.Add(curGrouping);
                        curGrouping = new FeaturedEventGrouping();
                    }
                }

                rptSlider.DataSource = groupings;
                rptSlider.DataBind();
            }

 

Where the UpcomingEventsData property is pointing to the container block.  

#145159
Feb 25, 2016 22:37
Vote:
 

Found it!  Thanks to Dan Matthews and Alf Nilsson The key was in this thread: 

http://world.episerver.com/Forum/Developer-forum/EPiServer-7-CMS/Thread-Container/2013/7/Block-ContentReference-ID/

"If you cast CurrentBlock to IContent you will be able to access data from the IContent Interface."

So now I have:

        protected UpcomingEventsBlock UpcomingEventsData
        {
            get
            {
                var contentLink = (CurrentBlock as IContent).ContentLink;
                return ContentRepository.Get<UpcomingEventsBlock>(contentLink);
            }
        }

And it works like a charm.  

#145187
Feb 26, 2016 18:26
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.