Pregunta

I am having trouble figuring out how I can sort a list of items in descending order according to their created date (I know Sitecore allows for sorting items in ascending order by their created date). I'm still pretty new with Sitecore, so I'm not sure what to do...Any suggestions would be helpful!

 Item[] BlogPosts = HomeItem.Axes.SelectItems(@"child::*[@@templatename='BlogComment']");

            if (BlogPosts != null)
            {

                DataSet ds = new DataSet();
                DataTable posts = ds.Tables.Add("posts");

                posts.Columns.Add("PostName", Type.GetType("System.String"));
                posts.Columns.Add("DateCreated", Type.GetType("System.String"));
                posts.Columns.Add("PostComment", Type.GetType("System.String"));

                foreach(Item PostItem in BlogPosts)
                {
                    DataRow dr = posts.NewRow();

                    dr["PostName"] = PostItem.Fields["Name"].Value;
                    dr["DateCreated"] = PostItem.Statistics.Created;
                    dr["PostComment"] = PostItem.Fields["Comment"].Value;

                    posts.Rows.Add(dr);
                }
                commentsListRptr.DataSource = ds;//this is a repeater I'm using to show the data
                commentsListRptr.DataMember = "posts";
                commentsListRptr.DataBind();
            }
¿Fue útil?

Solución

You can sort the items using LINQ:

var items = Sitecore.Context.Database.SelectItems("/sitecore/content/home/*");
items = items.OrderBy(x => x[Sitecore.FieldIDs.Created]).Reverse();

Obviously change the query or item list to fit your requirements.

EDIT following addition of code:

Set up a repeater in your ascx, we'll use the Sitecore FieldRenderer but disable webediting in this control:

<asp:Repeater ID="rptBlogPosts" runat="server" OnItemDataBound="rptBlogPosts_ItemDataBound">
    <HeaderTemplate><table></HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><sc:FieldRenderer runat="server" ID="PostName" FieldName="Name" DisableWebEditing="True" /></td>
            <td><asp:Literal runat="server" ID="PostDate"></asp:Literal></td>
            <td><sc:FieldRenderer runat="server" ID="PostComment" FieldName="Comment" DisableWebEditing="True" /></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate></table></FooterTemplate>
</asp:Repeater>

And in your code behind on Page_Load bind the control, bind the datasource of your repeater and then set the FieldRenderer Item and display the created date in the format to suit.

private void Page_Load(object sender, EventArgs e)
{
    Item[] BlogPosts = HomeItem.Axes.SelectItems(@"child::*[@@templatename='BlogComment']");
    if (BlogPosts.Any())
    {
        rptBlogPosts.DataSource = BlogPosts.OrderBy(x => x[Sitecore.FieldIDs.Created]).Reverse();
        rptBlogPosts.DataBind();
    }
}

protected void rptBlogPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var currentItem = e.Item.DataItem as Item;
        var scPostName = e.Item.FindControl("PostName") as FieldRenderer;
        var litPostDate = e.Item.FindControl("PostDate") as Literal;
        var scPostComment = e.Item.FindControl("PostComment") as FieldRenderer;

        scPostName.Item = currentItem;
        litPostDate.Text = currentItem.Statistics.Created.ToString("H:mm:ss MM/dd/yy");
        scPostComment.Item = currentItem;
    }
}

Your Sitecore Query can be very expensive performance wise depending on how much content you have how deep your content tree is, I'm not sure which version of Sitecore you are using, but in any case you look to index your content (with Lucene) and use that to retrieve the posts. If you are using Sitecore 7 the take a look at this post about Linq to Sitecore

Otros consejos

Using the same idea, you could also use the following...

var items = Sitecore.Context.Database.SelectItems("/sitecore/content/home/*").OrderByDescending(x => x.Statistics.Created);

It eliminates an additional line of code.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top