SharePoint 2010 REST w/Lookup - Working Example

SharePoint 2010 REST w/Lookup - Working Example

pvestalpvestal Posts: 1Questions: 0Answers: 0
<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="/jQuery/REST/jquery-1.8.2.min.js"></script>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="/jQuery/REST/jquery.dataTables.css">
<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="/jQuery/REST/jquery.dataTables.min.js"></script>

<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("LoadPositions");
function LoadBINs() {
     
}
</script>
<script type="text/javascript">

function LoadPositions()  {
//remove alert in production
alert("Function running");

var call = $.ajax({
            
    url: "https://internalportal/_vti_bin/listdata.svc/CustomerRequests?$select=PositionNumber,Id,CreatedBy&$expand=CreatedBy",
            
    type: "GET",
        dataType: "json",
        headers: {
                Accept: "application/json;odata=verbose"
            }
       
        }); 


        call.done(function (data,textStatus, jqXHR){
            $('#htmlTable').dataTable({
                "bDestroy": true,
                "bProcessing": true,
                "aaData": data.d.results,
                "aoColumns": [
                    { "mDataProp": "PositionNumber" },
            { "mDataProp": "Id" },
                    { "mDataProp": "CreatedBy.Name" }

                ]
              });
        });
        call.fail(function (jqXHR,textStatus,errorThrown){
            alert("Error retrieving Tasks: " + jqXHR.responseText);
        });


}

</script>

//table headers must be same amount as data property fields
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="display" id="htmlTable">
<thead><th>Position Number</th><th>SharePoint List ID</th><th>Created By</th></thead>
</table>

This link was helpful to explain the difference in 2013 and 2010 $expand use: https://www.robertkaucher.com/tech/2014/2/27/expand-and-lookup-fields-in-sharepoint-2013

This was also helpful: http://www.andrewconnell.com/blog/Applying-Filters-to-Lookup-Fields-with-the-SP2013-REST-API

Replies

  • AbhisheksahayAbhisheksahay Posts: 5Questions: 2Answers: 0

    Getting a strange error as :"d.dequeue is not a function", have used dataTables earlier in SP Online versions for REST call and never seen this before and it worked fine.
    Any help would be appreciated.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    If you link to a page showing the issue, I'd be happy to take a look.

    Thanks,
    Allan

  • J33g73J33g73 Posts: 6Questions: 0Answers: 0
    edited May 2018

    @pvestal I just went through this myself. The REST API is missing a field to expand. CreatedBy is the lookup field, but what field is it looking up?

    Change From: CreatedBy&$expand=CreatedBy
    Change To: CreatedBy/fieldname&$expand=CreatedBy

    Also keep in mind the returned result will be nested.

This discussion has been closed.