Sitecore 5.3: Using childlists as datasource

Did you know that you can use a Sitecore item’s Children property as a datasource? Oh yes you can…
Here is a small example. First I set up a DataGrid:

<asp:gridview id="GridView" runat="server" autogeneratecolumns="false" showheader="false">
    <asp:boundfield datafield="Name">

The Children list is a list of Sitecore.Data.Items.Item, so I can use the “Name” property as datafield. I can also use DisplayName, ID, Modifed and other fields, but I cannot use The Field[] property (if anyone know if this is possible please let me know).

On my Page_Load event I set up the DataSource:

Sitecore.Data.Items.Item item = Sitecore.Context.Database.GetItem("/sitecore/content/home");
if (item != null)
  GridView.DataSource = item.Children;

Voila! A simple databound Sitecore Child list.


About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 5 and tagged . Bookmark the permalink.

6 Responses to Sitecore 5.3: Using childlists as datasource

  1. jimmyg423 says:

    <asp:TemplateField ShowHeader="false">
    <asp:Label ID="lblEventTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Fields[\"Title\"]") %>’></asp:Label>

  2. briancaos says:

    Nice solution to the missing Fields[] property. Thanks to Jimmy from Minneapolis.

  3. will says:

    I know this is a fairly old thread but just wanted to say thanks to jimmyg423 for the fields syntax and the OP as well. Saved me a ton of time late at night trying to figure out how to get the field values.

  4. Naresh says:

    Just a small change to Jimmy’s Solution.

    <sc:Text ID="scText" runat="server" Item="” Field=”field” />

  5. Naresh says:

    <sc:Text ID="scEventTitle" runat="server" Item="” Field=”eventtitle” />

  6. Tony Kiernan says:

    Can you add conditions to that?
    eg: item.Children.Where(c => c.something == true)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.