Add LookupValue programmatically

Jan 25, 2012 at 3:16 PM

Hey everyone,

i have big problem to add new values to a LookUpWithPicker-Field (Allow multiple values allwoed) programmatically using the Client Object Model.

My source code: (Testcode i try to add one item. the lookup-Field contains 3 items at the moment)

ClientContext remoteCtx = null;
            try
            {
                
                remoteCtx = new Microsoft.SharePoint.Client.ClientContext("http://sharepoint/testsite");
                Microsoft.SharePoint.Client.Web remoteWeb = remoteCtx.Web;
                remoteCtx.Load(remoteWeb);
                remoteCtx.ExecuteQuery();

                List list;

                ListCollection remoteLists = remoteWeb.Lists;
                remoteCtx.Load(remoteLists);
                remoteCtx.ExecuteQuery();

                foreach (var listItem in remoteLists)
                {
                  if (listName.Equals("Contacts"))
                  {
                    list = listItem;
                  }
                }

                ListItem item;

               CamlQuery query = new CamlQuery();
               query.ViewXml = "<Where><Eq><FieldRef Name=\"Title\" /><Value Type=\"Text\">Marco</Value></Eq></Where>";
               ListItemCollection collection = list.GetItems(query);

               remoteCtx.Load(collection);
               remoteCtx.ExecuteQuery();
               item = collection[0];

                ICollection<FieldLookupValue> o = item["rel_picture"] as ICollection<FieldLookupValue>;

  FieldLookupValue[] lookupFieldColl = new FieldLookupValue[4]; // int i = 0; foreach (FieldLookupValue element in o) { Console.WriteLine(element.LookupValue); Console.WriteLine(element.LookupId); FieldLookupValue updateField = new FieldLookupValue(); updateField.LookupId = element.LookupId; lookupFieldColl.SetValue(updateField, i); i++; } FieldLookupValue updateField2 = new FieldLookupValue(); updateField2.LookupId = 17; lookupFieldColl.SetValue(updateField2, i); item["rel_picture"] = lookupFieldColl; item.Update(); remoteCtx.ExecuteQuery(); Console.WriteLine(); } catch (Exception e) { Console.WriteLine(e); } finally { remoteCtx.Dispose(); }

I get this exception "Invalid look-up value" :-(

 

Does anyone know a solution?

 

Greetings form Germany,

Kangee

Jan 31, 2012 at 1:52 AM

Hello to all,

the same question - but just around powershell.

Could you please provide an example?

Will the same approach applicable as for the regular Lookup field - "8#;blah-blah" - smth like this ?

Jan 31, 2012 at 4:31 AM

I've sorted it out. It can be processed in the same way as the regular Lookup field.