Edwin Waegemakers

Work smarter

Create a link that opens in a new window in SharePoint 2007

It seems that links in Sharepoint 2007 lists still open in the same window. There is no build in option to change this as far as I know, so what I did was create a new field type.

I've based this on an article from Patrick Tisseghem that can be found here.

I could not get the sample to work. In the end I found that I had an inherits statement in the control's ascx that did not need to be in there.  

The steps to create the custom field are:

  1. Create a class for the value of the field
  2. Create a class custom field itself
  3. Create a class for the control that is used for rendering (it uses a control template for the actual rendering)
  4. Create an ascx file containing the rendering template
  5. Create a field type definition file
  6. Deploy, register as safe control and recycle the app pool

Here is the code:

1. LinkFieldValues.cs

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace MyCompany.MOSS.Web.Link
{
public class LinkFieldValue: SPFieldMultiColumnValue
{
private const int numberOfFields = 3;

public LinkFieldValue()
: base(numberOfFields)
{ }

public LinkFieldValue(string value)
: base(value)
{ }

public string Hyperlink
{
get { return this[0]; }
set { this[0] = value; }
}

public string Description
{
get { return this[1]; }
set { this[1] = value; }
}
public string Target
{
get { return this[2]; }
set { this[2] = value; }
}
}
}

2. LinkField.cs

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace MyCompany.MOSS.Web.Link
{
class LinkField: SPFieldMultiColumn
{

public LinkField(SPFieldCollection fields, string fieldName):base(fields, fieldName)
{

}
public LinkField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{

}
public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fldControl = new LinkFieldControl();
fldControl.FieldName = InternalName;
return fldControl;
}
}
public override object GetFieldValue(string value)
{
if (string.IsNullOrEmpty(value))
{
return null;
}
return new LinkFieldValue(value);
}
}
}

3. LinkFieldControl.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace MyCompany.MOSS.Web.Link
{
class LinkFieldControl : BaseFieldControl

{
protected TextBox hyperlinkTextBox;
protected TextBox descriptionTextBox;
protected DropDownList targetDropDownList;

protected override string DefaultTemplateName
{
get { return "LinkFieldRendering"; }
}

public override object Value
{
get
{
EnsureChildControls();
LinkFieldValue fieldValue = new LinkFieldValue();
fieldValue.Description = descriptionTextBox.Text.Trim();
fieldValue.Hyperlink = hyperlinkTextBox.Text.Trim();
fieldValue.Target = targetDropDownList.Text.Trim();

return fieldValue;
}
set
{
EnsureChildControls();
LinkFieldValue fieldValue = (LinkFieldValue)value;
descriptionTextBox.Text = fieldValue.Description;
hyperlinkTextBox.Text = fieldValue.Hyperlink;
targetDropDownList.SelectedValue = fieldValue.Target;
}
}

public override void Focus()
{
EnsureChildControls();
hyperlinkTextBox.Focus();
}

protected override void CreateChildControls()
{
if (Field == null)
return;

base.CreateChildControls();

if (ControlMode == SPControlMode.Display)
return;

hyperlinkTextBox = (TextBox)TemplateContainer.FindControl("hyperlinkTextBox");
if (hyperlinkTextBox == null)
throw new ArgumentException("Corrupted LinkFieldRendering template - missing hyperlinkTextBox.");
hyperlinkTextBox.TabIndex = TabIndex;
hyperlinkTextBox.CssClass = CssClass;
hyperlinkTextBox.ToolTip = Field.Title + " Link";

descriptionTextBox = (TextBox)TemplateContainer.FindControl("descriptionTextBox");
if (descriptionTextBox == null)
throw new ArgumentException("Corrupted LinkFieldRendering template - missing descriptionTextBox.");
descriptionTextBox.TabIndex = TabIndex;
descriptionTextBox.CssClass = CssClass;
descriptionTextBox.ToolTip = Field.Title + " description";

targetDropDownList = (DropDownList)TemplateContainer.FindControl("targetDropDownList");
if (targetDropDownList == null)
throw new ArgumentException("Corrupted LinkFieldRendering template - missing targetDropDownList.");
targetDropDownList.TabIndex = TabIndex;
targetDropDownList.CssClass = CssClass;
targetDropDownList.ToolTip = Field.Title + " target";

if (ControlMode == SPControlMode.New)
{
hyperlinkTextBox.Text = Field.GetCustomProperty("DefaultHyperlink").ToString();
descriptionTextBox.Text = Field.GetCustomProperty("DefaultDescription").ToString();
targetDropDownList.SelectedValue = Field.GetCustomProperty("DefaultTarget").ToString();
}
}
}
}

