1

I have a custom list in SharePoint 2013. This list contains text and lookup fields. While I do not have an issue populating the text fields, the lookups are giving me trouble.

I found and tried this example from MSDN 'AssignedToId': {"results":[1]} but I had no luck. Is the 1 the ID of the value?

I have a drop down for the following items.

<select id='Division'>
    <option value=''>-- choose division --</option>
    <option value='1'>Selection1</option>
    <option value='2'>Selection2</option>
    <option value='3'>Selection3</option>
</select>

The values for each option are the list IDs in the source list below is the JavaScript I have been using. If Division is in the JSON my add item fails. If I remove Division completely the new item will be created populating all the other fields. If someone has a better method I would appreciate it.

var divisionId = parseInt($("#Division").val());

var item = {
    "__metadata": { "type": itemtype },
    "Division": {"results":[divisionId]},
    .    
    .    
    .    
    .    
};

$.ajax({
    url: url + "/_api/web/lists/getbytitle('listName')/items",
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(item),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        alert("Successful")
    },
    error: function (data) {
         alert("failed");
    }
}); 

Thanks in advance
Carlos

  • Is it a requirement to use JSON.. What are you working on, is it a Visual Web Part, a SharePoint page.. ? – Arsalan Adam Khatri Jan 17 '14 at 20:53
  • it is client side. The js is being referenced from SharePoint page – lopezcs Jan 17 '14 at 21:02
  • Just to be clear --- You want to dynamically inject a new option into a select list at runtime via javascript that you include on the page? – tyshock Jan 17 '14 at 21:05
  • I have a form, and when I save it, the code should create a new item in a SharePoint Custom list. I have a field that is a lookup but not required and if a try to include it, the POST does not work. I missed to mention, I'm using REST API – lopezcs Jan 17 '14 at 21:11
  • This helped thanks, one thing I realized is the following. I removed the Division from the REST call, and created a successful item in the list. When I looked at the response for the fields there was no Division field but a DivisionId. When I made this change in rest to "DivisionId": divisionId, even though the lookup field name is Division it worked. I'm just wondering if this is the proper way, or is it more like a hack – lopezcs Jan 20 '14 at 15:10
1

What if you try:

var divisionId = parseInt($("#Division").val());

var item = {
    "__metadata": { "type": itemtype },
    "Division": divisionId,
    .    
    .    
    .    
    .    
};
1

The above answer is incorrect. As someone else has suggested - if you have a lookup field named 'xyz' you need to use 'xyzId' when saving value to it. It's a bit confusing but it works. I had a similar problem with lookup field 'QuoteID' and it works after I changed it to 'QuoteIDId'

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.