Updating SharePoint multi-value Lookup Fields using the listdata.svc REST API

Sharepoint Lookup columns that allow multiple values are represented as a separate collection in the REST interface. For instance, if you have a list named BudgetLine and a list named Account, and BudgetLine has a  multi-value lookup column called Account that looks up the Title on the Account list, you would use the following url to get a list of  Accounts for the BudgetLine with an Id of 1:

/_vti_bin/listdata.svc/BudgetLine(1)/Account

To add a new Account to the BudgetLine with an ID of 1  you would do a POST to

/_vti_bin/listdata.svc/BudgetLine(1)/Account

with an  http header ‘X-HTTP_method: MERGE’, and in the JSON Body, put the uri  of the  Account you want to add:

{
“__metadata”: {
“uri”: “https://rgove2.sharepoint.com/KnockoutSP/_vti_bin/listdata.svc/Account(3)”
}
}

To  remove the Account with an id of 2 from the BudgetLine with an Id of 3 you could do a  delete on

/_vti_bin/listdata.svc/BudgetLine(3)/$links/Account(2)

deletelookupitem

Note that if you do a DELETE on

/_vti_bin/listdata.svc/BudgetLine(3)/Account(2)

without the $links Sharepoint will  delete the Item from the Accounts list rather than just removing it from the lookup.

You CANNOT delete all the lookup values in a single DELETE call to

/_vti_bin/listdata.svc/BudgetLine(3)/$links/Account

This returns an error (…/$links/Account/’ is not valid for DELETE operation. The URI must refer to a single resource from an entity set or from a property referring to a set of resources.)

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

9 Responses to Updating SharePoint multi-value Lookup Fields using the listdata.svc REST API

  1. Scott says:

    Is there a way to remove all the values from the multi value lookup column without needing all the specific ID numbers?

    • russgove says:

      Great question, thanks. I thought maybe I could just do an http Delete on /_vti_bin/listdata.svc/BudgetLine(2)/Account but i got an error back saying
      “The URI ‘https://xxxx.sharepoint.com/KnockoutSP/_vti_bin/listdata.svc/BudgetLine(2)/Account/’ is not valid for DELETE operation. The URI must refer to a single resource from an entity set or from a property referring to a set of resources.”

      I did some searching for this error message and found a discssion at http://stackoverflow.com/questions/16010578/odata-wcf-unable-to-perform-post-put-delete
      that says “The PUT and DELETE verbs must refer to a specific entity, not an entity set.”

      So I guss we cant do that.

      • Scott says:

        Thanks for researching that! Is there a way to remove the /Account item value out of the BudgetLine’s lookup column instead of removing the Account list item? When I run a delete it deletes the list item, instead of just removing it from the lookup column. :/

  2. russgove says:

    If you do a delete on /_vti_bin/listdata.svc/BudgetLine(3)/Account(2) it deletes the BudgetLine(3)?… Let me know some details (your lists & code) and I’ll see if I can help.

  3. You need to send a delete to /_vti_bin/listdata.svc/BudgetLine(3)/$links/Account(2)

    If you omit $links it will not only delete the entry in the primary list item but delete the actual item from the lookup list as well.

    http://sharepoint.stackexchange.com/questions/65650/rest-api-update-a-muli-value-user-field/65745#65745

  4. Kris says:

    Hi Russel,

    Great info, I’ve been looking for this for a long time. I am still however running into an issue adding the lookup. I have a product, and a product feature . The product can have multiple features and is a multi lookup to the features list. When I try the above, I get an error back URI http://catalog/_vti_bin/listdata.svc/Products(1331)/Features is not valid for PUT operations. The URI must point to a single resource for PUT operations. Any help would be greatly appreciated.

    Here’s a code snippet that is throwing the error
    var body = { “__metadata”: {“uri”: feature.__metadata.uri }};
    //now lets update the lookup field on the product to point to this new feature.
    $.ajax({
    url: product.__metadata.uri + “/Features”,
    contentType: “application/json;odata=verbose”,
    method: “POST”,
    headers: {
    “Accept”: “application/json;odata=verbose”,
    “X-RequestDigest”: requestDigest,
    “X-HTTP-Method”: “MERGE”,
    “If-Match”: product.__metadata.etag
    },
    body: JSON.stringify(body),
    success: function(results) {
    //The feature has successfully been linked, lets reload the features list on the interface
    getProductFeatures(productId);
    },
    error: function (error, x, y) {
    var msg = error.status + ” ” + error.statusText + “Error Saving linking feature to the product”;
    if (error.responseJSON)
    msg += error.responseJSON.error.message.value;
    webServiceErrorHandler(msg);
    }
    });

    • Chris Mercer says:

      Kris, did you ever figure this out? I’ve been trying to get this to work for almost a month and I finally stumbled across this blog and am having the exact same issue you describe… “The URI ‘http://(sp)/(site)/_vti_bin/listdata.svc/list(item)/choicefieldlist/’ is not valid for PUT operations…

  5. Pingback: Clear all values in a multi-value lookup, via REST, SP 2010 | DL-UAT

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