1

I'm trying to use the SharePoint 2013 JavaScript Object Model to retrieve a List via an internal name equivalent.

SPList doesn't have a Name or Internal Name property. It is referred to by the Title; however in my case the Title has been altered.

For example the current Title is 'My Images' however, the list URL is '/Lists/MyImages'

I've been browsing the SPList properties using SharePoint Manager 2013 and I've found a couple of properties that might help:

  • EntityTypeName (MSDN)
  • RootFolder (MSDN)

Both of these properties are giving me the internal name ('MyImages') but I don't know anything about these properties and which property I should use (if either of them)

Can someone give me some info on these properties and what they are based on? The MSDN pages don't give me enough information to work with

  • 1
    why don't you access the list via GUID? – Gwny Aug 4 '14 at 9:53
5

If you don't want to use the GUID, you'll have to make an extra call and get the vti_listname property from the list folder. The folder you can get by url using the getFolderByServerRelativeUrl on the SP.Web object.

Example:

var clientContext = SP.ClientContext.get_current();

var web = clientContext.get_web();
var folder =  web.getFolderByServerRelativeUrl(String.format('{0}/lists/MyImages',_spPageContextInfo.webServerRelativeUrl));

clientContext.load(folder, "Properties");

clientContext.executeQueryAsync(function () {

    var list = web.get_lists().getById(folder.get_properties().get_item("vti_listname"));
    clientContext.load(list);
    clientContext.executeQueryAsync(function () {

        console.log(list.get_title());

    },function(sender, args) {
        console.log("Fail: " + args.get_message());
    });

}, function(sender, args) {
    console.log("Fail: " + args.get_message());
});
  • I'm trying this code but the first clientContext.executeQueryAsync (loading folder properties) is giving me an exception - I enter the outer fail function – jasonscript Feb 16 '15 at 2:11
  • @jasonscript check if you are using the corret url: String.format('{0}/lists/MyImages',_spPageContextInfo.webServerRelativeUrl); – Anders Aune Feb 16 '15 at 6:00
2

Like to note that RootFolder as you point out yourself is not a bad idea if you were to use REST (and not jsom), like this:

/_api/Web/Lists?$filter=RootFolder/Name eq 'MyImages'

Then just pick the first one, and you get one result with all additional list data. (Syntax tested on 365, might need $expand on-premise.)

1

How to get List by its Url:

function getListByName(name,success,error)
{
   var ctx = SP.ClientContext.get_current();
   var lists = ctx.get_web().get_lists();
   var result = ctx.loadQuery(lists, 'Include(RootFolder)');

   ctx.executeQueryAsync(
     function () {
       var list;  
       for(var i = 0; i < result.length; i++) {
           if(result[i].get_rootFolder().get_name() == name) {
               list = result[i];
               break;
           } 
       }
       success(list);
      }, 
      error);
}

Key points:

  • requires only a single request to the server
  • compatible with SharePoint 2010/2013 JSOM

Usage

getListByName('Pages',
    function(list) {
        if(list) 
            console.log('List has been found');
    },
    function(sender, args) {
        console.log(args.get_message());
    });
  • Great! But, I'm still curious about the property EntityTypeName, is it a safe way to rely on while getting a list? – Eliya Amanoeel Feb 23 '16 at 8:56
  • Be aware that you will need extra permissions to get rootfolder of lists, that's why personally i prefer Anders Aune's solution – Verthosa May 4 '16 at 9:25
0

You can use Getlist method of the Web object MSDN Link.

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.