Surendra Sharma

Surendra Sharma

Search This Blog

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.

Friday, July 31, 2015

How to redirect from specific page of OLD site URL to NEW site URL without writing single line of code



If your client website is already running and you have developed it again with same or different technology.

Once the coding and testing of this new website is complete. What next?
Deploy on server. Make it LIVE.

So there may be some old URL or pages which should be redirected to new website pages.

Good example is if somebody bookmarked the old URL then instead of page not found it should be redirected to new pages.

In simple terms it’s called 301 redirect.

How to achieve 301 redirect without writing code in ASP.NET?

Just make entries in web.config by specifying old URL pages mapping with new pages as below.

Suppose you old page URL is http://www.example.com/contact.php and you want to redirect it to new page as http://www.example.com/guest/contact-detail.aspx

than your config file entry is

<configuration>
    <location path="contact.php">
      <system.webServer>
        <httpRedirect enabled="true" destination="guest/contact-detail.aspx" httpResponseStatus="Permanent" />
      </system.webServer>
    </location>
    <location path="license.pdf">
      <system.webServer>
        <httpRedirect enabled="true" destination="newlicense.aspx" httpResponseStatus="Permanent" />
      </system.webServer>
    </location>

</configuration>
You can specify any numbers of pages here. But this is good for limited pages. My suggestion is that its limit should be upto 50 pages.

If you have more than 50 pages then write the custom redirecting code.

Please leave your comments or share this tip if it’s useful for you.