Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, January 9, 2018 9:50 AM
Hi everyone
i have lookup field in a list , i want to change the look value of lookup field here is my code. existing value is "Jhon" i want to replace it with "Clark"
public void load()
{
try
{
using (ClientContext context = new ClientContext("http://IP Address/Leaves/"))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new NetworkCredential("username", "********");
Web webObj = context.Web;
List listObj = webObj.Lists.GetByTitle("Leaves");
//CamlQuery camlquery = CamlQuery.CreateAllItemsQuery(200000);
DateTime dtStart = Convert.ToDateTime("11/14/2016 4:25:06 PM");
string dtStartDate = dtStart.ToString("yyyy-MM-ddTHH:mm:ssZ");
string dtEndDate = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");
Microsoft.SharePoint.Client.CamlQuery camlquery = new CamlQuery();
camlquery.ViewXml = "<Where><And><Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtStartDate + "</Value></Geq>" + "<Leq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtEndDate + "</Value></Leq></And></Where>";
ListItemCollection listitemcoll = listObj.GetItems(camlquery);
context.Load(listitemcoll);
context.ExecuteQuery();
if (listitemcoll.Count != 0)
{
foreach (ListItem item in listitemcoll)
{
int ID = Convert.ToInt32(item["ID"]);
var listItem = listObj.GetItemById(Convert.ToInt32(ID));
context.Load(listItem);
context.ExecuteQuery();
FieldLookupValue lookup = item["Employee"] as FieldLookupValue;
string lvalue = lookup.LookupValue; \\ existing value is "jhone" new value "Clark"
int lId = lookup.LookupId;
lookup.LookupId = lId;
item["Employee"] = lookup;
item.Update();
context.ExecuteQuery();
}
}
else
{
//write messages to client - no data is present
}
}
}
catch (Exception)
{
throw;
}
}
All replies (4)
Tuesday, January 9, 2018 10:14 AM
Hi Razim,
To update a lookup field using client side object model, ID of the item from the lookup list is required. This id can then be used to update the listitem's FieldLookupValue.
Please refer the below article for the same
Try This : https://rmanimaran.wordpress.com/2012/07/16/update-lookup-field-using-client-object-model/
I hope this helps.
Best Regards,
Ramesh
Tuesday, January 9, 2018 10:31 AM
Dear Ramesh i am getting lookupFieldID and lookupvalue now i want to update lookupvalue to a new name.
kindly help
Wednesday, January 10, 2018 6:00 AM
Hi,
To modify the value of lookup field, you need to modify the Id of FieldLookupValue to achieve this. The Id is the source list item id.
We couldn't modify the LookupValue directly because it is read only.
In your situation, you need to create a list item of the source list with the related field value "Clark" if the "Clark" not exists. then remember this item id and set this id to "lookup.LookupId".
Here is a simple demo for your reference.
Below is the source list.
The list that use lookup field.
My Code for you:
class Program
{
static void Main(string[] args)
{
string siteURL = "http://sp/sites/DevSite";
ClientContext context = new ClientContext(siteURL);
ListItem item = context.Web.Lists.GetByTitle("CustomList4").GetItems(new CamlQuery()).GetById(1);
context.Load(item);
context.ExecuteQuery();
FieldLookupValue lv = item["Employee"] as FieldLookupValue;
if (lv == null) lv = new FieldLookupValue();
lv.LookupId = 2;
item["Employee"] = lv;
item.Update();
context.ExecuteQuery();
Console.WriteLine("success");
Console.ReadLine();
}
}
Screenshot of result and you can see the lookup field value has been modified.
Best regards,
Lee Liu
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected]
Monday, January 22, 2018 9:51 AM
Hi,
I am checking to see how things are going there on this issue.
If the issue was resolved, you can mark the helpful post as answer to help other community members find the helpful information quickly.
Best regards,
Lee Liu
Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].
Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.