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 ?

3

3 Answers 3

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());
}
9
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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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