Q:

Can't retrieve metadata from a document in SharePoint inside a folder in javascript client object model

The below code is working fine for me when the document that contains the meta data isn't inside a folder. As soon as the document is placed into a folder in the document library it stops working after while (enumerator.moveNext()) { I've placed console.log in seperate lines in the code and that is the point at which i stop getting a response.
I've created a new dispform.aspx called viewitem.aspx, and this js file is linked to it to pull the metadata out and display it in different divs on the page. 
below is the code: 
function getBody() {
    var context = new SP.ClientContext("https://xxx.sharepoint.com/sites/xxx");
    var list = context.get_web().get_lists().getByTitle('Docs');
    JSRequest.EnsureSetup();
    var listItemId = GetUrlKeyValue("ID");
    var item = new SP.CamlQuery();
    item.set_viewXml("<View><Query><Where><Eq><FieldRef Name='ID'/><Value Type='Number'>" + listItemId + "</Value></Eq></Where></Query></View>");
    returnedItemsBE1 = list.getItems(item);
    context.load(returnedItemsBE1);
    context.executeQueryAsync(onSucceededCallbackBE1);
        }
        function onSucceededCallbackBE1() {
            var enumerator = returnedItemsBE1.getEnumerator();
            while (enumerator.moveNext()) {
                var listItem = enumerator.get_current();
                var tmbod = listItem.get_item('TmBody');
                var tmfile = listItem.get_item('TmAttachmentTitle');
                var tmId = listItem.get_item('TmTransID');
                var tmfileurl = 'https://xxx.sharepoint.com/sites/xxx/Docs/'+tmId+'_'+tmfile;
                document.getElementById("fileLink").href = tmfileurl;
                document.getElementById("fileLink").innerText = tmfile ;                                    
                var bdiv = document.getElementById("bodyDiv");
                bdiv.innerHTML = tmbod;
                    }                                           
                }                               

`
I'm using SharePoint 2013 online, I can't figure out how the item being in a folder can make any difference, the URL changes but it still contains the file ID etc... 
I've searched and searched and can't find any reference to this, any help would be appreciated!

A:

Try using Scope='Recursive' in you View tag. Else, it will only get the files in the given directory.
item.set_viewXml("<View Scope='Recursive'><Query><Where><Eq><FieldRef Name='ID'/><Value Type='Number'>" + listItemId + "</Value></Eq></Where></Query></View>");

There are four view scopes:

Default (no Scope given): retrieve all the files in the current folder
All: retrieve files and folders in the current folder
Recursive: retrieve files in the current folder and its sub-folders, and in their sub-folders, etc.
RecursiveAll: same as Recursive, but also retrieves the sub-folders.

I found this handy page if a visual explanation works better for you.

