Surendra Sharma

Surendra Sharma

Search This Blog

Thursday, August 20, 2015

Different ways to get item Created date time on web page in Sitecore



Sometime we need to show item created date and time in Sitecore. Here are some of the possible ways to show on aspx or ascx control.

I am using repeater here but you can directly use on page

<asp:Repeater ID="rpCars" ItemType="Sitecore.Data.Items.Item" runat="server">
    <ItemTemplate>
        <p>
            <strong>

               Way 1 : <%# Item.Fields[Sitecore.FieldIDs.Created].Value %> <br />
               Way 2 : <%# Item.Fields["__Created"].Value %> <br />
               Way 3 : <sc:FieldRenderer ID="FieldRenderer1" FieldName="__Created" runat="server" DataSource="<%#: Item.ID %>" /> <br />
               Way 4 : <%# Sitecore.DateUtil.FormatIsoDate(Item.Fields[Sitecore.FieldIDs.Created].Value, "dd-MMM-yyyy hh:mm") %>
            </strong>
        </p>
    </ItemTemplate>
</asp:Repeater>

Please leave your comments or share this code if it’s useful for you. Also share if you know any other way to get it.

Sunday, August 16, 2015

How to convert first letter of every word in a sentence to upper in C#



I come across with simple but very interesting problem where I have to make first letter of every word in a sentence to capital. For example if I have sentence "top 5 most advance computer" then it should be displayed as "Top 5 Most Advance Computer".

There are lots of ways to do this 

·         Write for loop and do the string operation
·         Call the VB.NET methods etc.

But I found LINQ help me to do this in simple and clean manner. Here is method for this

public static string ConvertFirstCharOfWordToUpper(string str)
{
    return string.Join(" ", str.Split(' ').Select(x => x.First().ToString().ToUpper() + x.ToLower().Substring(1)));
}
 
Please leave your comments or share this code if it’s useful for you.

Thursday, August 13, 2015

How to use Strongly Typed Repeater in Sitecore?



It’s all about ITEMS in Sitecore. We are maintaining hierarchy of items in content tree i.e. parent child relationship.

It’s a common scenario in every Sitecore project to get child of particular node.

Typically we use Repeater to bind and show the items.

But do you know about how strongly type Repeater can help us?

For this set Repeater ItemType property as ItemType="Sitecore.Data.Items.Item"

Set DataSource property of every control in repeater to <%#: Item.ID %>

This forces the control to retrieve its field values from that particular bind item as opposed to the Context item. Colon in binding expression <%#: %> indicates that final bound data should be HTML-encoded.

So your final HTML code looks like this

<asp:Repeater ID="rptCars" ItemType="Sitecore.Data.Items.Item" runat="server">
    <HeaderTemplate>
        <div>
            <h3>Latest Cars</h3>
    </HeaderTemplate>
    <ItemTemplate>
<sc:Text Field="Car Name" runat="server" DataSource="<%#: Item.ID %>" />
       <sc:FieldRenderer FieldName="Car Description" runat="server" DataSource="<%#: Item.ID %>" />
       <sc:Link DataSource="<%#: Item.ID %>" Field="Car Website" runat="server" />
    </ItemTemplate>
    <FooterTemplate>
        </div>
    </FooterTemplate>
</asp:Repeater>

Code behind page

rptCars.DataSource = Sitecore.Context.Item.GetChildren().Where(x => x.TemplateName.Equals("Cars")).ToList();
rptCars.DataBind();
 
Please leave your comments or share this code if it’s useful for you.