I need a way to determine if a publishing page is checked out by the current user; I need this because I'd like to put a redirect, if the page is not checked out.
Any ideas?
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communitySP.File.checkOutType property gets a value that indicates how the file is checked out of a document library
$.ajax({url: "/_api/web/getFileByServerRelativeUrl('" + pageUrl + "')/checkOutType",
headers: { "Accept": "application/json; odata=verbose" },
success: function(data) {
if(data.d.CheckOutType == 0) {
console.log('The file is checked out');
}
}
});
var context = SP.ClientContext.get_current();
var web = context.get_web();
var file = web.getFileByServerRelativeUrl(pageUrl);
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());
}
);
You can use the Client Object Model to get a reference to the file and then use the checkOutType property to see if it has been checked out.
They're both working, thanks!
I've found another way, a little faster because it doesn't need to call the server back again, but it can only to determine if the file is checked out by the current user, not by a generic user.
I've found a way to achieve this, tested on Sharepoint 2013.
Just add the following script inside a script webpart after putting it inside the publishing page.
There's a variable called "PageState", that contains a useful field.
<script>
(function() {
var isCheckedOut = false;
if (typeof(PageState) != "undefined" && PageState)
{
isCheckedOut = PageState.ItemIsCheckedOutToCurrentUser == "1";
}
if (!isCheckedOut)
{
//do redirect
}
})();
</script>