2

I am developing SharePoint Hosted App to get specific file's all properties like author, created time, modified time, last major version etc.

I didn't get the author name of file and I got SPUser object in console. How can I get user name from SPUser object?

Also for version I got SPFileVersionCollection object, how to get last major version from that?

Here my developed code

 function getFileInfo(SiteRelativeUrl, FileUrl)
{
    var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
    hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
    var context = new SP.ClientContext(appweburl);        
    var appContextSite = new SP.AppContextSite(context, hostweburl + SiteRelativeUrl);
    console.log(hostweburl + SiteRelativeUrl);
    var web = appContextSite.get_web();
    var file = web.getFileByServerRelativeUrl(FileUrl);
    context.load(file);
    context.executeQueryAsync(function () 
    {    
        console.log(file.get_title());
        console.log(file.get_timeCreated());
        console.log(file.get_timeLastModified());
        console.log(file.get_modifiedBy());
        console.log(file.get_versions());
        console.log(file.get_author().get_userId());
    }, function (sender, args) {
        console.log('failed. Message:' + args.get_message());
    });
}
    $(document).ready(function () {        
    getFileInfo('/', '/Shared%20Documents/try_getalllistwidhiddent.txt');
});

function getQueryStringParameter(param) {
var params = document.URL.split("?")[1].split("&");
//var strParams = "";    
for (var i = 0; i < params.length; i = i + 1) {
    var singleParam = params[i].split("=");
    if (singleParam[0] == param) {
        return singleParam[1];
    }
}

}

1

You can get user details by its id like following:

function getUserById(userId) {

    var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
    hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
    var context = new SP.ClientContext(appweburl);
    var appContextSite = new SP.AppContextSite(context, hostweburl + SiteRelativeUrl);
    var web = appContextSite.get_web();
    var user = web.get_siteUsers().getById(userId);
    context.load(user);

    context.executeQueryAsync(function() {
        console.log(user.get_title());
    }, function() {

    });
}

You don't have to find last major version from SPFileVersionCollection. SP.File has property named get_majorVersion (). See in MSDN

Just use following

console.log(file.get_majorVersion());
0

To get the full name of the SP.User object use:

.get_lookupValue()

I am not sure how to get the last version from version collection, but have a look at THIS.

Unfortunately there's no method or property in SP.FileVersionCollection object to get the last major version, but I think you can somehow loop through the versions?

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.