1

I have a document library which i am making a Ajax call to:

function GetAttachments(){
    $.ajax({
        url: "XXX_api/web/lists/GetByTitle('CM Attachments')/items?$top=5000",
        type: "GET",
        headers: {
            accept: 'application/json',
        },
        success: function(data){
            console.log('All docs below')
            console.log(data)
        },
        error: function(data, error, err, errorCode, errorMessage){
            console.log(data)
            console.log(error)
            console.log(err)
            console.log(errorCode)
            console.log(errorMessage)

        }
    })
}

When inspecting the console.log(data) it returns the array in JSON (as specified in my header) But i can't see any URL's related to the document, i have an odata.editLink: and GUID. Is there any way to return the actual URL of this document?

My reason for wanting this is so i can build a "document" list in a custom form i have. I will make a call inside the form to get the attachments and filter where ID is === to formID.

I'd then like to mimic the documents as attachments (will append an icon for each document, and pass the document URL as a href ) so when they are clicked are opened in a new window. Depending on the document type the outcome should either preview the document or download.

Any help is greatly appreciated.

Cheers.

1

You need to add the EncodedAbsUrl field in the select fields. You can add all the fields that you want using the internal column names. So, modify your endpoint as below:

url: "XXX_api/web/lists/GetByTitle('CM Attachments')/items?
$select=Title,ID,EncodedAbsUrl&$top=5000",

Your full code would be :

function GetAttachments(){
    $.ajax({
        url: "XXX/_api/web/lists/GetByTitle('CM Attachments')/items?$select=Title,ID,EncodedAbsUrl&$top=5000",
        type: "GET",
        headers: {
            accept: 'application/json'
        },
        success: function(data){
            console.log('All docs below')
            console.log(data)
        },
        error: function(data, error, err, errorCode, errorMessage){
            console.log(data)
            console.log(error)
            console.log(err)
            console.log(errorCode)
            console.log(errorMessage)

        }
    });
}

You can bind the Title, ID and the URL field to the UI from the JSON.

enter image description here

  • Thanks @Gautam Sheth, this is exactly what i need. Very helpful! Will comment back once implemented in my code! – Luke Walker Aug 16 '17 at 11:44
0

Change URL to THIS

url: "test-site/_api/web/lists/GetByTitle('CM Attachments')/items?$Select=File/ServerRelativeUrl",
  • If required add File/Name to get Name if the file – Jayakumar Kulkarni Aug 16 '17 at 9:42
0

Use getfolderbyserverrelativeurl in your REST call. This will return document URL in response.

Note : This will not get files from the sub folders.

below is the URL that you can use:

XXX_api/web/getfolderbyserverrelativeurl('CM Attachments')/items?$top=5000

OutPut:

enter image description here

  • I guess this will not bring the files which are inside the folders, for that explicitly call the function again – Jayakumar Kulkarni Aug 16 '17 at 9:45
  • Yes you are right. It will not bring files from the sub folders. I will mention the same in my answer. – Rohit Waghela Aug 16 '17 at 9:50

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.