sharepoint add lookup field programmatically-client object model

Add lookup field programmatically sharepoint 2010-client object model

Insert the following namespace

using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;

Use the following code.

   ClientContext ccsite;
   List listobj;
   List listobj_lookup;            //Object for lookup list
   ListItemCollection _icoll;
  public MainPage()
      {
           InitializeComponent();

           ccsite = new ClientContext(ApplicationContext.Current.Url);
           ccsite.Load(ccsite.Web);
           listobj = ccsite.Web.Lists.GetByTitle("ListName");
           listobj_lookup= ccsite.Web.Lists.GetByTitle("ListName");     // (LookUp List)
           ccsite.Load(listobj);
        
            CamlQuery qry = new CamlQuery();
            qry.ViewXml = "<View/>";

            _icoll_lookup= listobj_lookup.GetItems(qry);
            ccsite.Load(_icoll_lookup);         
            ccsite.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(success), null);
       }

 private void success(object sender,ClientRequestSucceededEventArgs arg)
    {
         Dispatcher.BeginInvoke(datacon);
     }

private void datacon()
   {
            //Geting List Item Record ID of lookup value
            int recordID = 0;
            foreach (ListItem item in _icoll_lookup)
            {
                if (Convert.ToString(item["ListItmeValue"]) == "YourString")
                { recordID = Convert.ToInt32(item["ID"]); }
            }

            //For insert Lookup value
            ListItem litem = listobj.AddItem(new ListItemCreationInformation());
            FieldLookupValue lookupobj = new FieldLookupValue { LookupId = recordID };
            litem["ColumnName1"] = lookupobj as FieldLookupValue;
            litem.Update();
            ccsite.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(succed), null);
    }

private void succed(object sender, ClientRequestSucceededEventArgs arg)
    { }

For Insert Multiple Lookup Values Click Here

Comments

Popular posts from this blog

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

Add content type to SharePoint List/Library using REST API

A type named 'SP.Data. could not be resolved by the model error