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);

Reference: http://basquang.wordpress.com/2013/05/17/update-singlemultiple-lookup-field-in-sharepoint-2010-using-wcf-data-services-rest-api-c/

Another Way:

<asp:CheckBoxList ID=”cblCustomer” runat=”server”></asp:CheckBoxList>

Code-Behind:

SPFieldMultiChoiceValue multiChoiceCustomer = new SPFieldMultiChoiceValue();
foreach (ListItem listItem in cblCustomer.Items)
{
if (listItem.Selected)
{
multiChoiceCustomer.Add(listItem.Value.ToString() + “;#” + listItem.Text.ToString());
}
}
item[“Customer”] = multiChoiceCustomer;

item.Update();

 

Advertisements
This entry was posted in Outlook, SharePoint 2010, Visual Studio 2010. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s