Question

I getting list file of a Doc Library from add-ins:

enter image description here

This is my code:

function getListfile() {
    var hostWebContext = new SP.AppContextSite(clientContext, hostWebURL);
    var listFile = hostWebContext.get_web().get_lists().getByTitle('TestUpFile');
    var camlQuery = new SP.CamlQuery();    
    var Items = listFile.getItems(camlQuery);
    clientContext.load(Items, 'Include(Title, Id)');
    clientContext.executeQueryAsync(Function.createDelegate(this, function () {    
        var oEnumerator = Items.getEnumerator();
        while (oEnumerator.moveNext()) {           
            var id = oEnumerator.get_current().get_id();
            var file = oEnumerator.get_current().get_file();           
            var title = file.get_title();
            //var name = file.get_name();
           //console.log("Name : " + name );
        }
       

    }), Function.createDelegate(this, function (sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

    }));
}

But it occur error at : var title = file.get_title();

Uncaught Error: The property or field 'Title' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
    at Function.Error.create (ScriptResource.axd?d=DExyTm8uKGY1W9ivcBlFvYgQb6day584ROECBXdJixEgkUWRj9HmKa9QWW79COoxmGZWEhQiCsLw0SW_rY202OsYbVH2JF4_8wkor-NZ0i-wlVYG3V5mKlMNzvmEND0f-untBrY1zhKROVXCGZ1WQJH0rr9wOXmJ07Sh0VGA4gsVuFmgDpVrTJS4uF69jYKk0&t=4f7d5f1:5)
    at SP.File.checkUninitializedProperty (sp.runtime.js:2)
    at SP.File.get_title (sp.js:2)
    at App.js:39
    at Array.<anonymous> (ScriptResource.axd?d=DExyTm8uKGY1W9ivcBlFvYgQb6day584ROECBXdJixEgkUWRj9HmKa9QWW79COoxmGZWEhQiCsLw0SW_rY202OsYbVH2JF4_8wkor-NZ0i-wlVYG3V5mKlMNzvmEND0f-untBrY1zhKROVXCGZ1WQJH0rr9wOXmJ07Sh0VGA4gsVuFmgDpVrTJS4uF69jYKk0&t=4f7d5f1:5)
    at ScriptResource.axd?d=DExyTm8uKGY1W9ivcBlFvYgQb6day584ROECBXdJixEgkUWRj9HmKa9QWW79COoxmGZWEhQiCsLw0SW_rY202OsYbVH2JF4_8wkor-NZ0i-wlVYG3V5mKlMNzvmEND0f-untBrY1zhKROVXCGZ1WQJH0rr9wOXmJ07Sh0VGA4gsVuFmgDpVrTJS4uF69jYKk0&t=4f7d5f1:5
    at SP.ClientRequest.$3K_0 (sp.runtime.js:2)
    at Array.<anonymous> (ScriptResource.axd?d=DExyTm8uKGY1W9ivcBlFvYgQb6day584ROECBXdJixEgkUWRj9HmKa9QWW79COoxmGZWEhQiCsLw0SW_rY202OsYbVH2JF4_8wkor-NZ0i-wlVYG3V5mKlMNzvmEND0f-untBrY1zhKROVXCGZ1WQJH0rr9wOXmJ07Sh0VGA4gsVuFmgDpVrTJS4uF69jYKk0&t=4f7d5f1:5)
    at ScriptResource.axd?d=DExyTm8uKGY1W9ivcBlFvYgQb6day584ROECBXdJixEgkUWRj9HmKa9QWW79COoxmGZWEhQiCsLw0SW_rY202OsYbVH2JF4_8wkor-NZ0i-wlVYG3V5mKlMNzvmEND0f-untBrY1zhKROVXCGZ1WQJH0rr9wOXmJ07Sh0VGA4gsVuFmgDpVrTJS4uF69jYKk0&t=4f7d5f1:5
    at Sys.Net.WebRequest.completed (ScriptResource.axd?d=DExyTm8uKGY1W9ivcBlFvYgQb6day584ROECBXdJixEgkUWRj9HmKa9QWW79COoxmGZWEhQiCsLw0SW_rY202OsYbVH2JF4_8wkor-NZ0i-wlVYG3V5mKlMNzvmEND0f-untBrY1zhKROVXCGZ1WQJH0rr9wOXmJ07Sh0VGA4gsVuFmgDpVrTJS4uF69jYKk0&t=4f7d5f1:5)

How can get information of file from add-in?

Was it helpful?

Solution

You need to load the file property in clientcontext like the following:

clientContext.load(Items, 'Include(Title, Id, ContentType, File)');
clientContext.executeQueryAsync(Function.createDelegate(this, function () {    
    var oEnumerator = Items.getEnumerator();
    while (oEnumerator.moveNext()) {           
        var id = oEnumerator.get_current().get_id();
        console.log(id);           
        if(oEnumerator.get_current().get_contentType().get_name() != "Folder"){
            var file = oEnumerator.get_current().get_file(); 
            if(file != null){ 
                        
                var title = file.get_title();
                console.log(title);
                //var name = file.get_name();
                //console.log("Name : " + name );
            }
        } 
        
        
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top