I have a ListView that I am binding to a SPList. In this list view I would like to have a link to each SPListItem.

Microsoft has conveniently decided that the URL member of an SPListItem should be a mysterious link that does not work. After some research I found some other people that had run into this and used the following work around:

listItem.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?ID=" + listItem.ID

I attempted to shoe horn this into my ListView ItemTemplate and it almost works (using 0 instead of a PAGETYPE):

            <a id="a_ItemLink" href="<%# DataBinder.GetIndexedPropertyValue(((SPListItem)Container.DataItem).ParentList.Forms, "[0].Url")  + "?ID=" + Eval("ID") %>" runat="server"><%# Eval("Title") %></a><br />

The issue that GetIndexedPropertyValue sets my links to something like this:


I'm not sure why it would include the Microsoft.SharePoint.SPForm part, but obviously this is not working. Has anyone successfully DataBound to a SPListItem and extracted the URL? Am I doing something wrong above?


If you have items from one list, you can use this solution:

// Column with link
var hyperLinkField = new HyperLinkField();
hyperLinkField.HeaderText = "Display item link";
hyperLinkField.Text = "Show";
hyperLinkField.DataNavigateUrlFields = new string[] { "ID" };
hyperLinkField.DataNavigateUrlFormatString = list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?ID={0}";

This was tested and works with SPGridView and GridView.

That code should be similar to this aspx markup:

  HeaderText="Display item link"
  DataNavigateUrlFormatString="<%= list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url %>?ID={0}"

MSDN reference for HyperLinkField:

| improve this answer | |

It's casting properly (which is why you're seeing Microsoft.SharePoint.SPForm), but it's not getting the property that you're trying to set. What exactly does the [0] correspond to? Can you try "[\"Url\"]" instead? I'm getting that from this link.

Edit: Aaaand I just noticed that the link is to your own post :)

| improve this answer | |
  • Haha, yeah similar issue. My Problem is that I got that working and the URL that is returned doesn't work. Apparently this is the expected functionality (I would like to hear an explanation from Microsoft on that one....) – Abe Miessler May 23 '11 at 18:04

If you are trying to pass more than one fields in target url, you can simply add one more field in the DataNavigateUrlFields array, like this:

hyperLinkField.DataNavigateUrlFields = new string[] { "ContentFolder", "ID" };

What I just mentioned can be an obvious thing for a smart developer, but I just wanted to say it out loud because I was trying to find the solution to allow multiple fields and when I came across this solution, I wondered, what if I do that, would it help? and it actually did.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.