{"id":249,"date":"2010-05-07T17:32:10","date_gmt":"2010-05-07T15:32:10","guid":{"rendered":"http:\/\/www.itidea.nl\/wordpress\/?p=249"},"modified":"2015-09-08T20:32:52","modified_gmt":"2015-09-08T18:32:52","slug":"programmatically-change-the-value-of-a-lookup-column","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/programmatically-change-the-value-of-a-lookup-column\/","title":{"rendered":"Programmatically change the value of a lookup column"},"content":{"rendered":"<p>Lookup values are internally referenced as a combination of ID(int) and field value(string) in the following format: 1;#FirstItem.<br \/>\nThe class offered by SharePoint for lookup values is SPFieldLookupValue. The method SPFieldLookupValue takes 1 or 2 values:<\/p>\n<ol>\n<li>\u201c1;#FirstItem\u201d (string)<\/li>\n<li>lookupId and lookupValue. Obviously the lookupId represents the field\u2019s ID, lookupValue represents the field\u2019s value.<\/li>\n<\/ol>\n<p>Obviously you have to know the ID and value of the new item to set to the lookup field.<br \/>\nTo enumerate all the possible items of the lookup list:<\/p>\n<p>string itemsListed = string.Empty;<br \/>\nSPListItemCollection sources = web.Lists[&#8220;Source&#8221;].Items;<br \/>\nforeach (SPListItem item in sources)<br \/>\n{<br \/>\n\u00a0\u00a0\u00a0 itemsListed += item[&#8220;ID&#8221;].ToString() + &#8220;;#&#8221; + item[&#8220;Title&#8221;].ToString() + System.Environment.NewLine;<br \/>\n}<\/p>\n<p>An example of the content of itemsListed:<br \/>\n1;#FirstItem<br \/>\n2;#SecondItem<br \/>\n3;#ThirdItem<\/p>\n<p>To search for a specific item you can use a CAML query:<\/p>\n<p>SPQuery query = new SPQuery();<br \/>\nstring toSearchFor = &#8220;ThirdItem&#8221;;<br \/>\nquery.Query = &#8220;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name=&#8217;Title&#8217; \/&gt;&lt;Value Type=&#8217;Text&#8217;&gt;&#8221; + toSearchFor + &#8220;&lt;\/Value&gt;&lt;\/Eq&gt;&lt;\/Where&gt;&#8221;;<br \/>\nSPListItemCollection collection = web.Lists[&#8220;Source&#8221;].GetItems(query);<br \/>\nIf(collection.Count == 1)<br \/>\n{<br \/>\n\u00a0\u00a0\u00a0 int idOfItem = int.Parse(collection[0][&#8220;ID&#8221;].ToString());<br \/>\n\u00a0\u00a0\u00a0 string totalLookup = idOfItem.ToString() + &#8220;;#&#8221; + toSearchFor;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lookup values are internally referenced as a combination of ID(int) and field value(string) in the following format: 1;#FirstItem. The class offered by SharePoint for lookup values is SPFieldLookupValue. The method SPFieldLookupValue takes 1 or 2 values: \u201c1;#FirstItem\u201d (string) lookupId and &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/programmatically-change-the-value-of-a-lookup-column\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"class_list":["post-249","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2007","tag-moss-2007"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=249"}],"version-history":[{"count":1,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/249\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/249\/revisions\/250"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}