Try our conversational search powered by Generative AI!

Get current page from block

Vote:
 

Hi all,

My block:

    public class MyBlock : BlockData
    {
        [EditorDescriptor(EditorDescriptorType = typeof(MyBlockPageEditorDescriptor))]
        public virtual string Test { get; set; }

    }

    [EditorDescriptorRegistration(TargetType = typeof(int), UIHint = "TestPageMultiple")]
    public class MyPageEditorDescriptor : EditorDescriptor
    {
        public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
        {
            SelectionFactoryType = typeof(MyPageSelectionFactory);
            ClientEditingClass = "epi.cms.contentediting.editors.SelectionEditor";
            base.ModifyMetadata(metadata, attributes);
        }
    }

    public class MyPageSelectionFactory : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            ///TODO 
        }
    }

How I can get current page which call myblock (In TODO)?I need it because I want to set different information in dropdown property.

#72677
Jun 26, 2013 14:48
Vote:
 

I try to use 

 var pageBase = HttpContext.Current.Handler as EPiServer.PageBase;

(In TODO) but it return null. 

#72679
Edited, Jun 26, 2013 15:16
Vote:
 

Are you using MVC or WebForms?

In MVC it does not work with HttpContext.Current.Handler as EPiServer.PageBase

#72681
Jun 26, 2013 15:53
Vote:
 

I using WebForms. 

#72699
Jun 26, 2013 16:33
Vote:
 

Then Andrey's answer should be quite good:

 

var pageBase = HttpContext.Current.Handler as EPiServer.PageBase;
if(pageBase != null) {
    var currentPage = pageBase.CurrentPage;
}

    

#72700
Jun 26, 2013 16:35
Vote:
 
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 ?

#72701
Jun 26, 2013 16:41
Vote:
 

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?

#72703
Edited, Jun 26, 2013 16:48
Vote:
 

 

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.

 

#72783
Edited, Jun 28, 2013 16:59
Vote:
 

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?

#72784
Jun 28, 2013 17:02
Vote:
 

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

#72787
Edited, Jun 28, 2013 17:46
Vote:
 

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
                }
            }
        }

    

#72788
Edited, Jun 28, 2013 17:56
Vote:
 

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.

#72789
Jun 28, 2013 18:06
Vote:
 

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.
#72800
Edited, Jun 30, 2013 16:14
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.