How to Use the SharePoint MultipleLookupField Control: A Step-by-Step Guide

When working with SharePoint, you may encounter scenarios where you need to associate multiple items from one list with an item in another list. One useful control for achieving this is the MultipleLookupField. In this post, we’ll guide you through using the MultipleLookupField control step-by-step, ensuring you understand how to display and manage multiple lookup values within your web parts.

The Problem: Connecting Two Lists

The primary challenge is integrating the MultipleLookupField control into a web page that runs in the context of SharePoint. You might want to link two SharePoint field collections, such as tasks and documents. This requires some basic setup as well as understanding how to manipulate the control effectively.

Getting Started with MultipleLookupField

Step 1: Create Your SharePoint Environment

  1. Set Up a Team Site: Start by creating a new team site within SharePoint.
  2. Add Tasks: Go to the Tasks list and create a few tasks.
  3. Create a Document: Upload a document to the Shared Documents library.
  4. Create a Lookup Column:
    • In the Shared Documents library, create a new column named “Related”.
    • Set this column to be a Lookup field that references the “Title” field of the Tasks list.
    • Make sure to allow multiple values for this column.

Step 2: Creating the Web Part

To start utilizing the MultipleLookupField, you need to create a web part. Here’s a simplified example to guide you through this process.

Basic Structure of Your Web Part

Label l;
MultipleLookupField mlf;

protected override void CreateChildControls()
{
    base.CreateChildControls();
    SPList list = SPContext.Current.Web.Lists["Shared Documents"];
    if (list != null && list.Items.Count > 0)
    {
        LiteralControl lit = new LiteralControl("Associate tasks to " + list.Items[0].Name);
        this.Controls.Add(lit);

        mlf = new MultipleLookupField();
        mlf.ControlMode = SPControlMode.Edit;
        mlf.FieldName = "Related";
        mlf.ItemId = list.Items[0].ID;
        mlf.ListId = list.ID;
        mlf.ID = "Related";
        this.Controls.Add(mlf);

        Button b = new Button();
        b.Text = "Change";
        b.Click += new EventHandler(bClick);
        this.Controls.Add(b);

        l = new Label();
        this.Controls.Add(l);
    }
}

void bClick(object sender, EventArgs e)
{
    l.Text = "";
    foreach (SPFieldLookupValue val in (SPFieldLookupValueCollection)mlf.Value)
    {
        l.Text += val.LookupValue.ToString() + " ";
    }

    SPListItem listitem = mlf.List.Items[0];
    listitem["Related"] = mlf.Value;
    listitem.Update();
    mlf.Value = listitem["Related"];
}

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    EnsureChildControls();
}

Step 3: Deploying Your Web Part

  1. Build and Deploy: Compile your code and deploy the web part to your SharePoint environment.
  2. Add the Web Part to Your Site: Navigate to your team site homepage and add an instance of your new web part.

Understanding the Click Handler

The click event handler bClick is crucial for updating the UI.

  • Fetching Values: It begins by retrieving all selected values from the MultipleLookupField and displaying them.
  • Updating the List Item: It updates the “Related” field in the document list with the selected tasks, ensuring synchronization between the UI and the list item.

Additional Notes

  • The example provided is quite basic: it’s important to note that it lacks comprehensive error handling and isn’t meant for production use. Consider extending it for better usability.
  • You might find that if you comment out the line mlf.Value = listitem["Related"];, the UI may not sync correctly with the underlying data, thus making this line necessary for proper functionality.

Conclusion

By following the steps outlined above, you can successfully integrate the MultipleLookupField control into your SharePoint site. This enables efficient management of related items from different lists, enhancing your overall SharePoint experience. With a bit of practice and refinement, you can turn these basic principles into powerful solutions tailored to your specific needs.

Feel free to reach out with any questions or share your experiences using the MultipleLookupField control in SharePoint!