Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a doc's URL using REST API + JQuery in SharePoint 2013?

I am doing basic OData/REST calls to a SP2013 doc library. I am trying to get down to the item's URL and can't determine how to do this. I am very familiar with the server-side object model and understand that the file object is one level deeper than the item. Can someone point me in the right direction or share documentation on how to get down to the file level? I have scoured google. Here's my code that works for simply getting access to all the items in doc library and any metadata columns I wish to target:

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
</html>

<script>
    // workaround for access error
    jQuery.support.cors = true;

    // create REST query
    var requestUri = "http://sp2013/_api/Web/Lists/getByTitle('Documents')/items"; 

    // execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function(data){
            alert(data.d.results);
            $.each(data.d.results, function(index, item){
                if (item["Meta1"] == null) {
                    $("body").append("<h1>No Title</h1>");
                }
                else {
                    $("body").append("<h1>" + item["Meta1"] + "</h1>");
                }
            });
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert(textStatus);
        }
    });
</script>
like image 252
Josh Avatar asked Jan 30 '14 20:01

Josh


2 Answers

For the full url, try:

"http://sp2013/_api/Web/Lists/getByTitle('Documents')/items?$select=EncodedAbsUrl"
like image 96
athom Avatar answered Oct 12 '22 02:10

athom


Use the $select query option with FileRef parameter to return document Url:

https://contoso.sharepoint.com/_api/web/lists/getbytitle('Documents')/items?$select=FileRef

References

Use OData query operations in SharePoint REST requests

like image 27
Vadim Gremyachev Avatar answered Oct 12 '22 01:10

Vadim Gremyachev