Is it possible to get list by url using ECMA script client model? For example something like this:
context.get_web().get_lists().getByUrl("Lists/MyList");
UPD!:
Please, refer to this correct answer on this question (for SP 2013 version only)
Is it possible to get list by url using ECMA script client model? For example something like this:
context.get_web().get_lists().getByUrl("Lists/MyList");
UPD!:
Please, refer to this correct answer on this question (for SP 2013 version only)
There is no "get by URL" method for lists in the client object model that I am aware of.
About the closest thing you have is a GetFolderByServerRelativeUrl on the Web object:
ClientContext.Web.GetFolderByServerRelativeUrl(url)
Depending on what you want to do this might work for you...unfortunately you can't get the parent list from the folder object like you can in the .NET api.
If it is possible, you can use server insert like this:
context.get_web().get_lists().getById('<%= MyListId %>');
If you can't use server insert and JS is placed, for example, on DispForm.aspx - SharePoint ListProperty control can help:
context.get_web().get_lists().getByTitle('<SharePoint:ListProperty Property="Title" runat="server"/>');
(Don't think it's the best solution) Get all list's in the web and iterate them to find one (or more) you need.
var lists = context.get_web().get_lists();
context.load(lists, 'Include(DefaultDisplayFormUrl)');
context.executeQueryAsync(function (sender, args) {
var enumerator = lists.getEnumerator();
while (enumerator.moveNext()) {
var list = enumerator.get_current();
if (list.get_defaultDisplayFormUrl().indexOf('Lists/MyList') != -1) {
// Get it
}
}
}, function(sender, args){ /*error*/});
For anyone in future, I wrote code that will do this (Note: this is managed client object model, re-write in JavaScript as appropriate).
listUrl
is a WEB relative URL segment of the list, for example "Shared Documents".
Note I'm just setting this.listName
here, but at that point you have the full List object, so grab whatever you need at that point.
Folder folder = this.sharePointContext.Web.GetFolderByServerRelativeUrl(listUrl);
ListCollection lists = this.sharePointContext.Web.Lists;
this.sharePointContext.Load(folder);
this.sharePointContext.Load(lists, list => list.Include(l => l.RootFolder, l => l.Title));
this.sharePointContext.ExecuteQuery();
foreach (List list in lists)
{
if (list.RootFolder.ServerRelativeUrl == folder.ServerRelativeUrl)
{
this.listName = list.Title;
}
}
var context = SP.ClientContext.get_current();
var lists = context.get_web().get_lists();
context.load(lists, 'Include(RootFolder)');
context.executeQueryAsync(function (sender, args) {
var enumerator = lists.getEnumerator();
while (enumerator.moveNext()) {
var oneList = enumerator.get_current();
console.log(oneList.get_rootFolder().get_name());
}
});