Unable to fetch versionItem.get_createdBy().get_title(). Error Title is not initialize. Please help
-
11-01-2021 - |
Вопрос
I have a problem in JSOM code for SharePoint. Below is the description:
When I try to fetch get_createdBy from FileVersion object it returns SP.User object and when I try to fetch user title or loginName ( .get_title() or .get_loginName() ) from SP.User object I get an error that Title nor loginName is not initialize. Below is my code without createdBy and is working fine with no errors and I am able to get all other details from a FileVersion object except createdBy. Any help would be appreciated.
var totalVersions = 0;
var versionDetailsCounter = 0;
function getVersionHistory() {
var fileVersions = clientContext.get_web().getFileByServerRelativeUrl(_spPageContextInfo.serverRequestPath).get_versions();
clientContext.load(fileVersions);
clientContext.executeQueryAsync(Function.createDelegate(this, function () { onGetVersionHistorySuccess(fileVersions); }), onFail);
}
function onGetVersionHistorySuccess(fileVersions) {
var objlistVersionEnumerator = fileVersions.getEnumerator();
totalVersions = fileVersions.get_count();
vTableHtml = "";
while (objlistVersionEnumerator.moveNext()) {
var vItem = objlistVersionEnumerator.get_current();
vTableHtml += "<tr> <td>" + vItem.get_versionLabel() + "</td> <td>" + formatDate(vItem.get_created(), 2) + "</td> <td>" + vItem.get_checkInComment() + "</td> </tr>";
versionDetailsCounter++;
}
if (totalVersions === versionDetailsCounter) {
if (vTableHtml) {
vTableHtml = "<div id='lblVersionHistory'><h5><strong>Version History</strong></h5></div><table class='table'><thead><tr><th>Version No.</th><th>Modified</th><th>Comments</th></tr></thead><tbody>" + vTableHtml + "</tbody></table>";
$('#divVersionHistory').html(vTableHtml);
} else {
vTableHtml = "Version history not available.";
}
}
}
Решение
You have to include the extra props you need on the load:
clientContext.load(fileVersions, "Include(CreatedBy)");
Since you are using other props too, you'll need those in the include:
clientContext.load(fileVersions, "Include(CreatedBy,VersionLabel, Created, CheckInComment)");
You can reduce the size of the data returned from the query if you specify the load
to only include the data you use:
clientContext.load(fileVersions, "Include(CreatedBy.LoginName,VersionLabel, Created, CheckInComment)");