Thursday, September 20, 2012

Adding rows to SharePoint 2013's Promoted Links

I have to say, I quite enjoy the ... a hem ... Windows 8 style that SharePoint 2013 brings with the Promoted Links list.  I do however feel it can get a little dull, rather quickly.  Here is a little something I put together to jazz it up a bit. 
 
I'm using the Promoted Links as a Navigation tool and to give my Home Page a little style. 
 
So I've created an out of the box Promoted Links list in SharePoint 2013.  I added some links, 9 to be exact, with some nifty background images.  Here is what i have:

 



Now let's add a web part to a page and see what we get out of the box:
 
 
So with the space I've allocated in my page layout for these links, I can only display about 5 tiles wide.  The web part was nice enough to give me a little scroller at the top so I can see the rest of the links.  It cycles through the next 5 links:
 
Not bad... but not what I'm looking for.  Especially for a navigational tool, it isn't very user friendly.  What I'm looking for is to display all of my links, but to have multiple rows within my web part.  So with a quick view with our Developer Tools, we can see HTML the structure that the web part is rendered with:
  
Looks pretty reasonable; looks like all we have to do is add an extra Table Row to the main table, and move some of the Promoted Link DIV's to that new row.  Whip up a little jQuery that looks something (or exactly) like this:
 
$(document).ready(function () {
 
// Update this value to the number of links you want to show per row
var numberOfLinksPerRow = 5;
 
// local variables
var pre = "<tr><td><div class='ms-promlink-body' id='promlink_row_";
var post = "'></div></td></tr>";
var numberOfLinksInCurrentRow = numberOfLinksPerRow;
var currentRow = 1
// find the number of promoted links we're displaying
var numberOfPromotedLinks = $('.ms-promlink-body > .ms-tileview-tile-root').length;
  // if we have more links then we want in a row, let's continue
  if (numberOfPromotedLinks > numberOfLinksPerRow) {
    // we don't need the header anymore, no cycling through links
    $('.ms-promlink-root > .ms-promlink-header').empty();
    // let's iterate through all the links after the maximum displayed link
    for (i = numberOfLinksPerRow + 1; i <= numberOfPromotedLinks; i++) {
      // if we're reached the maximum number of links to show per row, add a new row
      // this happens the first time, with the values set initially
      if (numberOfLinksInCurrentRow == numberOfLinksPerRow) {
        // i just want the 2nd row to
        currentRow++;

 
        // create a new row of links
        $('.ms-promlink-root > table > tbody:last').append(pre + currentRow + post);
        // reset the number of links for the current row
        numberOfLinksInCurrentRow = 0

    }

    // move the Nth (numberOfLinksPerRow + 1) div to the current table row
    $('.ms-promlink-body > .ms-tileview-tile-root:nth-child(' + (numberOfLinksPerRow + 1) + ')').appendTo($('#promlink_row_' + currentRow));
    // increment the number of links in the current row
    numberOfLinksInCurrentRow++;

  }
}
});


All you have to do is change the value of numberOfLinksPerRow to the number of links you want to display on a row, and your good to go!
 
This is what my page looked like when I was done:
 
 
Enjoy! 

11 comments:

  1. Replies
    1. I actually had a custom MasterPage in my solution. So I just referenced the JQuery library and created a custom .JS file that I threw my script in.

      Delete
  2. Justin,
    I would like to embed two Promoted Link webparts in one page, and I would like each to have 6 items per row, but each list has greater than 6 items. With your code the first list stops at the maximum items per row (6 in my case), and the rest become part of the second list. Do you know how I would update the code to fix this?

    ReplyDelete
    Replies
    1. Was there any resolution to this? I'm facing the same issue.

      Delete
  3. This looks really cool, however I can't get it working correctly. Any chance you could do an exact step by step guide?

    Thanks in advance

    ReplyDelete
    Replies
    1. Hiya Did you manage to get this working? I can't get this to work either

      Delete
    2. I got this to work but it sorts the tiles in descending order :/


      To implement:

      1. Put JS min at: Create a script library & place it there
      2. Put JS Script at: Place the JS script into the script library
      3. Add a “promoted links” web part to the page.
      4. Add a “content editor” web part to the page, click into the CEWP text area, in the ribbon “Format Tab” selection action “Edit Source” and insert in the code below:

      script language="javascript" src="..path of jquery.min.js">
      /script>

      script language="javascript" src="..path to this Script file-name.js">
      /script>



      ** Note - add an open bracket < into the opening script tags above. I removed it because the tag "script" was not allowed.

      Delete
  4. Hi Justin, Thanks for sharing the code!
    Unfortunately, it seems it does not like IE8...
    I have 12 tiles: I get 11 tiles on the first row, and 1 on the second row... Any advice as to how to change the code to make it work with IE8?
    Thanks for your help!

    ReplyDelete
  5. Its not working with multiple promoted link webparts. How to use this code for only single promoted link webpart while there are many promoted link webparts.
    Please help how to overcome on this.

    ReplyDelete