6

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?

3 Answers 3

7

SP.File.checkOutType property gets a value that indicates how the file is checked out of a document library

Get file checkout type via REST

$.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');
                  }
               }
});

Get file checkout type via CSOM (JavaScript)

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());
      }
);
5

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.

SP.File.checkOutType property

5

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>
2
  • It didn't work for me when I embedded it on the aspx page.
    – Tito
    Oct 28, 2016 at 14:44
  • 1
    Works great on a publishing page, and no need for a request to the server!
    – Evariste
    Oct 5, 2018 at 12:44

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.