• This email address is being protected from spambots. You need JavaScript enabled to view it.

    Recent items


Tuesday, 23 July 2013 23:02

How to create a simple WebControl to view the Lookup Value

Written by
Rate this item
(0 votes)

If you use this field to render a lookup field, the results is an hyperlink to view the related item.

Show/Hidden csharp code

View source
<SharePointWebControls:FieldValue ID="lfYear" FieldName="Year" runat="server"/>

If you want to create a reusable WebControl just to render the Lookup Value, you can copy and paste this code and reproduce these steps:

  • Create the web control. The property FieldName would be used to retreive the date from the SPListItem

Show/Hidden csharp code

View source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Publishing.WebControls;
using Microsoft.SharePoint.Publishing.Internal.WebControls;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using Microsoft.SharePoint;
    public class SimpleLookupView: WebControl
        private string name;
        [Bindable(true), DefaultValue(""), Localizable(false)]
        public string FieldName
            get { return name; }
            set { name = value; }
        protected override void Render(HtmlTextWriter writer)
                string value = GetLookupValue(FieldName);
                if (SPContext.Current.ListItem.Fields.ContainsFieldWithStaticName(FieldName))
                if (!string.IsNullOrEmpty(value) && value.Contains(";#"))
                    value=value.Split(new string[]{";#"},StringSplitOptions.RemoveEmptyEntries)[1];
            catch { }
        public static string GetLookupValue(string fieldName)
            string value = string.Empty;
            if (SPContext.Current.Item.Fields.ContainsFieldWithStaticName(fieldName))
                value = SPContext.Current.Item[fieldName].ToString();
            if (!string.IsNullOrEmpty(value) && value.Contains(";#"))
                value = value.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1];
            return value;

  • Register the WebControl into the PageLayout

Show/Hidden csharp code

View source
<%@ Register Tagprefix="LCWebControls" Namespace="" Assembly=", Version=, Culture=neutral, PublicKeyToken=13f47470cb97767e"%>

  • Use the web control

Show/Hidden csharp code

View source
<LCWebControls:SimpleLookupView ID="lfYear" FieldName="Year" runat="server" />


Read 34494 times Last modified on Tuesday, 12 January 2016 10:34


Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.