Create link to Sitecore Item
Asked Answered
D

6

23

I know I have done this before but I can't seem to remember where or how.

I want to create a link to an Item in Sitecore. This code:

Sitecore.Data.Items.Item itm = Sitecore.Context.Database.GetItem(someID);
return itm.Paths.Path.ToString();

Produces the following string:

http://localhost/sitecore/content/Home/Item1/Item11/thisItem

I would like to have this string instead:

http://localhost/Item1/Item11/thisItem.aspx

What is the correct way to get the path to the item? In this case I can't use a normal Sitecore link:

Sitecore.Web.UI.WebControls.Link
Devest answered 23/3, 2009 at 17:13 Comment(0)
A
37

You're needing this one, assuming you're running Sitecore v6 or anything more recent (tested with 8.2-6, should work with 9 too):

Sitecore.Links.LinkManager.GetItemUrl(item);
Avast answered 23/3, 2009 at 20:30 Comment(0)
F
4

If you are still using Sitecore 5.3, you can use this. Be warned this method is deprecated in Sitecore 6.0.

string url = item.Paths.GetFriendlyUrl();
Ferrite answered 31/7, 2009 at 2:49 Comment(0)
U
1

Your Web Control:

<asp:HyperLink ID="HyperLinkItem" runat="server">
    Item
</asp:HyperLink>

Your Code:

var homeItem = Sitecore.Context.Database.GetItem("/sitecore/content/Home");
HyperLinkItem.NavigateUrl = Sitecore.Links.LinkManager.GetItemUrl(homeItem);
Uncaused answered 23/4, 2013 at 1:42 Comment(0)
C
1

You can render item link as below:

Sitecore.Data.Items.Item itm = Sitecore.Context.Database.GetItem(someID);
return Sitecore.Links.LinkManager.GetItemUrl(itm);
Canale answered 23/9, 2013 at 15:46 Comment(0)
A
0

Try this

  <asp:HyperLink ID="hlItem" runat="server">

    </asp:HyperLink>

aspx.cs

   Sitecore.Data.Items.Item itm = Sitecore.Context.Database.GetItem("/sitecore/content/Home");

    hlItem.Navigateurl =sitecore.links.linkmanager.getitemurl(itm);
Antenna answered 29/8, 2014 at 15:51 Comment(0)
F
-6

You can do by this also:

Item.Paths.FullPath

It is same as:

Sitecore.Links.LinkManager.GetItemUrl(item);
Flare answered 9/8, 2012 at 17:59 Comment(1)
No, it's not the same at all. FullPath returns the item path, GetItemUrl() uses the LinkProvider to create a friendly URL.Diphtheria

© 2022 - 2024 — McMap. All rights reserved.