4

I'm trying to trigger the NewForm.aspx of a list by clicking on a link. I would like to known how to get a list ID using JavaScript (Or jQuery)? (SP2010) I've already tried :

var context = new SP.ClientContext.get_current();
var web = context.get_web();

web contains an empty object so I cannot use it. why ?

Atish Kumar Dipongkor
  • 13,371
  • 5
  • 32
  • 64
ameliapond
  • 736
  • 2
  • 14
  • 34

3 Answers3

6

Should be some thing like following

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('Name of the List');
context.load(list, 'Id');

In the success, try

var listId = list.get_id();

Update for web is undefined

Run code under ExecuteOrDelayUntilScriptLoaded()

ExecuteOrDelayUntilScriptLoaded(getListId, "sp.js");
var list;

function getListId() {
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    list = web.get_lists().getByTitle('Name of the List');
    context.load(list, 'Id');

    context.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, error));

}


function success() {
    listId = list.get_id();
    console.log(listId);
}

function error(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}
Atish Kumar Dipongkor
  • 13,371
  • 5
  • 32
  • 64
1

I was looking for this myself.

In some cases, you should be able to use the SP object for this.

Thought it may come in handy in case someone else bumps into this post.

SP.ListOperation.Selection.getSelectedList()

enter image description here

https://stackoverflow.com/questions/6422939/get-currently-selected-list-in-javascript-on-load-sharepoint-2010

Tiago Duarte
  • 5,477
  • 2
  • 21
  • 43
0

As per @VitaliClimenco's comment, the list id is stored in the global javascript object _spPageContextInfo : _spPageContextInfo.pageListId.

Alex
  • 137
  • 9