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?
You could have run into a simple problem which tripped me up:
When you use getByTitle()
you need to use the List's display name,
Is your list display name "MyList" or might it be "My List"?
To find the display name look at what the list is called in the "Site Contents" page.
To find the internal name, look at the URL when you hover over (or click) the List name in "Site Contents".
Try get_web().get_lists().getByTitle("My List");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With