Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

SetDefaultValues for PageType property

A.D
A.D
Vote:
 

Not sure how to go about setting a default value for a PageType property

For example:

[ContentType(DisplayName = "Article Page")]
public class Article : PageData
{
    public virtual string Heading { get; set; }
    public virtual XhtmlString MainBody { get; set; }
public virtual PageType PageTypeFilter { get; set; }

    public override void SetDefaultValues(ContentType pageType)
    {
        base.SetDefaultValues(pageType);

        Heading = "My awesome article";
        MainBody = new XhtmlString(" Enter your article text here");
PageTypeFilter = not sure how to set this
    }
}

The PageType property provides drop down list of all the page types.

How do you have one selected by default, or force the dropdown to only show a subset of pagetypes/

Thanks

#145040
Feb 24, 2016 3:50
Vote:
 

You can always use a selection factory to control the options. 

http://world.episerver.com/blogs/Linus-Ekstrom/Dates/2013/12/SingleMultiple-selection-in-EPiServer-75/

#145044
Feb 24, 2016 7:02
Vote:
 

If you do that one you get full control of your drop-down but a little more work. You can use 

var repository = ServiceLocator.Current.GetInstance<PageTypeRepository>();

Use the list method to generate the options 

http://world.episerver.com/documentation/class-library/?documentId=cms/7/083daf2b-a239-d900-4d3d-3bfb250b79a8

Then either set default value like you do or set it in factory. It's hidden in the metadata object and called initial value.

#145046
Feb 24, 2016 7:28
A.D
Vote:
 

Great, thanks Daniel. Yes, this works:

            var repository = ServiceLocator.Current.GetInstance<PageTypeRepository>();
            PageTypeFilter = repository.Load<MyPageType>();

I guess you could also use the List method then use Linq to filter it,

Cheers,

#145163
Feb 26, 2016 6:04
* 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.