Company      |       Articles & Research      |      Services      |      Software      |      Contact

Remember To Encode SharePoint Link* Fields Correctly

When using a SPGridView control or a similar interface control representing fields out of a SharePoint list, it is important to remember that the SharePoint Link* fields are encoded correctly. Most other types when using something like SPListItem.GetFormattedValue will be fine, but when LinkTitle, LinkDiscussionTitle, etc. are hit you can use something like the below to HTML encode it.

C#:
  1. string.Format("<a href=\"{0}/{1}?ID={2}\">{3}</a>", SPListItem.Web.Url, SPListItem.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url, SPListItem.ID, SPListItem.GetFormattedValue(SPField.InternalName));

To test the type just use , then employ an equality test:

C#:
  1. if (Equals(SPListItem.GetFormattedValue(SPField.InternalName), "LinkTitle"))

share save 171 16 Remember To Encode SharePoint Link* Fields Correctly

Related posts:

  1. Returning GUID’s For Internal SharePoint Fields
  2. Returning The SharePoint Start Workflow Link
  3. Remember When Using SPUtility.FormatDate To UTC (Extension Method)
  4. Building Master SPFile WebForm Collections, Remember SPListItems!
  5. Get A SharePoint Field By Name

1 Comment »

  1. [...] Remember To Encode SharePoint Link* Fields Correctly [...]

    Pingback by Links (9/10/2009 « Steve Pietrek – Everything SharePoint) — September 10, 2009 @ 4:33 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment