ListItem
resource does not expose ServerRelativeUrl
property, you could use FileRef
property instead. But still it does not seem possible to accomplish using SharePoint 2013 REST since FileRef
property could not be retrieved for a projected field.
And again SharePoint 2010 REST comes to the rescue!
The following query returns Lookup field file url:
/_vti_bin/listdata.svc/Documents?$select=DocumentImage/Name,DocumentImage/Path&$expand=DocumentImage
where DocumentImage
is a Lookup field
The expression to construct a File url:
fileUrl = DocumentImage.Path + '/' + DocumentImage.Name;
Example
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var selectFields = ['Title','DocumentImage/Name','DocumentImage/Path'];
var expandFields = ['DocumentImage'];
getListItems(webUrl,'Documents',selectFields,expandFields)
.done(function(data){
for(var i = 0; i< data.d.results.length;i++) {
var image = data.d.results[i].DocumentImage;
if(image != null) {
var imageUrl = image.Path + '/' + image.Name;
console.log(imageUrl);
}
}
});
where
function executeRequest(url,method,headers,payload)
{
if (typeof headers == 'undefined' || headers == null){
headers = {};
}
headers["Accept"] = "application/json;odata=verbose";
if(method == "POST") {
headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
}
var ajaxOptions =
{
url: url,
type: method,
contentType: "application/json;odata=verbose",
headers: headers
};
if(method == "POST") {
ajaxOptions.data = JSON.stringify(payload);
}
return $.ajax(ajaxOptions);
}
function getListItems(webUrl,listName,selectFields, expandFields){
var endpointUrl = webUrl + "/_vti_bin/listdata.svc/" + listName;
endpointUrl+= '?$select=' + selectFields.join(",");
endpointUrl+= '&$expand=' + expandFields.join(",");
return executeRequest(endpointUrl,'GET');
}