4. LinkControl.ascx

<%@Control Language="C#" Debug="true" Inherits="MyCompany.MOSS.Web.Link.LinkFieldControl"%>
<%@Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>

<SharePoint:RenderingTemplate ID="LinkFieldRendering" runat="server">
<Template>
<asp:TextBox runat="server" id="hyperlinkTextBox" MaxLength="400" Size="40"/>
<asp:TextBox runat="server" id="descriptionTextBox" MaxLength="100" Size="40"/>
<asp:DropDownList runat="server" id="targetDropDownList">
<asp:ListItem Value="_Blank" Text="Nieuw Venster"/>
<asp:ListItem Value="_Self" Text="Dit Venster"/>
</asp:DropDownList>
</Template>
</SharePoint:RenderingTemplate>

5. fldtypes_LinkControl.xml

<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">MyLink</Field>
<Field Name="ParentType">MultiColumn</Field>
<Field Name="TypeDisplayName">My Link</Field>
<Field Name="TypeShortDescription">My Link (Link, Description, Target)</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowOnListAuthoringPages">TRUE</Field>
<Field Name="ShowOnDocumentLibraryAuthoringPages">TRUE</Field>
<Field Name="ShowOnSurveyAuthoringPages">TRUE</Field>
<Field Name="ShowOnColumnTemplateAuthoringPages">TRUE</Field>
<Field Name="FieldTypeClass">MyCompany.MOSS.Web.Link.LinkField,MyCompany.MOSS.Web.Link, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null</Field>
<PropertySchema>
<Fields>
<Field Name="DefaultHyperlink" DisplayName="Default Link:" MaxLength="400" DisplaySize="40" Type="Text">
<Default>http://www.nu.nl</Default>
</Field>
<Field Name="DefaultDescription" DisplayName="Default Description:" MaxLength="100" DisplaySize="40" Type="Text">
<Default>Nieuws</Default>
</Field>
<Field Name="DefaultTarget" DisplayName="Default Target:" MaxLength="20" DisplaySize="20" Type="Text">
<Default>Nieuw Venster</Default>
</Field>
</Fields>
</PropertySchema>
<RenderPattern Name="DisplayPattern">
<Switch>
</Expr>
<Case Value="">
</Case>
<Default>
<HTML><![CDATA[<A HREF="]]></HTML>
<Column SubColumnNumber="
0" HTMLEncode="TRUE"/>
<HTML><![CDATA[" target="]]></HTML>
<Column SubColumnNumber="2" HTMLEncode="TRUE"/>
<HTML><![CDATA[">]]></HTML>
<Column SubColumnNumber="1" HTMLEncode="TRUE"/>
<HTML><![CDATA[</a>]]></HTML>
</Default>
</Switch>
</RenderPattern>
</FieldType>
</FieldTypes>

6. Deploy

  • Place the dll in the GAC
  • Copy the xml file to c:\program files\common files\microsoft shared\web server extensions\12\template\xml
  • copy the ascx file to c:\program files\common files\microsoft shared\web server extensions\12\template\controltemplates
  • Add this line to the safecontrols section in the web.config file:

<SafeControl Assembly="MyCompany.MOSS.Web.Link, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" Namespace="MyCompany.MOSS.Web.Link" TypeName="*" Safe="True" />

  • iisreset

 

Comments

SharePoint, SharePoint and stuff said:

Entwicklung Create a link that opens in a new window in SharePoint 2007 Event Handler Part 1: Everything

# March 23, 2007 1:52 AM

Hydrocodone guaifenesin syreth. said:

Hydrocodone. Cold water extraction for hydrocodone.

# May 30, 2009 12:18 AM

Percocet withdrawal symptoms. said:

Percocet.

# June 3, 2009 1:48 AM

Amoxicillin drug interactions. said:

Amoxicillin dosage. Dosing of amoxicillin for sinus infection. Amoxicillin. Prescription free amoxicillin. Amoxicillin and colds.

# June 5, 2009 2:20 AM