4

I'm trying to retrieve the corresponding ListItem from a page layout in the master page gallery using JSOM. I'm using the same approach that Vadim provides in this question, JSOM - get listitem by file url, as below:

function getAssociatedPageLayoutListItem() {
    var promiseObject = new RSVP.Promise(function (resolve, reject) {

        var pageLayoutListItem = web.getFileByServerRelativeUrl("/_catalogs/masterpage/MyCustomPageLayout.aspx");

        clientContext.load(pageLayoutListItem, 'ListItemAllFields');
        clientContext.executeQueryAsync(
            function () {
                resolve(pageLayoutListItem);
            },
            function (error) {
                reject(error);
            }
        );
    });

    return promiseObject;
}

The problem is that SharePoint returns an ArgumentException with the message Value does not fall within the expected range..

ErrorInfo
ErrorCode=-2147024809
ErrorMessage=Value does not fall within the expected range.
ErrorTypeName=System.ArgumentException
ErrorValue=(null)

I have run into this exception before, and back then it seemed as it had something to do with what fields I requested, but this time I can't seem to fix it. How can I get a clearer picture of what is missing, and how to fix it?

Update Loading only the requested file throws the same error.

It works perfectly when I use the REST-api and access the page layout and the ListItem for any given relative url and page.

Update 2 The desired method works on a specific site collection, but not on the root or another one. My biggest lead so far is to compare the site collections and see in what ways they differ. It might be due to access rights.

clientContext.load(pageLayoutListItem)
  • This code runs perfectly for me: var ctx = SP.ClientContext.get_current(); var url = "/_catalogs/masterpage/RootCommonPage.aspx"; var file = ctx.get_web().getFileByServerRelativeUrl(url); //get file ctx.load(file,'ListItemAllFields'); ctx.executeQueryAsync( function () { var listItem = file.get_listItemAllFields(); var comments = listItem.get_fieldValues().Title; }, function() { alert('error'); } ); – Nadeem Yousuf Dec 12 '14 at 11:19
  • It doesn't work for me unfortunately. I still get the same System.ArgumentException: Value does not fall within the expected range. – Daniel B Dec 12 '14 at 12:24
  • Problem solved! I'll provide an extensive answer on what was wrong! – Daniel B Dec 12 '14 at 13:59
1

The main issue with this problem is related to how you retrieve the SP.ClientContext and SP.Web objects that are used in CSOM to retrieve the information you want. Below I present the solution to my problem and the conclusions I've come to. If they are wrong, please correct me so I can gain a better understanding if this!

Consider these two following methods:

Method 1:

var siteUrl = "http://sp/MySite";
var clientContext = new SP.ClientContext(siteUrl); //simplified (in reality a method that returns this)
var web = clientContext.get_web();

var pageLayoutListItem = web.getFileByServerRelativeUrl("/_catalogs/masterpage/MyCustomPageLayout.aspx");
clientContext.load(pageLayoutListItem, 'ListItemAllFields');
clientContext.executeQueryAsync(function () {
    resolve(pageLayoutListItem.get_listItemAllFields());
}, function (error) {
    reject(error);
});

In this method, we use the constructor for the SP.ClientContext that takes a parameter as this:

The server-relative URL that starts with / or a full URL that starts with http:// or https://.

var object = new SP.ClientContext(serverRelativeUrlOrFullUrl);

This is the case in the question, where the SP.Web object is passed around between different functions that all work, because they never leave the site-scope of the SP.Web object. The problem occurs when you try to access something that is outside the scope of the site.

This is what the SP.Web object looks like

$0_0: SP.ClientContext
$1C_0: true
$1F_0: Object
$1r_0: Object
$8_0: SP.ClientRequest
$F_1: SP.Web
$w_0: "http://sp/MySite"

Trying to retrieve items from the master with this object will not work, since it's scoped to `/MySite.

web.getFileByServerRelativeUrl("/_catalogs/masterpage/MyCustomPageLayout.aspx");

will thus throw an exception.

Method 2:

The below will work, since we aren't locked to a specific site.

var clientContext = SP.ClientContext.get_current();
var pageLayoutListItem = clientContext.get_web().getFileByServerRelativeUrl("/_catalogs/masterpage/MyCustomPageLayout.aspx");
clientContext.load(pageLayoutListItem, 'ListItemAllFields');
clientContext.executeQueryAsync(function () {
    resolve(pageLayoutListItem.get_listItemAllFields());
}, function (error) {
    reject(error);
});
  • 1
    I'd upvote you multiple times if I could, even 4 years later – Treycos Feb 8 '18 at 14:17

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.