Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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... }

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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