Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have included some javascript in SharePoint master page, I need this javascript to have access to the opened list ID.

For example when I load

 www.mypage.com/codev/_layouts/15/start.aspx#/Lists/All Fields/AllItems.aspx

I need my JavaScript function to have access to the list: All Fields ID.

Notice there is no query string. I suppose I could extract the list name from the URL but as I'm trying to make it work on any site/list combination I was hoping there would be an easier way to extract the current opened list ID's GUID.

share|improve this question

2 Answers 2

At least two options are available to determine List Id on View page:

Option 1: SP.ListOperation.Selection Methods

SP.ListOperation.Selection.getSelectedList() Method gets the ID of the list being selected by the client user. The following example will return List Id in a View page.

Example:

var listId = SP.ListOperation.Selection.getSelectedList();

Option 2: spPageContextInfo client context

With every page is associated client context (_spPageContextInfo object), that object contains List Id when the View page is rendered. The following example demonstrates how to get List Id in View page.

Example:

//get current List Id in View page
var listId = _spPageContextInfo.pageListId;
share|improve this answer

Check out the ctx variable. There's lots of great info in there. In your case, what you need is:

ctx.listName 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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