Create SPFieldLookup programatically
Februar 21st, 2009 Posted in .NET, SharePoint
There aren’t many posts describing how to add a SPFieldLookup column to a list programatically. Here’s how I do it:
I have a list named “MyList” and I have a list named “MyLookupList”. I want to create a lookup column within “MyList” looking up values from “MyLookupList”.
SPList myList = web.Lists["MyList"]; SPList myLookupList = web.Lists["MyLookupList"]; myList.Fields.AddLookup("Lookup", myLookupList.ID, false); SPFieldLookup fieldLookup = myList.Fields["Lookup"] as SPFieldLookup; // Display Title Column in lookup field // (this is shown by default but this way you can change it) fieldLookup.LookupField = myLookupList.Fields[SPBuiltInFieldId.Title].InternalName; fieldLookup.Update();
Tags: SPFieldLookup
Februar 27th, 2009 at 1:17 pm
Yeah, it’s surprisingly easy when you know how! I was surprised that there wasn’t a constructor for the SPFieldLookup that also accepts the field to look up against.
Here was my description!
http://www.novolocus.com/2009/02/09/wss-practice-create-a-list-columns-and-view-programmatically
Oktober 2nd, 2009 at 8:58 am
Oktober 27th, 2009 at 4:46 am
Quick question:
I have a list that uses a content type that uses a site lookup column.
The contenty type is added to the list and the lookup column is added to the content type.
Everything works, but the list only shows the lookup column values as items in its dropdown, if I go to the UI and go to the site lookup column and click the okay button.
Otherwise the list does not show any value in the dropdown.
I know if I posted some code it would help, my applogies for not be able to do that.
Anythoughs why I have to go to the site column in the UI and click ok to get the items to appear.
What does the okay button on the site column do, update the items? Does items.Update work the same?
März 19th, 2010 at 4:04 pm
Hi !
THANK YOU SO MUCH for this post !!! I have been struggling so many weeks trying to make this feature work using Silverlight Client object model and Sharepoint 2010 (my bosss is thinking to fire me I guess because I just cannot make this work….)
I cannot find the Fields.AddLookup method though
(
I just found Fields.AddDependentLookup and it doesn’t seem to do the same thing (I am using Silverlight client object model).
Also, can you please point out how you can update a Managed Metadata column in a list (programatically of course) ? **I beg you** please !
What I am trying to do is to automatically create tasks (in a task list) and adding metadata automatically (I added an extra “Managed Metadata” column on it) but I didn’t have any luck yet. I mean, I can create tasks items without a problem, but I can’t set the metadata column in code.
When I monitor the traffic to the site (POST from newform.aspx) using Fiddler or the IE 8 debugger, I see that in the MetadataField, a string (yes, a string !) gets passed in the form of :
|. Example:
Europe|dda79be8-f4f0-4072-a1d0-e2d5ee8fd749
I tried assigning this field just like that to my list before calling the Update() and ExecuteAsync() methods but it doesn’t get added. I tried every possible combination (array of strings, reversing the values, passing it as a GUID, etc.).
I also want to be able to correlate a task to an item
in another list, using a Lookup Field (whenever a production item gets inserted in the production list, I generate tasks related to that item so I can monitor what people are doing on that Item later on).
I found that this scenario is overkill when you want to implement it using custom workflows, so I decided to do it myself using Silverlight 3.0 and the Sharepoint 2010 client object model.
Microsoft’s lack of documentation on the client API is just unacceptable and their examples are simplistic at best (they only deal with string columns !).
In real-world scenarios there are DateTimes, Numeric, User, Lookups and also Managed Metadata columns.
If you have any hints or ideas, I beg you to contact me to jlagos@sympatico.ca
I will buy you a lunch if you ever come to Montreal !
Cheers and thanks again for this unique and very good post in the whole www !
Javier L.
März 24th, 2010 at 10:38 pm
Hi,
i would like to offer tutorial on how to create basic sharepoint lookup field within visual studio. here is a tutorial with screenshots and comments. it could help to someone.
http://sharepoint-anthony.blogspot.com – Sharepoint Lookup Field