I try to use
var pageBase = HttpContext.Current.Handler as EPiServer.PageBase;
(In TODO) but it return null.
Are you using MVC or WebForms?
In MVC it does not work with HttpContext.Current.Handler
as
EPiServer.PageBase
Then Andrey's answer should be quite good:
var pageBase = HttpContext.Current.Handler as EPiServer.PageBase;
if(pageBase != null) {
var currentPage = pageBase.CurrentPage;
}
public class MyPageSelectionFactory : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
var pageBase = HttpContext.Current.Handler as EPiServer.PageBase; // it return null
}
}
Any idea how to get page which load myblock at moment ?
Sorry I missed that part that you wanted to find it in your SelectionFactory.
I don't think you're supposed to base your selection on surroundings like that.
Specially if it's a Shared Block since they can be placed on any Page and multiple Pages.
What are you trying to accomplish?
So my task is:
1. Create block with dynamic dropdown property.
I found how to create dropdown property http://world.episerver.com/Blogs/Linus-Ekstrom/Dates/2012/9/EPiServer-7-Configuring-editors-for-your-properties/
now I need to implement next functionality:
I use this block on two page (for example: on Article page and on News page)
When I go in edit view on article page I need that dropdown property has some value.
When I go in edit view on news page I need that dropdown property use another value (different that in article)
As you see dropdown property values has depends which page it use.
I thought it would be good to do it when the data is generated for that dropdown property.
So I in edit view on article page - generate data which need for article page
if I in edit view on news page - generate data which needs for news page. (generate data - values for dropdown property).
that's my goal.
You mean that the data should be different depending on which Page Type the Page is? Not depending on any value on the Page itself?
yes, but in my case, I have category property in article page and category property in news page, They also play a role in generate data for dropdown property in block.
there is also a dependence on values of the page
For example How I thought about implementation it:
public class MyPageSelectionFactory : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
var pageBase = HttpContext.Current.Handler as EPiServer.PageBase;
var pageType = typeof (page.CurrentPage);
if (page == ArticlePage.GetType())
{
var property = ((ArticlePage) page.CurrentPage).SomeProperty;
// generate data for dropdown
}
if (page == NewsPage.GetType())
{
var property = ((NewsPage) page.CurrentPage).SomeProperty;
// generate data for dropdown
}
}
}
That would work when you are visiting a page, not when you are in Edit Mode.
The HttpHandler something else in edit mode.
Try to find some inspiration on http://joelabrahamsson.com/limiting-content-and-page-reference-properties-to-values-of-a-specific-type-in-episerver-cms/
the tricky part would be to add information from the page to the block which in turn can tell the attribute.
Thanks Alf. I found this article http://joelabrahamsson.com/hiding-episervers-standard-category-property/.
So I use this code in
public class MyPageSelectionFactory : ISelectionFactory
{
public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
dynamic contentMetadata = metadata;
var ownerContent = contentMetadata.OwnerContent as IContent;
var ownerType = ownerContent.GetType().BaseType;
if (page == ArticlePage.GetType())
{
// generate data for dropdown
}
if (page == NewsPage.GetType())
{
// generate data for dropdown
}
}
}
It works in edit mode.
Hi all,
My block:
How I can get current page which call myblock (In TODO)?I need it because I want to set different information in dropdown property.