Blogger

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

    Recent items

Login

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;
 
namespace LucaCostante.com.WebControls
{
    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)
        {
            try
            {
                string value = GetLookupValue(FieldName);
                if (SPContext.Current.ListItem.Fields.ContainsFieldWithStaticName(FieldName))
                    value=SPContext.Current.Item[FieldName].ToString();
 
                if (!string.IsNullOrEmpty(value) && value.Contains(";#"))
                    value=value.Split(new string[]{";#"},StringSplitOptions.RemoveEmptyEntries)[1];
                writer.Write(value);
            }
            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="LucaCostante.com.WebControls" Assembly="LucaCostante.com, Version=1.0.0.0, 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

2 comments

Leave a comment

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