0

Using JSOM, I would like to check if the home page is checked out, and if the current user is the one who checked-out the page.

The test occurs from another page.

Here is what I have now:

var context = SP.ClientContext.get_current();
var rootWeb = context.get_site().get_rootWeb();
var me = rootWeb.get_currentUser();
var homePageUrl = (_spPageContextInfo.siteServerRelativeUrl === '/' ? '' : _spPageContextInfo.siteServerRelativeUrl) + '/pages/Accueil.aspx'; // my home page

var homePage = rootWeb.getFileByServerRelativeUrl(homePageUrl);

var checkoutUser = homePage.get_checkedOutByUser();

context.load(me);
context.load(checkoutUser);

context.executeQueryAsync(
    Function.createDelegate(this, function () {
        var message = null;
        // some tests 
        if (checkoutUser && checkoutUser.get_id() !== me.get_id()) {
            message = "HP is not checked-out or checked out by someone else";
        }

        if (message) {
            alert(message);
        }
    }),
    Function.createDelegate(this, function () {
        // TODO: add error handler
    })
    );

If the homepage is checked out this is working as expected. However, if the home page is not checked out, the query fails and I get this error:

"Impossible d’appeler la méthode ou de récupérer la propriété d’un objet null. L’objet renvoyé par la pile des appels suivante est null. « CheckedOutByUser\r\nGetFileByServerRelativeUrl\r\n »"

which can be translated as:

"Impossible to call method or retrieve property of a null object. Object return by the following stacktrace is null: CheckedOutByUser\r\nGetFileByServerRelativeUrl\r\n

What's wrong? How to fix?

PS: it's a SP2010 publishing site.

2

This error occurs due to SP.File.checkedOutByUser object properties are not being initialized when the File is not checked out.

Solution

Instead of retrieving SP.File.checkedOutByUser object at line:

context.load(checkoutUser);

replace it with retrieving SP.File object:

context.load(homePage);

Then you could determine File SP.File.checkedOutByUser and SP.File.get_checkOutType properties as demonstrated below.

The fixed example:

var context = SP.ClientContext.get_current();
var rootWeb = context.get_site().get_rootWeb();
var me = rootWeb.get_currentUser();
var homePageUrl = (_spPageContextInfo.siteServerRelativeUrl === '/' ? '' : _spPageContextInfo.siteServerRelativeUrl) + '/pages/Accueil.aspx'; // my home page


var homePage = rootWeb.getFileByServerRelativeUrl(homePageUrl);
context.load(me);
context.load(homePage);

context.executeQueryAsync(
    function () {

        var checkOutType = homePage.get_checkOutType();
        if(checkOutType === SP.CheckOutType.online){  /* Is file checked-out ? */
             var checkoutUser = homePage.get_checkedOutByUser(); 
             if (checkoutUser && checkoutUser.get_id() !== me.get_id()) {

             }
        }



    },
    function () {
        // TODO: add error handler
    }
);
1

This is not actually the way to fix my code, but I used another method that relies on the REST services. Basically, I can query my data in a single ajax call:

    var siteUrl = _spPageContextInfo.siteServerRelativeUrl === '/' ? '' : _spPageContextInfo.siteServerRelativeUrl;
    debugger;
    var getHPStatus = $.ajax({
        url: siteUrl + "/_vti_bin/listdata.svc/Pages()?$select=ExtraitVersId&$filter=Nom%20eq%20%27Accueil.aspx%27",
        method: "GET",
        headers: {
            "Accept": "application/json; odata=verbose"
        }
    });

    getHPStatus.then(function(data){
        var checkedOutUserId = data.d.results[0].ExtraitVersId;
    });

Basic, but working.

0
0

Why don't you check the checkOutType property and if the value is not none, then find who has checkedout the file.

var value = SP.File.get_checkOutType();
2
  • Does it means that I'll have to chain multiple queries?
    – Steve B
    May 9 '14 at 11:49
  • Yes, I also think so. May 9 '14 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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