1

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();
console.log("listID="+listID);
var viewID = SP.ListOperation.ViewOperation.getSelectedView();
console.log("listID ="+viewID );//here is null

I've searched in google without succes

Thanks for your help

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

1 Answer 1

2

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

_spPageContextInfo.viewId

and..

the list ID is:

_spPageContextInfo.listId

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

6
  • 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) Oct 28, 2016 at 7:44
  • Then you are probably trying to access it before the page is done loading _spPageContextInfo is core SharePoint functionality Oct 28, 2016 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/… Oct 28, 2016 at 9:44
  • That is a blog from 2013, the link in my reply is 3 years younger... Oct 28, 2016 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? Oct 28, 2016 at 13:16

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.