5

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)

5

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.

1

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"/>');

Solution using only JS (without server insert and controls)

(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*/});
0

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;
            }
        }
  • this is not a javascript solution – Mx. Aug 4 '14 at 13:01
  • Indeed (I found this in a fleeting search), but it wouldn't take much to re-write it to Javascript, only adding an async callback. All the objects are the same. – James Love Aug 4 '14 at 16:25
0
                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());
                    }
                });

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.