4

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
  • 5,956
  • 8
  • 43
  • 75

4 Answers4

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

Thorsten Hans
  • 2,675
  • 19
  • 21
  • 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? – Ahmed Magdy Jan 14 '12 at 10:24
2

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

Sandeep
  • 436
  • 2
  • 5
1

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

Shegit Brahm
  • 725
  • 2
  • 9
  • 22
  • 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. – Thorsten Hans Jan 11 '12 at 16:23
  • 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. – Shegit Brahm Jan 12 '12 at 09:32
  • 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 Jan 14 '12 at 10:22
  • @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"? – Shegit Brahm Jan 20 '12 at 08:02
0

I think that was you who asked the same question on sharepoint stackexchange. Just to link to my answer there, here is the link. There I give a complete example how you can get sharepoint lists using their "internalName" (url)

Community
  • 1
  • 1
Anatoly Mironov
  • 7,356
  • 1
  • 26
  • 27