Update Single/Multiple Lookup field in SharePoint 2010 using WCF Data Services, REST API, C#

If you want to use SharePoint 2010 WCF Data Services to update SharePoint lookup field in a list; you need remember following things:

To update single Lookup field: Use Id to reference to the looked up list object to the target list object similar as the code below:

item.CompetencyId = competency.Id; //Add looked up list item object by ID
dc.UpdateObject(item);             //Update the target object
dc.SaveChanges();		      //Save changes from data context

 

To update a lookup field that allow multiple values: Use the AddLink(object source, string sourceProperty, object target) from datacontext with the System.Data.Services.Client.SaveChangesOptions.Batch mode similar as the code below:

//Add multiple link to the target list object
foreach(var competency in competencies)
	{
		dc.AddLink(item, "Competency", competency);
	}
dc.UpdateObject(item);               
//SaveChanges in batch mode
dc.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);

 

Hope this help!

This Post Has 0 Comments

  1. Duc Tuan

    Thầy Quang viết 1 bài về ứng dụng kết nối và truy cập csdl SQLite cho bọn em tham khảo với

  2. Duc Tuan

    Úng dụng database cho nền tảng WP8 và viết theo mô hình chuẩn MVVM

Leave a Reply