Custom Sharepoint userfield webcontrol. Generate mailto
When I was using the userfield to display to username of a selected user or group I wanted this field to display username and email adres. Si I create a custom control to display name and generate a mailto link:
using System;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web.UI;
namespace Webcontrols
{
public class EnhancedUserField : UserField
{
protected override void Render(System.Web.UI.HtmlTextWriter output)
{
if (this.ItemContext.FormContext.FormMode == SPControlMode.Display)
{
try
{
using (SPSite oSPSite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb oSPWeb = oSPSite.OpenWeb())
{
String usersString = base.ListItemFieldValue.ToString();
SPFieldUserValueCollection userValueColl = new SPFieldUserValueCollection(oSPWeb, usersString);
int count = 0;
foreach (SPFieldUserValue userValue in userValueColl)
{
count++;
SPUser siteUser = userValue.User;
System.Diagnostics.Trace.TraceInformation("User found: {0}", siteUser.Name);
if (!string.IsNullOrEmpty(siteUser.Email))
{
string email = string.Format("mailto:{0}", siteUser.Email);
output.WriteBeginTag("a");
output.WriteAttribute("HREF", email);
output.Write(HtmlTextWriter.TagRightChar);
output.WriteEncodedText(siteUser.Name);
output.WriteEndTag("a");
if(count > 1)
output.WriteEncodedText(" , ");
}
else
{
output.WriteEncodedText(siteUser.Name);
}
}
}
}
}
catch { }
}
else
{
base.Render(output);
}
}
}
}
U can use this in your page layout in this way:
<%@ Register Tagprefix="Webcontrols" Namespace="Webcontrols" Assembly="Webcontrols, Version=1.0.0.0, Culture=neutral, PublicKeyToken=868bd32625cae9fb" %> Person: <WebControls:EnhancedUserField FieldName="Person" runat="server"></WebControls:EnhancedUserField >

