LinkedIn

Thursday, October 20, 2016

Progrmatically set Lookup Field Value in a SharePoint List using CSOM

Programmatically set Lookup Field Value in a SharePoint List using CSOM



 List> data = new List>();
                                    data = GetLookupIDFromList(clientContext, "Account", "AccountID", "Some Value");

ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                                    ListItem newItem = oList.AddItem(itemCreateInfo);
                                    newItem["Title"] = row.Field(0);
                                    newItem["OpportunityName"] = row.Field(1);
                                    if (data != null)
                                    {
                                        FieldLookupValue fld = new FieldLookupValue() { LookupId = int.Parse(data[0].Key) };
                                        if (int.Parse(data[0].Key) > 0)
                                        {
                                            newItem["AccountID"] = fld.LookupId;
                                            newItem["AccountName"] = fld.LookupId;
                                        }
                                    }




   public static List> GetLookupIDFromList(ClientContext context, string listname, string strLookupColumnName, string strLookupValue)
        {
            try
            {
                List> data = new List>();
                List list = context.Web.Lists.GetByTitle(listname);
                //string strCAMLQuery = "" + strLookupValue + "";
                string strCAMLQuery = "" +
                                   "" +
                                      "" +
                                         "" +
                                         "" + strLookupValue + "" +
                                      "
" +
                                   "
" +
                                "
";
                //If you are developing console application, use 
                // System.Web.HttpUtility.HtmlEncode(strLookupValue) instead of
                //System.Web.HttpContext.Current.Server.HtmlEncode(strLookupValue)

                CamlQuery query = new CamlQuery();
                query.ViewXml = @"" + strCAMLQuery + "";
                ListItemCollection items = list.GetItems(query);
                context.Load(items);
                context.ExecuteQuery();
                if (items.Count > 0)
                {
                    // int iRet = items[0].Id;

                    data.Add(new KeyValuePair(items[0].Id.ToString(), items[0].FieldValues["AccountName"].ToString()));
                    return data;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

4 comments:

  1. Historical sights are aplenty. At Jalan Tun. H.S. Lee (formerly known as High Street), the Kwang Siew Association harks back to 1888. Its temple roofs and eaves are adorned with stone dragons and mythical creatures, while two stone lions watch over its entrance. Proceeding south will bring you to the Sri Maha Mariamman Temple and the High Street Police Station, which was built in Watch New Jobs Alerts On Jawatankosong.

    ReplyDelete
  2. Moves to reduce corporate and financial risks in China have also hit sentiment. Sweeping new rules for the asset management industry, a crackdown on micro loans and losses imposed on the creditors of the state-owned Chongqing Iron & Steel have jolted markets, pushing government bond yields to three year highs and causing a sharp drop in stock Watch New Jobs Alerts On Jawatankosong.

    ReplyDelete
  3. Pune is one of the foremost industrial cities in India and has been contributing gradually to several small, medium sized industries and offering jobs to more than thousands of people every year. Pune has India's growing IT, manufacturing, engineering and automobile Watch Latest Jobs Alerts On Jawatankosong.

    ReplyDelete
  4. Najib has denied wrongdoing and has consolidated power by sacking critics within his ruling party and cracking down on Watch Jobs in Malaysia 2018 On Jawatankosong.

    ReplyDelete