Get Check-out Documents using Javascript
-
04-10-2020 - |
Domanda
Is there a way to get all the checked-out documents in a library? I know this cannot be done by search-driven webparts, but is there a way let's say through REST or something like that?
Soluzione
I can suggest 3 ways to achieve this:
First 2 are REST and Javascript:
You could use SP.File.checkOutType property to determine whether document is checked out of a document library
REST
$.ajax({url: "/_api/web/getFileByServerRelativeUrl('" + docUrl + "')/checkOutType",
headers: { "Accept": "application/json; odata=verbose" },
success: function(data) {
if(data.d.CheckOutType == 0) {
console.log('The file is checked out');
}
}
});
CSOM (JavaScript)
var context = SP.ClientContext.get_current();
var web = context.get_web();
var file = web.getFileByServerRelativeUrl(docUrl);
context.load(file);
context.executeQueryAsync(
function(){
if(file.get_checkOutType() == SP.CheckOutType.online) {
console.log('The file is checked out');
}
},
function(sender, args){
console.log(args.get_message());
}
);
Third one is the easiest based on Spservices:
SpServices
Use SpServices GetListItems
and write a query where you search for all the items where "CheckOutType
"("checkedOutTo
" please check the internal name) is not null. You can then iterate through all the items.
Do let me know if you need help writing the query. Cheers!
Altri suggerimenti
You can try something like this:
var thaUrl = "<app web url>/_api/web/lists/GetByTitle('<library>')//CheckOutType';
jQuery.ajax({
type: "POST",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
contentType: 'application/json',
url: thaUrl,
success: function (data) {
//whatever you want to do if success
},
error: function (err) {
alert('Error, I explode :(');
},
complete: function (item) { }
});
This will return all the files state in a library. Where:
CheckOutType values: Online = 0; Offline = 1; None = 2.
This worked great for me!
_api/web/lists/getByTitle('ListTitle')/items?$select=*,File&$expand=File
You'll get a return inline that contains CheckOutType for all Documents
<content type="application/xml">
<m:properties>
<d:CheckInComment />
**<d:CheckOutType m:type="Edm.Int32">0</d:CheckOutType>**
<d:ContentTag>{388E63ED-C38A-4660-AE30-97AB6A4834D1},3,6</d:ContentTag>
<d:CustomizedPageStatus m:type="Edm.Int32">0</d:CustomizedPageStatus>