3

I'm trying to get a list using CSOM in a SharePoint 2013 that was migrated from 2010.

This is the code that I'm using, and it is working in all my environments except this one.

function custom_getListOnUrlName() {
try {
    var clientContext = new SP.ClientContext(rootModel.listSiteUrl);
    var oList = clientContext.get_web().getList(rootModel.listUrl);
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name="Sequence" Ascending="True"/></OrderBy></Query></View>');
    rootModel.listItems = oList.getItems(camlQuery);
    clientContext.load(rootModel.listItems);
    clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded),
                Function.createDelegate(this, onQueryFailed));
} catch (err) {

}}

When looking to exceprion I'm getting this error

Object doesn't support property or method 'getList'

Does anyone know what could be causing it?

2

I was able to find the cause of the issue, the particular site collection is part of a farm that was not updated and does not include the February 2015 cu update. So I had to build a method to support both scenarios, if the getList() method is not available it will do the fall back method and will use the older method get_lists(). I ended up doing this because easier for an end user to get the list url instead of the list name.

Here is my code it might be useful if you need to deal with non updated SharePoint farms.

function custom_getListOnUrlName(listSiteUrl, listUrl, listName) {
    try {
        var clientContext = new SP.ClientContext(listSiteUrl);
        var oList = clientContext.get_web().getList(listUrl);
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name="Title" Ascending="True"/></OrderBy></Query></View>');
        var listItems = oList.getItems(camlQuery);
        clientContext.load(listItems);
        clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded),
                    Function.createDelegate(this, onQueryFailed));
    } catch (err) {
        try {
            onQueryFailed(listName);
        }
        catch (err) {
            console.log('Unable to get data from list');
        }
    }

}

function onQuerySucceeded(sender, args) {
    parseItems();
}

function onQueryFailed(listSiteUrl, listName) {
    try {

        var clientContext = new SP.ClientContext(listSiteUrl);
        var web = clientContext.get_web(); 
        var list = web.get_lists(); 
        var targetList = list.getByTitle(listName); 

        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name="Title" Ascending="True"/></OrderBy></Query></View>');

        var listItems = targetList.getItems(camlQuery);

        clientContext.load(listItems);
        clientContext.executeQueryAsync(onQuerySucceeded, onQueryFallBackFailed);
    }
    catch (err)
    {
        onQueryFallBackFailed();
    }
}

function onQueryFallBackFailed(sender, args) {
    console.log('Unable to get data from list');
}
  • Microsoft should update their documentation. Nice to know about the new method. I deleted my answer since it's no longer valid. – wjervis Feb 18 '16 at 16:59

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.