For a development i need to get the current view in a library. I added a content editor webpart on allitems.aspx that links to a js file. inside i have to get the current view id. I can have listid but viewid is always null

Here is how i do

var context = SP.ClientContext.get_current();
var web = context.get_web();
var listID = SP.ListOperation.Selection.getSelectedList();
var viewID = SP.ListOperation.ViewOperation.getSelectedView();
console.log("listID ="+viewID );//here is null

I've searched in google without succes

Thanks for your help

  • Ideally your code is correct and it should work.... please refer below link some more details are given here... sharepoint.stackexchange.com/questions/125436/… – Siddharth Vaghasia Oct 25 '16 at 19:14
  • I follow this post but it doesn't work. it returns null – user1898765 Oct 25 '16 at 19:58
  • Wrap your code in a function and try executing your code using ExecuteOrDelayUntilScriptLoaded(getListViewId, "sp.js"); – Aveenav Oct 25 '16 at 20:01

In a Library View (thus ONE ListView WebPart on the page) the view ID is in:



the list ID is:


that _spPageContextInfo object has a whole lot more you might be interested in.. inspect it in F12 dev tools..

Do check Jason Cribbets overview, you might not have all of these goodies yet in your environment:

And Jason Cribbet did a nice overview (april 2016):
http://summit7systems.com/know-your-office-365-and-sharepoint-2016-_sppagecontextinfo/ [sometimes outddated because O365 (SharePoint Online) now does support a lot of the data that is only listed for SPFx]

  • Hi, i use your solution but it's undefined in my case. (i have a content editor webpart on my page documentlibrary that load a js file) – user1898765 Oct 28 '16 at 7:44
  • Then you are probably trying to access it before the page is done loading _spPageContextInfo is core SharePoint functionality – Danny '365CSI' Engelman Oct 28 '16 at 7:47
  • if i look with developer tool i have: _spPageContextInfo.pageListId but no other guid. Here is which properties i have: sympmarc.com/2013/03/26/… – user1898765 Oct 28 '16 at 9:44
  • That is a blog from 2013, the link in my reply is 3 years younger... – Danny '365CSI' Engelman Oct 28 '16 at 10:22
  • I trust you but in my SP2013 Foundation i have only the properties i showed you. Do you know an other solution to get current view? – user1898765 Oct 28 '16 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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