Search This Blog

Saturday, May 23, 2015

Sharepoint add lookup fielprogrammatically-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)
    { }

No comments:

Post a Comment