2

I have one document library, in that, I have multiple folders.

For example:

  • Foldername1/Foldername2/Foldername3/Text.docx
  • Foldername1/Text.docx
  • Foldername1/foldernmae2/Text.docx

In which folder user want to upload document they can upload.

In this images inside 18 Folder I am having Award, Closeout, and rfp. Inside these 3, I have Documents, Miscellneous, Request for proposal. Inside this I have 64 and 67.

I want to retrive the file from particular folder passing Foldernames

This is my code :

function GetDocumentName(strContractNumber, strChecklistName) {
        //in this url i want to pass strContractNumber and strChecklistName
        url = '/_api/web/lists/getbytitle(\'Documents\')/Items?$expand=Folder&$select=FileLeafRef,FileRef&$filter=FileLeafRef eq \'' + strContractNumber + '\'';
       alert(url)
        $.ajax({
            type: 'GET',
            headers: {
                'accept': 'application/json;odata=verbose'
            },
            async: false,

            url: url,


            success: function (data) {
            $.each(data.d.results, function (index, item) {
              var s = item.FileLeafRef;
             });
            },

            error: function (data) {
            }
        });

    }

In the url, I have to pass the strContractNumber(18) and strChecklistName(Award). Now I want to get the Test.docx document name and store that document name in one string.

I tried this but is not working

url = '/_api/Web/GetFolderByServerRelativeUrl(\'Documents\')/Folders';

How can i acheive this?

1 Answer 1

2

Your answer is:

/getfolderbyserverrelativeurl('/Shared Documents/Folder A')/files

reference: MSDN FileCollection resource

By the way you are calling the api like this:

url = '/_api/web/lists/getbytitle(\'Contract CheckList Documents\')/Items?$expand=Folder&$select=FileLeafRef,FileRef&$filter=FileLeafRef eq \'' + strContractNumber + '\'';

Try this:

url = _spPageContextInfo.webAbsoluteUrl +'/_api/web/getfolderbyserverrelativeurl(\'Contract CheckList Documents\')/files?$expand=Folder&$select=FileLeafRef,FileRef&$filter=FileLeafRef eq \'' + strContractNumber + '\'';

If your site is http://sharepoint.local/sites/site then you have to call like this:

url = _spPageContextInfo.webAbsoluteUrl +'/_api/web/getfolderbyserverrelativeurl(\'sites/site/Contract CheckList Documents\')/files?$expand=Folder&$select=FileLeafRef,FileRef&$filter=FileLeafRef eq \'' + strContractNumber + '\'';

In my case the uri will be:

url = "http://vmsps2013/gestor/_api/web/getfolderbyserverrelativeurl('Documents')/files"

and return this: enter image description here

5
  • I was tried this but is not working how can i get the file name and store it in string. Commented Nov 23, 2016 at 14:42
  • I edited my answer Commented Nov 23, 2016 at 14:45
  • Hi marco in that url i want to pass the string values.First i want to pass 18(Foldername) after i want to pass Award. Then i want to get the Test.docx document. var filename = data.d.Fileref. In filename i want Test.docx name. how can i do this ? Commented Nov 24, 2016 at 6:46
  • I tried like this getFoldername = Documents/18 url = _spPageContextInfo.webAbsoluteUrl + '/' + '_api/web/GetFolderByServerRelativeUrl(' + getFoldername + ')/files'; in alert my url coming like this mysiteurl/_api/web/GetfolderbyServerRelativeUrl(Documents/18)/… but i did not get result Commented Nov 24, 2016 at 7:16
  • I edited my answer Commented Nov 24, 2016 at 13:38

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.