'List 'MyList' does not exist at site with URL

I am trying to make a query on a SPList but I got an error saying that List doesn't exist. How can I read the current context in the proper way ? Is somehow a deployment issue ?

var clientContext = new SP.ClientContext.get_current();;
var oList = clientContext.get_web().get_lists().getByTitle("MyList");

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="SearchUserName" /><Value Type="Text">' + loginName + '</Value></Eq></Where></Query></View>');
this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);

clientContext.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
  //do something
}), Function.createDelegate(this, function (sender, args) {
  //log error
}));

I get the error: List 'MyList' does not exist at site with URL '.... site path'

What am I doing wrong? Is there another way of reading the current context?



Solution 1:[1]

Remember SharePoint considers each subsite a seperate site. If your code runs on the root of your SharePoint site, but the lists resides in a subsite, your code will not work. You would then need to specify the site when setting your client context.

Solution 2:[2]

Are you sure that get_current is actually returning the correct context?

It might be safer to specify the context.

Also, you could use the lists ID instead of the name then get by ID instead.

Solution 3:[3]

After I ran into the same problem, I realized my mistake was the following : I was running Sharepoint's workbench with the root URL of my domain

So I was running workbench with URL

https://mydomainname.sharepoint.com/_layouts/15/workbench.aspx

While I should have used

https://mydomainname.sharepoint.com/sites/myfirstsite/_layouts/15/workbench.aspx

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robbert
Solution 2 Julian Knight
Solution 3 Erik Lallemand