Umer Pasha Blogs

www.umerpasha.com

jQuery to change size of the Promoted Links in Sharepoint 2013

on February 15, 2013

Sharepoint 2013 comes with standard Metro UI links app called “Promoted Links”. I will not go into how to set them up as you can google it and find various resources for that.

I was trying to put in two strips of promoted links and also wanted the second strip to be smaller than the first giving it a feel of a sub-menu. Coincidentally, there is no out of the box way of doing that. The images seemed to be scaled to 150px fixed.

So I added in a little bit of jQuery to resize them.

Here is the code that goes into a content editor webpart on the same page. Please change the path to your jQuery library reference. Change the image size you want. Change the WPQ number of the promoted link. You can find it by using developer tools on your browser hitting F12. Look for “promotedlinksbody_WPQ” :

<!--JQuery script to make promoted links any size. Also shifts to 2nd row if more than specified tiles. Author: Umer Pasha , Date: 3/15/2013-->

<script type="text/javascript" src="/scripts/jquery/jquery-1.8.1.min.js"></script>


<script language="javascript">
$(document).ready(function () {
 
// Update this value to the number of links you want to show per row
var numberOfLinksPerRow = 10;

// Set Image Size 
var ImageSize = "76";

// in case we have multiple promoted links on page, name the WPQ here
var promotedlinksbody_WPQ = "4";
 
// 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 = $('#promotedlinksbody_WPQ' + promotedlinksbody_WPQ + ' > .ms-tileview-tile-root').length;
//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++;

  }
}


	// Set the size of main promoted links body
	var ControlID='promotedlinksbody_WPQ';
	ControlID=ControlID + promotedlinksbody_WPQ;
	$("#" + ControlID).height(ImageSize);
	
	// iterate through each link and set the size
    for (i = 1; i <= numberOfPromotedLinks; i++) {
        
		ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
		ControlID= ControlID + '_1';
    	$("#" + ControlID).width(ImageSize);
    	$("#" + ControlID).height(ImageSize);
		
		ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
		ControlID= ControlID + '_2';
    	$("#" + ControlID).width(ImageSize);
    	$("#" + ControlID).height(ImageSize);

		ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
		ControlID= ControlID + '_7';
    	$("#" + ControlID).width(ImageSize);
		
		ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
		ControlID= ControlID + '_4';
    	$("#" + ControlID).width(ImageSize);
    	$("#" + ControlID).height(ImageSize);
    	
    	
		ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
		ControlID= ControlID + '_4';
    	$("#" + ControlID).width(ImageSize);
    	$("#" + ControlID).height(ImageSize);
    	//$("#" + ControlID).top(ImageSize);

	}

	ControlID="promotedlinksheader_WPQ" + promotedlinksbody_WPQ;
	$("#" + ControlID).hide();


});

</script>

Image

 

I am putting the javascript above in a .js file and putting the link to the .js file in a content editor. The content editor is on the same page as the promoted links.

smallPL1

These are the paths to the online vector icons I am using:

http://modernuiicons.com/icons/svg/appbar.reply.calendar.svg
http://modernuiicons.com/icons/svg/appbar.money.svg
http://modernuiicons.com/icons/svg/appbar.man.suitcase.svg
http://modernuiicons.com/icons/svg/appbar.calendar.14.svg
http://modernuiicons.com/icons/svg/appbar.calendar.svg
http://modernuiicons.com/icons/svg/appbar.places.svg

Here is the link to the .js file. Please rename to .js:   Link

 


