This topic has not yet been rated - Rate this topic

MultipleLookupField Class

Represents a control containing multiple lookup fields on a form page (not a list view page).

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class MultipleLookupField : LookupField
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
MultipleLookupField
Description

The Microsoft.SharePoint.WebControls.MultipleLookupField class inherits from the Microsoft.SharePoint.WebControls.LookupField class which is used to represent a Lookup field control. MultipleLookupField extends the concept of LookupField by allowing a field to lookup multiple values leveraging a robust selection interface. Shipped use of MultipleLookupField can be found when a Lookup field is set to allow multiple values. Selecting this configuration will toggle the SPFieldLookup.AllowMultipleValues property to true, which will ensure that BaseFieldControl is of type MultipleLookupField as opposed to LookupField. 

The robust selection interface is sponsored in the customary fashion as other picker controls in SharePoint using the Microsoft.SharePoint.WebControls.GroupedItemPicker control, items are added to the control in an orthodox fashion using GroupItemPicker.AddItem. In order to ensure uniqueness of items added to the collection used for control rendering, the list item ID’s are used by overriding LookupField.ItemIds to build a typed list of integer values.

Usage Scenario

MultipleLookField has representation internally as a standard SharePoint field control when toggling the SPFieldLookup.AllowMultipleValues property. However, it is possible to use the control with custom controls in order to build extended list metadata applications.

In the below, I am creating new SPSite and SPWeb objects to hydrate a specific SPList object by passing in the list name. This list represents the target datasource for the control. Following, a new MultipleLookField object is hydrated, and the required properties are set. Importantly, it should be noted that we specifying the list and item ID by using the MultipleLookupField.ListId and MultipleLookupField.ItemId property by consuming values sponsored by the previously hydrated SPList object.

C# Code Example

protected override void CreateChildControls()
{

using (SPSite site = new SPSite("ExampleUrl"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["ExampleList"];
MultipleLookupField mlf = new MultipleLookupField();
mlf.ControlMode = SPControlMode.Edit;
mlf.FieldName = "ExampleLookupField";
mlf.ListId = list.ID;
mlf.ItemId = list.Items[0].ID;
Controls.Add(mlf);
}
}
base.CreateChildControls();
}

Visual Basic .NET Code Example

Protected Overloads Overrides Sub CreateChildControls()

Using site As New SPSite("ExampleUrl")
Using web As SPWeb = site.OpenWeb()
Dim list As SPList = web.Lists("ExampleList")
Dim mlf As New MultipleLookupField()
mlf.ControlMode = SPControlMode.Edit
mlf.FieldName = "ExampleLookupField"
mlf.ListId = list.ID
mlf.ItemId = list.Items(0).ID
Controls.Add(mlf)
End Using
End Using
MyBase.CreateChildControls()
End Sub

Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com