<p>I want to use the  MultipleLookupField control in a web page that will run in the context of SharePoint. I was wondering if anyone would help me with an example, which shows step by step how to use the control two display two SPField Collections.</p>

[{'answer_id': 40929, 'author': 'Matt Bishop', 'author_id': 4301, 'author_profile': 'https://Stackoverflow.com/users/4301', 'pm_score': 3, 'selected': True, 'text': '<p>I\'m not entirely sure I understand your question, especially the bit about displaying two SPField collections. Sorry if this turns out to be the answer to a completely different question!</p>\n\n<p>Anyway here\'s a quick demo walkthrough of using the MultipleLookupField in a web part.</p>\n\n<p>Create a team site. Add a few tasks to the task list. Also put a document in the Shared Documents library. Create a new column in the Shared Documents library; call it "Related", have it be a Lookup into the Title field of the Tasks list, and allow multiple values.</p>\n\n<p>Now create a web part, do all the usual boilerplate and then add this:</p>\n\n<pre><code>Label l;\nMultipleLookupField mlf;\n\nprotected override void CreateChildControls()\n{\n    base.CreateChildControls();\n    SPList list = SPContext.Current.Web.Lists["Shared Documents"];\n    if (list != null &amp;&amp; list.Items.Count &gt; 0)\n    {\n        LiteralControl lit = new LiteralControl("Associate tasks to " + \n                             list.Items[0].Name);\n        this.Controls.Add(lit);\n\n        mlf = new MultipleLookupField();\n        mlf.ControlMode = SPControlMode.Edit;\n        mlf.FieldName = "Related";\n        mlf.ItemId = list.Items[0].ID;\n        mlf.ListId = list.ID;\n        mlf.ID = "Related";\n        this.Controls.Add(mlf);\n\n        Button b = new Button();\n        b.Text = "Change";\n        b.Click += new EventHandler(bClick);\n        this.Controls.Add(b);\n\n        l = new Label();\n        this.Controls.Add(l);\n    }\n\n}\n\nvoid bClick(object sender, EventArgs e)\n{\n    l.Text = "";\n    foreach (SPFieldLookupValue val in (SPFieldLookupValueCollection)mlf.Value)\n    {\n        l.Text += val.LookupValue.ToString() + " ";\n    }\n    SPListItem listitem = mlf.List.Items[0];\n    listitem["Related"] = mlf.Value;\n    listitem.Update();\n    mlf.Value = listitem["Related"];\n}\n\nprotected override void OnInit(EventArgs e)\n{\n    base.OnInit(e);\n    EnsureChildControls();\n}\n</code></pre>\n\n<p>Granted, this is borderline ridiculous -- everything is hard-coded, there is no error-handling at all, and it serves no useful purpose -- but it\'s only meant as a quick demo. Now build and deploy this web part and add an instance of it to your team site\'s homepage; it should allow you to get and set the tasks which are associated with the first document in the library.</p>\n\n<p>The strange bit towards the end of the button Click handler, where we read a value from mlf.Value and then write it back again, appears to be required if you want the UI to stay in sync with the actual list values. Try omitting the last line of bClick to see what I mean. This has been driving me nuts for the last hour or so, and I\'m hoping another commenter can come up with a better approach...</p>\n'}, {'answer_id': 41439, 'author': 'Matt Bishop', 'author_id': 4301, 'author_profile': 'https://Stackoverflow.com/users/4301', 'pm_score': 0, 'selected': False, 'text': "<p>Hm. Works fine on mine, so let's see if we can work out how your setup is different...</p>\n\n<p>It looks as though it's having trouble populating the control; my first guess would be that this is because the code makes so many assumptions about the lists it's talking to. Can you check that you've got a plain vanilla Team site, with (assume these names are case-sensitive): </p>\n\n<ol>\n<li>A list called Tasks, with several items in it</li>\n<li>A library called Shared Documents with at least one document</li>\n<li>A column called Related in the Shared Documents library</li>\n<li>The Related column is a Lookup field into the Title column of Tasks, and allows multiple values.</li>\n<li>The first document in Shared Documents has a value for Related</li>\n</ol>\n\n<p>Then add the webpart. Fingers crossed...</p>\n"}, {'answer_id': 45022, 'author': 'Matt Bishop', 'author_id': 4301, 'author_profile': 'https://Stackoverflow.com/users/4301', 'pm_score': 0, 'selected': False, 'text': '<p>Hm. OK, I\'m still trying to break mine... so I went to the layouts directory and created a file foo.aspx. Here it is:</p>\n\n<pre><code>&lt;%@ Page Language="C#" Inherits="System.Web.UI.Page" MasterPageFile="~/_layouts/simple.master" %&gt; \n&lt;%@ Register Tagprefix="foo" Namespace="Foople" Assembly="Foople, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"%&gt;\n&lt;asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server"&gt;\n&lt;foo:WebPart1 id="fred" runat="server" /&gt;\n&lt;foo:WebPart1a id="barney" runat="server" /&gt;\n&lt;/asp:Content&gt;\n</code></pre>\n\n<p>WebPart1 is the webpart from before. WebPart1a is the exact same code, but in a class that inherits directly from WebControl rather than from WebPart.</p>\n\n<p>It works fine, apart from a security validation problem on the postback that I can\'t be bothered to debug.</p>\n\n<p>Changing the masterpage to ~masterurl/default.master, I uploaded foo.aspx to the Shared Documents library, and it works fine from there too -- both the WebControl and the WebPart behave properly, and the security problem is gone too.</p>\n\n<p>So I\'m at a loss. Although I did notice this page with an obscure might-be-bug which is also in SPFolder.get_ContentTypeOrder(): <a href="http://forums.msdn.microsoft.com/en-US/sharepointdevelopment/thread/63baf273-7f36-453e-8293-26417759e2e1/" rel="nofollow noreferrer">http://forums.msdn.microsoft.com/en-US/sharepointdevelopment/thread/63baf273-7f36-453e-8293-26417759e2e1/</a></p>\n\n<p>Any chance you could post your code?</p>\n'}]
['https://Stackoverflow.com/questions/39910', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1360/']