74 responses to “jQuery to change size of the Promoted Links in Sharepoint 2013

  1. Dave says:

    Great post, looking forward to deploying it but I haven’t been able to get it to work.Are you sure the posted script works?

  2. Umer Pasha says:

    The screenshot is actual so it does work for me. What kind of issue is happening for you? Does it do nothing?

    A few culprits I can think of:
    1. Did you get the WPQ number right?
    2. Double check the jQuery API path….. try using a web path to make sure… something like this:
    http://code.jquery.com/jquery-1.8.1.min.js

  3. Umer Pasha says:

    I will double check the code and get back!

  4. Jim C says:

    My links aren’t changing size. I used the online jquery api path. Changed the WPQ # to 2.

    Which I believe should map to this (taken from page):
    promotedlinksbody_WPQ2

    So not sure what’s not lining up.

  5. Umer Pasha says:

    Guys, have you been using jquery-1.8.1.min.js? Just to make sure.

  6. Umer Pasha says:

    Please try the updated code above. I added the functionality later on to also restrict to 10 icons. Again, check the jQuery path and version and also set the WPQ number at the start of code.

  7. Umer Pasha says:

    Also, This goes into a script tag. i am using jquery-1.8.1.min.js

  8. geo says:

    please correct the syntax of this code

    for (i = numberOfLinksPerRow + 1; i table > tbody:last’).append(pre + currentRow + post);

  9. Umer Pasha says:

    Looks like wordpress was screwing up the code big time 😦 Is there a better way to post code here? The code tag does nothing 😦

    Anyway, thanks geo for pointing it out. i have updated the code in main post. Please check now and let me know the results.

    Thank you all for your feedback.

    • Mima says:

      Hi Umer, First, thanks for sharing this code!
      However, I tried it by adding your code in a Content Editor. I changed this line to use the web link:

      Then, I got promotedlinksbody_WPQ4 so I changed this line to
      var promotedlinksbody_WPQ = “4”;
      I have 5 tiles so I set this line to:
      var numberOfLinksPerRow = 5;
      Also, I changed the single and double quotes on all the code, since this page added the start and end quotes, which are different than the regular single and double quotes…
      However, the code still does not work…
      Please help! Could you please email this code to ampimartinez@yahoo.com?
      Thanks a lot!!! Highly appreciated!!!

      • Umer Pasha says:

        Thanks Mima. I seriously do not know why this is not working for so many. On other hand, I have had people telling me it worked fine for them.

        You followed the right steps. Thanks for letting me know about the problem with copying quotes 😦 I have added the link to the actual file I am using.

  10. Mima says:

    Hi Umer, Thanks for posting the actual js. It worked by putting all the code into a script editor webpart and using the web link for the jquery. However, I have only 5 tiles and for the second tile the image is distorted!! Do you have any idea why? Thanks again!

    • Umer Pasha says:

      Thanks for your help in resolving this.

      What kind of distortion? I mean is it because of the description text being too long? Is the image pixel distorted?

      Can I take a look at the image?

      • Mima says:

        the description text is OK…is the image pixel distorted… The image seems to be stretched down…sorry I do not know how to post a screenshot, since I am working on an intranet site.. 😦 I replaced it with another image and it always happens on the second tile – I had 3 tiles on one web part, then I tried another web part with 5 tiles and the second tile is where the image gets distorted badly… Thanks for looking into this…

  11. Mima says:

    When I look at the HTML with F12 I see my first id=”Tile_WPQ5_2_1″ —> starts with 2 not 1. Then, the second tile is “Tile_WPQ5_1_1” … I think this is throwing off the script at the end where you set the size of the promoted links… Not sure how to setup the iteration, because I would need to start on 2 then 1 and then 3, 4 and so on…Any ideas?

    • Umer Pasha says:

      Could be because of the order of the links in the list itself. I have them pretty much linear 1 to 7.

      • Mima says:

        I can see the order of the items with the Developers tools in HTML. However, I have an aspx page, so when I open it with SharePoint Designer I do not see the HTML code behind the scenes…. Do you know how can I access this HTML Code? Thanks for your help!

      • Umer Pasha says:

        No no. I meant the promoted links.

        Go to the page in the browser with promoted links webpart on it.

        Click the gear icon at top right, click “Edit Page”.

        Select the checkbox at top right of the promoted links webpart.

        Go to the top of the page in browser and on the ribbon, select tab that says “List”

        Change current view dropdown to “All Promoted Links”.

        This will show you the data in a datasheet kind of view.

  12. Mima says:

    Umer, Yes. I checked the datasheet and all the links are in sequential order. However, When I looked at the page with the developers tools, I see the tiles with a different sequential order – which I think is messing up the jQuery… What I do not know is how to edit this HTML Code, since I do not see it from the SharePoint Designer… Thanks again for helping me!

  13. Mima says:

    this is the code I see:

  14. Mima says:

    div id=Tile_WPQ2_1_1
    div_id =Tile_WPQ2_6_1
    div_id =Tile_WPQ2_3_1
    div_id =Tile_WPQ2_4_1
    div_id =Tile_WPQ2_5_1
    div_id =Tile_WPQ2_7_1

  15. Colin says:

    Brilliant, worked like a charm. After, that is, I pasted the code into notepad and replaced all the Word Press formatting with plain ASCII characters.

    In case anyone else wants to retain a space between tiles and make the text fit you simply add the following (or something like it) to the first control reference in the for loop.

    $(“#” + ControlID).css(‘margin’, ‘3px’);
    $(“#” + ControlID).css(‘font-size’, ‘x-small’);

  16. Rakhi Sandhu says:

    I haven’t been able to get this working. Does anyone have a step by step process?

    I have saved jquery-1.8.1.min.js into my siteassets – is this correct? or do i have to put somewhere else? I’ve then changed path as below:

    I have 9 tiles so far i would like to arrange tiles 5 per line (more tiles will be added in the future)

    I’ve tried added the script to a Content Editor web and Script Editor but cannot get it working

    any help appriciated

    • Umer Pasha says:

      Hi. I will email you the steps in the morning.

      • Mima says:

        hi, I have not been able to get it working either 😦 could you please email the steps to me too? Thanks for your help!

      • Umer Pasha says:

        Here you go:

        First up, download the file directly from here.

        http://www.umerpasha.com/junk/smallpromotedlinks.js.txt

        Rename the file to smallpromotedlinks.js

        Now, you see the first line after comments that has a path to jquery? You need to change that to where-ever you have the jquery file. I have it in scripts folder and then in jquery folder. You can change it to whichever folder you keep it.

        src=”/scripts/jquery/jquery-1.8.1.min.js

        Next, put the code from smallpromotedlinks.js into a content editor webpart or link to the smallpromotedlinks.js file.

        You will have to change the WPQ number before you use the smallpromotedlinks.js file though. Where it says:

        var promotedlinksbody_WPQ = “4″;

        For that use the dev tools in your browser using F12 and see what is the number of your Promoted Links Webpart.

        As per your requirement, change the number of links per row where it says:
        var numberOfLinksPerRow = 10;

        Let me know if you need further assistance.

        Thank you.

  17. I think the admin of this web page is genuinely working hard in support of his web page, since here every
    information is quality based information.

  18. Sebastian N. says:

    Good evening from Germany, i have just tried your script. I have only one problem, I have 6 Links and give the commando that i will have 4 in one row. But i get only 1 link in raw 2 and 5 in raw1 and not more. I get always only one link in raw 2, what make i wrong.

    Please help me, so i can use your nice script.

    Best regards

  19. Shivam says:

    What does a novice like me do. I have absolutely no knowledge of codes and stuff as I am a Civil Engineer. Been working with Sharepoint for the past one month and hopefully understand a bit. I need to basically change the size of the “All Promoted Links” Tile to fit the entire top web part. So basically I need the tile to be 6 inches by 1 inch. Please can you tell me a step by step method to do so.

    Thanks. Appreciate any help provided.

  20. Nipesh Shah says:

    Hi there is one thing may help, If we try to refer the jquery file (jquery-1.8.1.min.js) outside from office 365 then it will give error. Just upload this file on the same server where you upload the other script and then give its reference,

  21. always3099 says:

    Hi Umer,

    Thank you very much for sharing your code. Your code wroks in my SP2013 site. However, the image size is the only thing I couldn’t figure it out yet.

    I need to display image size in Width 370 x Height 350, so I changed your code to

    // Set Image Size
    var ImageSizeW = “370”;
    var ImageSizeH = “350”;

    And then changed this part:
    // Set the size of main promoted links body
    var ControlID=’promotedlinksbody_WPQ’;
    ControlID=ControlID + promotedlinksbody_WPQ;
    $(“#” + ControlID).height(ImageSizeH);

    // iterate through each link and set the size
    for (i = 1; i <= numberOfPromotedLinks; i++) {

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_1';
    $("#" + ControlID).width(ImageSizeW);
    $("#" + ControlID).height(ImageSizeH);

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_2';
    $("#" + ControlID).width(ImageSizeW);
    $("#" + ControlID).height(ImageSizeH);

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_7';
    $("#" + ControlID).width(ImageSizeW);

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_4';
    $("#" + ControlID).width(ImageSizeW);
    $("#" + ControlID).height(ImageSizeH);

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_4';
    $("#" + ControlID).width(ImageSizeW);
    $("#" + ControlID).height(ImageSizeH);
    //$("#" + ControlID).top(ImageSize);

    }

    ControlID="promotedlinksheader_WPQ" + promotedlinksbody_WPQ;
    $("#" + ControlID).hide();

    });

    The image width display perfect (370px) in the result, but the image height displays 150px and the other 200px height is blank. Could you tell me which part I should modify in the code to display the size I need. Thank you again. 🙂

    • Jenny says:

      Thanks for adding this. I was able to get it working with a different height.
      Have you run into the issue where if you delete an item from your promoted links list, then any new items that you add, the resizing isn’t working? Really hoping someone has a workaround for this issue. Thanks!

  22. always3099 says:

    Good news!! I can change the image height now. 🙂

    I added
    $(“#” + ControlID).height(ImageSizeH);
    into the last part of for statement code:

    // iterate through each link and set the size
    for (i = 1; i <= numberOfPromotedLinks; i++) {

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_1';
    $("#" + ControlID).width(ImageSizeW);
    $("#" + ControlID).height(ImageSizeH);

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_2';
    $("#" + ControlID).width(ImageSizeW);
    $("#" + ControlID).height(ImageSizeH);

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_7';
    $("#" + ControlID).width(ImageSizeW);
    $("#" + ControlID).height(ImageSizeH); // new added code

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_4';
    $("#" + ControlID).width(ImageSizeW);
    $("#" + ControlID).height(ImageSizeH);

    ControlID='Tile_WPQ' + promotedlinksbody_WPQ + '_' + i ;
    ControlID= ControlID + '_4';
    $("#" + ControlID).width(ImageSizeW);
    $("#" + ControlID).height(ImageSizeH);
    //$("#" + ControlID).top(ImageSize);

    }

    ControlID="promotedlinksheader_WPQ" + promotedlinksbody_WPQ;
    $("#" + ControlID).hide();

    });

  23. always3099 says:

    Does any one know how to adjust the shadow part? The shadow part is too high now since I added the height of the images.

    Thank you. 🙂

  24. I got this working on Hosted sharepoint 2013 site. I was wondering how I can reduce the height of the shadow when I hover over this app. I mean when I hover my mouse over this app on the page, the title and description rolls over and takes more than half the height of this app. Any ways to reduce the height of the description rollover?

  25. always3099 says:

    It took me 2 days struggling for the showdow of the Promoted Links. I end up using Mosaic jQuery to create the same effect like Promoted Links. It only took me 1 hour. 🙂

    Here is the Mosaic jQuery link: http://buildinternet.com/project/mosaic/1.0/

  26. shaversnyc says:

    Great script! Thank you. I’m experiencing something odd that I haven’t seen mentioned yet. Script works great when I initially add to the page. It also works fine when I add new items to my Promoted Links list. If I delete items from the list, however, the script starts to fail. Example: If I delete an item from the list, any new items I add doesn’t have the new size applied. Any idea why this is happening and how I can avoid? I will be making lots of updates (adding & deleting) to my promoted links list. Thank you!

    • Umer Pasha says:

      I hope its not recreating the app and giving it a new wpq number. do an f12 in browser and check the wpq number.

      • shaversnyc says:

        Thanks for the quick reply. The script still appears to be numbered correctly. The webpart itself is also still numbered the same. It’s very odd. I have a separate question. How do I get it to display just one item? For this purpose, I only need to show the newest item added to the Promoted Links list. Thank you.

      • shaversnyc says:

        i wonder if it has to do with the sequence of the items in the list being disrupted. Any way around this?

      • shaversnyc says:

        One last thing to add about the issue I’m having. Steps to recreate:
        1. Apply the script to a promoted links web part as instructed – works great!
        2. Add new items to promoted links after script is applied – works great!
        3. Delete an item from promoted links – script still works great for remaining items.
        4. Add a new item (Item A) to promoted links – newest item is not resized.
        5. Add another new item (Item B) to promoted links – Item B is not resized but Item A is now resized.

        It seems to be not applying the script to the newest item only. Thank you very much for your help with this issue.

      • Clay Wright says:

        I’m having the same issue mentioned here by shaversnyc…troubling enough, if you add an additional item, it DOES fix the icon that was previously giving you grief. If you delete the latest item, the last item in the list is still unsized. Any update on this? I can’t find another way to work around it :\

      • mwentzel says:

        For future reference to those reading this, the reason for this is that the number of the subitems doesn’t get reused. Therefore if you have

        div id=Tile_WPQ2_1_1
        div_id =Tile_WPQ2_6_1
        div_id =Tile_WPQ2_2_1
        div_id =Tile_WPQ2_3_1
        div_id =Tile_WPQ2_4_1
        div_id =Tile_WPQ2_5_1
        div_id =Tile_WPQ2_7_1

        the delete link for div_id =Tile_WPQ2_3_1 and then add a new one you will then have something like:

        div id=Tile_WPQ2_1_1
        div_id =Tile_WPQ2_6_1
        div_id =Tile_WPQ2_2_1
        div_id =Tile_WPQ2_4_1
        div_id =Tile_WPQ2_5_1
        div_id =Tile_WPQ2_7_1
        div_id =Tile_WPQ2_8_1

        You still have 7 links (numberofPromotedLinks) but the IDs are not as expected and because a simple for loop is use for identifying the control IDs Tile_WPQ2_8_1 will not be operated upon. What I used instead is something along the lines of:

        // get all the promoted link elements
        var promotedLinks = $(‘#promotedlinksbody_WPQ’ + listId + ‘ > div.ms-tileview-tile-root’);

        for (i = 0; i < promotedLinks.length; i++)
        {
        // controlIdPrefix becomes a base for ControlID here…
        var controlIdPrefix = promotedLinks[i].id;
        controlIdPrefix = controlIdPrefix.substring(0, controlIdPrefix.lastIndexOf('_'));
        console.log('resizing ' + controlIdPrefix);

        // rest of code here….
        }

  27. Moe says:

    Hey. Got this code working as intended and I find it very useful, so big a big thank for you. However, now I’d like to make the ImageSize to apply for all of the promoted links so that there wouldn’t be the feeling of sub-menu. In other words the current code leaves two of the Images as default sized (150px) and I’d like to have all of them as e.g. 90px.

    Could you suggest me how to do this? Really seen a lot of effort but no results so far.

  28. Swapnil Phatale says:

    Thank You for your post.
    Its working fine.

  29. Swapnil Phatale says:

    Hi Umer
    How to show details box ribbon at the bottom of tile.
    Thanks in advance.

  30. Bryan says:

    Do you know how to make the left and right arrows return? Everything works great, but have a use for adding the arrows back.

    thanks.

  31. Evans says:

    Hi Umer, GREAT POST. It is working for me. Do you know if there is a way to adjust the font size of the text in the vertical slider? It appears not to be adjusting that at all. Thanks for a great post and your great support!

    • Umer Pasha says:

      thank you for encouraging words. I am away from SharePoint for a few days working on a different project. but I guess I’d start with using browser tools (f12) to pinpoint the Id or the class for that control with font and then try to manipulate font-size for that ID or class.

  32. Veasey says:

    Everything works except the images are not centered. When I increase the size to 200 the blue background shows. Somehow there is an inline style “right: 59px” showing up in the CSS.

  33. Josie Bond says:

    I have gotten the left and right arrows back but now when scrolling to view more links, it cuts off the last link half way. Where or how do I adjust how much to left it should scoll? When inspecing element I see that on the div with the class=”ms-promlink-body” it has put inline styling for left: -320px but needs to be at -435px to show all of the last link.

    Any help would be much appreciated.

    Thanks,
    Josie

  34. Jacqueline Broner says:

    This was working great but when I put another Promoted Links app on the same page in a different web part – some of the links from the first one show up in that part instead, and also the new links don’t resize. How do you adapt this script to work with multiple instances of it? Thank you!

  35. […] to 150 x 150. You can change them, but the solution is not an ‘on/off’ button but code. Click Here if you’re interested in changing the size, but given the aim of these posts, let’s just […]

  36. Carlton says:

    Umer,
    Thanks for the script. I did notice there is a comment at:
    //$(“#” + ControlID).top(ImageSize);

    This causes the Title of the Tile to not display unless you hover over it. Maybe you had this in by design?

    Thanks again for sharing!

  37. Caellhen says:

    Thanks a lot for the script! Really useful. There is a small problem though if you have more than 2 lines. The lines from the third and below appear with a huge whitespace before them. To correct this, simply change the line
    var pre = “<div class='ms-promlink-body' id='promlink_row_";
    with this:
    var pre = "<div class='ms-promlink-body' style='height: " + ImageSize + "px' id='promlink_row_";

  38. Mark says:

    I have a strange event. Everything worked great yesterday. I tried to add another promoted link and it was the original size and the ones from yesterday were 76. Then I tried to find the promotedlinksbody_WPQ to make sure it didn’t chage. I can not find it anywhere. Any ideas as to what happened? I even created a new page and put the promoted links on it and cant find promotedlinksbody_WPQ. Odd.

  39. […] a great solution posted by Umer Pasha here, however it was not enough, and actually what was there did not fully work with my promoted links […]

  40. I still cant get this to work at all. This is the solution I need just not working.

  41. I want to apply this code to all prompted link web parts on the page i have several and I am not clear on how i get then to just show one on each line and I have tried a bunch and still cant get this working. Help please

Leave a comment