3

Conditions: SP 2010, html/javascript, using REST to read/write to various lists. Im using a jquery multiselect plugin to set a multi-value lookup column. I need the selecting Groups of items, easy filtering, etc. features. http://www.erichynds.com/blog/jquery-ui-multiselect-widget

On this form, one of the columns I need to update is a multi-value lookup column. I have successfully built the array it wants with the __metadata object, like so:

{ __metadata: { uri: myURI + myLookupId + ")" } }

This works great to send new lookup values to the column, but SharePoint does not replace the lookup values with the array of values I am sending it, it only adds new ones.

Is there a way to bulk remove the values in the lookup column? Wipe it clean? So I can have it be empty before I send it my array of lookup values?

I found how to delete the lookup values one by one, but have had no success finding a way to clear all the values.
http://yetanothersharepointblog.wordpress.com/2013/03/29/updating-sharepoint-multi-value-lookup-fields-using-the-rest-api/

If possible I would like to avoid writing a helper function that tracks every time a user checks or un-checks a box, then compares that to the columns lookup values, etc.

If there is a way to do clear the lookup column via the sharepoint object model instead of REST, I'm ok with that too.

Thanks for any help!

Here is the ajax call in case it will help. Maybe there is a special way of sending it so it replaces the data?

// user clicks submit, fires this function
// The url and columns (mybody) are stored and sent to the function

function UpdateMtgItem(url, mybody) {

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    processData: false,
    beforeSend: beforeSendFunction,
    url: url,
    data: mybody,
    dataType: "json",
    success: function (data) { alert("Saved."); },
    error: function () { alert("error"); }
}); beforeSendFunction = function (xhr) {
    xhr.setRequestHeader("If-Match", "*");
    // Using MERGE so that the entire entity doesn't need to be sent over the wire.
    xhr.setRequestHeader("X-HTTP-Method", 'MERGE');
}
}
1

Unfortunately REST api doesn't support this. One of the solutions would be to use SharePoint Batch API and send ajax request to Lists.asmx web service. It's possible to add/update/delete several items in one request through Batch API. Here is how it might look like:

var batchCommandXml = '<?xml version="1.0" encoding="utf-8"?> \
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" \
     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body> \
  <UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">\
    <listName>Contacts</listName> \
    <updates>\
    <Batch OnError="Continue"> \
      <Method ID="1" Cmd="Delete"> \
          <Field Name="ID">10</Field> \
      </Method> \
      <Method ID="2" Cmd="Delete"> \
          <Field Name="ID">11</Field> \
      </Method> \
    </Batch>\
    </updates>\
  </UpdateListItems></soap:Body></soap:Envelope>';

$.ajax({
  type: 'POST',
  url: "http://server/sites/_vti_bin/Lists.asmx",
  contentType: 'text/xml',
  dataType: 'xml',
  data: batchCommandXml,
  success: function () {},
  headers: {
    'SOAPAction': 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems'
  },
  error: function () {}
});

Here are more details about Batch API syntax:

http://msdn.microsoft.com/en-us/library/websvclists.lists.updatelistitems(v=office.14).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.