0

I have a lookup column that correlates to a background image in my presentation layer. Is it possible to expose the URL to the actual file via the $expand mechanism using REST?

Example REST URI

https://my.sharepoint.com/_api/web/lists/getbytitle('Documents')/items?$select=Title,FileRef,Document_x0020_Image/ServerRelativeUrl&$expand=Document_x0020_Image

If I do a separate REST call to the actual Document Image list there are a couple properties that look useful, the most useful being ServerRelativeUrl. But I get an invalid request error trying to $expand on this property. See below.

The query to field 'Document_x0020_Image/ServerRelativeUrl' is not valid.

I'd very much appreciate any help with this issue. Thanks.

0

1 Answer 1

1

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');
}

Your Answer

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

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