2

The checked out files can be reached via the UI by browsing Document library --> settings --> Manage Checked out Files. I would like to do it using the usual "function, success, failed" procedure in Javascript. Here is the snippet to fill.

var oList;
function theFunction() {
    clientContext = new SP.ClientContext.get_current();
    web = clientContext.get_web();
    var oList = web.get_lists().getByTitle("MyLibrary");   
    clientContext.load(oList);
    clientContext.executeQueryAsync(onSuccess, onFail);        
}
function onSuccess(sender, args) {
    //console.log(oList.Checkedoutfiles.count);
}
function onFail(sender, args) {
    alert('Request failed.\n' + args.get_message() + '\n' + args.get_stackTrace());
}

Thx.

1 Answer 1

0

It looks like you have to explicitly load the checkOutType (and CheckedOutByUser, if you want it)

    varwebSite = clientContext.get_web();  
    this.list = webSite.get_lists().getByTitle(listname);  
    this.folder = this.list.get_rootFolder();  
    this.files = this.folder.get_files();  
    clientContext.load(this.folder);  
    clientContext.load(this.files, 'Include(CheckOutType,Name,CheckedOutByUser)'); 

Try this, from here:

HTML

<!-- Style required for HTML Snippet -->  
< style type = "text/css" >  
.lst - table th  
{  
    background - color: #ddd;  
    border: 2 px solid# fff;  
    text - align: left  
}  
.lst - table td  
{  
    background - color: #eee;  
    border: 2 px solid# fff;  
}  
.web - heading  
{  
    padding: 2 px;  
} < /style>  
<!--Include jQuery library to perform dynamic html dom manipulation -->  
< script type = "text/javascript" src = "/siteassets/jquery.js" > < /script>     < div >  
< h2 class = "web-heading" > Files with checked - out Status < /h2> < div id = "fldrUrl" > < /div> < /div> < table width = "100%"  
    cellpadding = "10"  
    cellspacing = "2"  
    id = "lstTable"  
    class = "lst-table" >  
< thead >  
< tr >  
< th > File Name < /th> < th > Checked - out Status < /th> < th > Checked - out By < /th> < /tr> < /thead> < tbody > < /tbody> < /table>  

JavaScript

<script type="text/javascript">  
functiongetcheckoutStatus(listname)  
{  
    varclientContext = SP.ClientContext.get_current();  
    if (clientContext != undefined && clientContext != null)   
    {  
        varwebSite = clientContext.get_web();  
        this.list = webSite.get_lists().getByTitle(listname);  
        this.folder = this.list.get_rootFolder();  
        this.files = this.folder.get_files();  
        clientContext.load(this.folder);  
        clientContext.load(this.files, 'Include(CheckOutType,Name,CheckedOutByUser)');  
        clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));  
    }  
}  

functionOnLoadSuccess(sender, args)  
{  
    varfilesEnumerator = this.files.getEnumerator();  
    $('#fldrUrl').html("Folder URL: " + _spPageContextInfo.webAbsoluteUrl + this.folder.get_serverRelativeUrl());  
    while (filesEnumerator.moveNext())  
    {  
        varcurrentFile = filesEnumerator.get_current();  
        vartrow = "";  
        trow += "<tr>";  
        trow += "<td>" + currentFile.get_name() + "</td>";  
        if (currentFile.get_checkOutType() < 2)   
        {  
            trow += "<td>" + currentFile.get_checkOutType() + " - Yes" + "</td>";  
            trow += "<td>" + currentFile.get_checkedOutByUser().get_title() + "</td>";  
        } else {  
            trow += "<td>" + currentFile.get_checkOutType() + " - No" + "</td>"  
            trow += "<td></td>";  
        }  
        trow += "</tr>";  
        //Append each list to the table as a row   
        $("#lstTabletbody").append(trow);  
    }  
}  

functionOnLoadFailed(sender, args)   
{  
    try   
    {  
        console.log('Error: ' + args.get_message() + '\n' + args.get_stackTrace());  
    } catch (err) {}  

}  
functioninjectMethod()  
{  
    getcheckoutStatus("Site Pages");  
}  
ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");  
</script>  
4
  • Thank you for your answer. I guess I can calculate the number of checked out files this way. However, I would like a solution that does not evolve browsing an entire library of 4000+ items to find out only 2 documents are checked out. This script is suppose to be run before my page is displayed. I'm afraid that it will take too long with a heavy library. Plus, my page already contains multiple calls toward the server before rendering. When trying to do the same with PowerShell there is a variable containing only the checked out files. I would like to get it using JavaScript too.
    – ameliapond
    Mar 14, 2017 at 17:22
  • Your question does not reflect what you've commented here. Mar 14, 2017 at 19:33
  • Then call the same endpoint Powershell probably does: msdn.microsoft.com/en-us/library/office/dn450841.aspx Mar 14, 2017 at 19:47
  • Yes I would like to, but as you can see all examples are using query that getting information about a specific file.
    – ameliapond
    Mar 20, 2017 at 16:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.