2

I'm calling listdata.svc with xmlhttprequest and POST to pass json. The new item is being created, but a new item is being created on the lookup list when I do, even though I am passing an existing lookup. Does anyone have any idea how to stop this behavior?

Here is the json:

{ "Title" : "Title", "lookup": { "__metadata":{ "uri": "/_vti_bin/listdata.svc/ListName(LookupId)", "type": "Microsoft.SharePoint.DataService.ListNameItem"} "LookupField": "lookupDisplayValue", "Id" : "LookupID"}, OtherFields...}

I've tried with one or the other of uri and type metadata. It works with either, but at least one needs to be present.

2 Answers 2

2

I had exactly the same situation and have solved it. I got to the solution by looking through the JSON returned by the service during a GET call for all list items on a list which included a Lookup column to another list.

When there is a Lookup column returned, you always see the name of the column, with a __deffered field attached to it, with a uri to the specific list, item id and column name. Beneath that, there is another field with has the same name, but has Id tacked onto the end of the name. It has an int value associated with it. That int value is a reference to the item id in the list you are looking up

So when you do a post and you update a Lookup field (and you do not want a new item inserted into the lookup list), just tack Id onto the end of the column name and pass it the Id int value of the item in the lookup list.

So for your code, you had:

{ 
"Title" : "Title", 
  "lookup": { 
    "__metadata": { 
      "uri": "/_vti_bin/listdata.svc/ListName(LookupId)", 
      "type": "Microsoft.SharePoint.DataService.ListNameItem"} 
    "LookupField": "lookupDisplayValue", 
    "Id" : "LookupID"
  }, 
  OtherFields...
}

If we change this to the following, it should work:

{ 
  "Title" : "Title", 
  "lookupId": 3, 
  OtherFields...
}
1

while I can´t see the error in you code , SPService is always worth a try an 10 times worth the bytes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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