Get SharePoint list by Internal Name using ECMAScript / JavaScript Object Model

16,847

Solution 1

Its always recommended getting lists using ListUrl , which is not changed when List Title changes.

Solution 2

no the CSOM only offers methods to query lists by it's Id or Title.

See http://msdn.microsoft.com/en-us/library/ee549620.aspx

The SharePoint List Schema doesn't offer InternalNames at the moment. See the Schema description http://msdn.microsoft.com/en-us/library/ms415091.aspx

Thorsten

Solution 3

I don't know if you mean that, but inside my JavaScript-File I'm able to use Object Model if I declare these three lines first.

/// <reference name="MicrosoftAjax.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />
/// <reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />

This is relevant code, works without call with {SelectedItem} or stuff:

var items = SP.ListOperation.Selection.getSelectedItems();
var listID = SP.ListOperation.Selection.getSelectedList();

This loads (parts) of the Client Object Model, so I guess there you can go on. Because this is supported with IntelliSense.

Edit2: The other way to get and use a list with JavScript only is

var web;  
var context;  
var listTitle = "ListName";  

function InitiateThisScript(itemId) {   
    context = new SP.ClientContext.get_current();  
    web = context.get_web();  
    list = web.get_lists().getByTitle(listTitle);  
    item = list.getItemById(itemId);  
    context.load(web;  
    context.load(list);  
    context.load(item);  
    context.executeQueryAsync(handleItem(item, list));  
}  

This way requires in your Elements.xml, where I defined my buttons, that you call it

CommandAction="javascript:InitiateThisScript('{SelectedItemId}','');" />

Edit3: Be careful using this JavaScript without any security checks. Because for example you have delivered this solution to a site, which has lets say two lists. First one you suggested to have this JavaScript and a second one. If you have custom buttons that appears in both list than you work on second list, but using the buttons fire to the first list as long as it is possible.
Lets say you have a button that clears content and you have in both lists a column called "title". If you are on second list and press button "delete title" than on your first list the title from item with same itemId will be deleted. On your second list happens nothing.
This appears from visibility of your buttons and no check, if designated list is the one you are working on.

Shegit

Edit: Scrolling my tabs I found this one: Retrieve items from a folder with EcmaScript & COM

Share:
16,847
Ahmed Magdy
Author by

Ahmed Magdy

My name is Ahmed Magdy, I'm working as Senior Customer Engineer &lt;developer /&gt; at Microsoft with 16+ years experience in software development. LinkedIn - Stackoverflow Careers - @amgdy Founder of HackItRight - @hackitright

Updated on June 23, 2022

Comments

  • Ahmed Magdy
    Ahmed Magdy almost 2 years

    How can I load items from a SharePoint list using its InternalName? As far as I know I can get it using either Id or Title like the following:

    var clientContext = new SP.ClientContext('/News/');
    var web = clientContext.get_web();
    var list = web.get_lists().getById("{1DBA9283-0AFA-4FA1-9BBA-70D8D190971F}");
    ...
    
  • Ahmed Magdy
    Ahmed Magdy over 12 years
    Can u give me an example using JavaScript Object Model?
  • Thorsten Hans
    Thorsten Hans over 12 years
    Sure the three lines on top gave you IntelliSense for the JS CSOM but the code sample you've listed here will only work if you're within the context of a list. For example it will work if one or multiple ListItems are selected within the ListWebPart. This Snippet will not work on a plain LayoutsPage. Because AFAIK Selection will be undefined.
  • Shegit Brahm
    Shegit Brahm over 12 years
    That is correct so far. As far as I understand AMgdy, he is asking about getting items from a list, not on a plain Page. And beside, I used this code on a plain list, don't know if SharePoint handles this as a ListWebPart. But than every list is a ListWebPart and I don't see my misunderstanding from question.
  • Ahmed Magdy
    Ahmed Magdy over 12 years
    Yes @shegit-brahm get me right, I have a js file in the master page and pulling some article, images dynamically using JavaScript Object Model
  • Ahmed Magdy
    Ahmed Magdy over 12 years
    Thank you @thorsten-hans for your reply but I know already that I doesn't exists, what I meant is there anyone figure out a modified code snippet to get it by InternalName?
  • Shegit Brahm
    Shegit Brahm over 12 years
    @AMgdY I added some Javascript. But I was not able to get the code looking well. I have many spaces now, but still all cut. What did you mean with "InternalName"?