4

We have added a list view web part to a page, and we get the Current View link in the web part. Which is configurable when modifying the actual web part.

What we'd like to see (instead of having to click the ellipses button/context menu to get the other views) is the views displayed horizontally like when you are viewing the list itself.

When viewing the list itself, we see All Items View 1 View 2, etc.

Is this possible in the list view web part to display all available views in a horizontal/breadcrumb layout? Please advise.

Trying this in simple webpart code snippet to get back all views:

<script>
var viewCollection = null;
    function runCode() {

        var clientContext = new SP.ClientContext.get_current();
        if (clientContext != undefined && clientContext != null) {
            var web = clientContext.get_web();

            var listCollection = web.get_lists();
            var list = listCollection.getByTitle("MyListName");
            this.viewCollection = list.get_views();

            //var viewInfo = new SP.ViewCreationInformation();
            //viewInfo.set_title('MyView');
            //this.viewCollection.add(viewInfo);

            clientContext.load(this.viewCollection);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
    }

    function onQuerySucceeded() {
        var viewInfo = 'Tasks list current views: \n\n';
        var viewEnumerator = this.viewCollection.getEnumerator();
        while (viewEnumerator.moveNext()) {
            var view = viewEnumerator.get_current();
            viewInfo += view.get_title() + '\n';
        }
        alert(viewInfo);
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>
<a onclick='return runCode();'>Click here</a>
6
  • The easiest way is to use a Content Editor web-part on top of the list view web-part. Then format it as per your wish. Dec 29, 2014 at 20:25
  • 1
    I don't need to format any columns, I just want to show all the views Dec 29, 2014 at 20:28
  • Formatting in the sense of how you want to display the View URL's. Dec 29, 2014 at 20:29
  • I can see it's the span class ms-pivotControl-container, but in when viewing the list, more anchor tags are available than with the list view web part from the home page Dec 29, 2014 at 20:32
  • Have you tried to edit the web part properties? Scroll down to miscellaneous options and in there is a check box for view selector. Dec 30, 2014 at 9:17

2 Answers 2

1

Since you are using SharePoint 2013 I would recommend you to consider the following approach. In SharePoint 2013 was introduced a so called Client Rendering Mode (CSR) which is intended for rendering of List Views and Forms using HTML and JavaScript and which is a default rendering mode.

About View selector menu

ClientPivotControl control (clienttemplates.js) is intended for rendering View selector menu, SurfacedPivotCount property defines the amount of view menu options rendered horizontally (default value is 3)

How to display all view options using CSR

The following template demonstrates how to override SurfacedPivotCount property in order to display all view menu options:

function renderHeaderTemplateWithAllViewsMenu(renderCtx, fRenderHeaderColumnNames){
    var viewData = eval(renderCtx.ListSchema.ViewSelectorPivotMenuOptions);
    ClientPivotControl.prototype.SurfacedPivotCount = viewData.length -2; //display all View options except 'Create View' & 'Modify View'   
    return RenderHeaderTemplate(renderCtx, fRenderHeaderColumnNames); //render default Header template
}

SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
  Templates: {
    Header: renderHeaderTemplateWithAllViewsMenu
  }
});

How to apply the changes

There are at least two options how to apply the changes:

  1. Using JSLink property of XLV web part
  2. Place JavaScript template on page via Script Editor/Content Editor web parts

Here is how to apply the changes using the second option:

  1. Switch the page (NewForm.aspx) into edit mode
  2. Add Script Editor webpart right below the list view web part.
  3. Put the specified code by wrapping it using script tag code into the Script Editor, for example: <script type="text/javascript">{Template JS code goes here}</script>
  4. Save the page

Result

Assume a Workflow Tasks list and all view menu options should be rendered. After applying the corresponding JavaScript template the List View will be rendered as shown on picture below:

enter image description here

1
  • 1
    Hi Vadim. Thank you for the suggestion. I'm working with a default.aspx page, as it's the home page for the site collection. We have numerous listview web parts on this page, and if I try the JS, I get: TypeError: Unable to get property 'length' of undefined or null reference Jan 2, 2015 at 17:44
0

Maybe I am missing something but why not add a list view web part for each view and use a simple JavaScript to hide or show only the views you are interested in seeing. If this sounds like a solution for you leave a comment and I will send you or post here a script I wrote that simulates tabs for the list view web parts on a page.

Your Answer

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

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