Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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");
share|improve this question

3 Answers 3

For anyone in future, I wrote code that will do this.

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;
            }
        }
share|improve this answer

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*/});
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.