Search code examples
c#ektron

Is there a way to manually set the content items on a ContentView templated server control?


The examples I'm seeing for the new ContentView templated server control all use a ContentModelSource server control on the front end. What if I have a method already created that uses the FrameworkAPI and sets all sorts of weird filters in the criteria object and returns a List<ContentData>. Is there a way I can pass that list of content data into my ContentView server control and totally skip having any sort of ContentModelSource control on the page?


Solution

  • I've done a lot of digging into this issue, and I've discovered the following things:

    • It is true that a ContentModelSource is required to use the ContentView server control. You get an ugly .NET exception if you try to go without it.
    • You are not required to actually get the data from that ContentModelSource
    • Data can be set by using the SelectMethod property on the ContentView control. Set it to the name of a public method on your page that returns either ContentData or List<ContentData>.
    • Alternatively, you can wait until the Page_Load event and set the Model.ContentList property. You get a .NET exception (Null Reference, i think) if you try to set it during Page_Init.

    ASPX:

    <ektron:ContentModelSource runat="server" ID="cmsNull"></ektron:ContentModelSource>
    
    <ektron:ContentView runat="server" ID="cvPrimary" ModelSourceID="cmsNull">
    </ektron:ContentView>
    

    C#:

    protected void Page_Load(object sender, EventArgs e)
    {
        var cm = new ContentManager();
    
        var criteria = new ContentCriteria();
        criteria.AddFilter(ContentProperty.Type, CriteriaFilterOperator.EqualTo, EkEnumeration.CMSContentType.Content);
    
        cvPrimary.Model.ContentList = cm.GetList(criteria);
    }