1/07/2012

SharePoint: Move the “Add New” Link to the Top of the Web Part

 

When you display a long list in a web part you will find that the “Add new” link is often out of sight at the bottom of the page. Your users will have to scroll to the find the link. This article takes a look at some JavaScript to change the order things are displayed in a web part.

 

The before and after:

image

 

Note: This has been tested on only one SharePoint 2010 installation, so use at your own risk and test, test and test.

 

Steps to make this change on only one page:

  1. Open Notepad and paste in the JavaScript from below
     
  2. Save the Notepad file with a name like "MoveAddNewItem.html"
     
  3. Upload this Notepad file to a library such as "Site Assets", "Site Files" or other library
     
  4. Go to that library, right-click this file and copy the Shortcut (the URL to the file)
     image
     
  5. Add a Content Editor Web Part below the last web part on the page, edit this web part, and paste the URL you just copied into the Content Link box.
    image
     
  6. Save your changes and test the result

 

Steps to make this change for all pages:

  1. Using SharePoint Designer, edit your master page
  2. Add the JavaScript below just before the </BODY> tag

 

The JavaScript

Notes:

  • The Try Catch is to deal with the picture library and the calendar, which do not add the “add new” links
  • If you are modifying the code, you will want to remove the Try Catch during your testing
  • The test for "ms-bottompaging" is for long lists that are displaying a previous / next links
  • The line that starts with “theTable = x” is one long line  ( theTable = x[i]…… )
<script>
// CEWP trick from techtrainingnotes.blogspot.com! 
// techtrainingnotes.blogspot.com/2012/01/sharepoint-move-add-new-link-to-top-of.html
// Find all tables
var x = document.getElementsByTagName("TABLE"); 
var ListList = "";

for (var i=x.length-1; i>0; i-- ) 
{
  // find just the tables that are list web parts (have a "summary" element)
  if (x[i].summary)
  {
    try {
      // Now find the "add new" rows
      var theTable = x[i].parentNode.parentNode.parentNode.parentNode.nextSibling;
      // but if the table is the paging table (next / previous) then get the next table
      if (theTable.innerHTML.indexOf("ms-bottompaging") > 0)
      {
        theTable = x[i].parentNode.parentNode.parentNode.parentNode.nextSibling.nextSibling.nextSibling;
      }

      // hide the unneeded horizontal line
      theTable.rows[0].style.display="none";
      // hide the extra blank space
      theTable.rows[2].style.display="none";

      // move the table    
      var theContainer = x[i].parentNode.parentNode.parentNode.parentNode.parentNode;
      theContainer.parentNode.insertBefore(theTable,theContainer);
    }
    catch (err123) {}
  } 
}

</script>

 

.

14 comments:

Anonymous said...

This is awesome! It worked for us on the first try! THANKS so much.

Anonymous said...

This worked great but after a few seconds, it disappears and is back at the bottom. Suggestions? Thx.. Mike

Mike Smith said...

A few seconds? Sorry I have not idea. It should either work or not work. Did you make any changes after adding the JavaScript?

Mike

Anonymous said...

No, no changes. Maybe our corporate people have something in place that's causing it. Bummer. I was all excited... Mike

Anonymous said...

Here's an update - this is on a blog page in sharepoint and every time it auto-updates, the link goes back to the bottom. -Mike

Mike Smith said...

Not sure what you mean by "auto-updates", but check the URL of the page before and after adding the code. Are you looking at a different page?

Mike

Mike said...

It's a Sharepoint blog for our group. It's set to refresh every 30 seconds. The URL doesn't change, but at every refresh, the Add New Item goes back to the bottom. If I leave the page and come back, it's there again until the next auto refresh.

Mike Smith said...

Ok, I need some more info...

What kind of list or library is this being done on?

Is the autorefresh you are using set in the web part's AJAX settings section? If so, the web part is being regenerated on each refresh and our custom JavaScript is not being rerun. I created a fix for this for a similar issue in calendars. (it'll take me a while to find it.)

Mike

Anonymous said...

This code works very nice! Here's a potential issue though: by adding a CEWP the "Tools" tab on the ribbon disappears.

Is there anyway to have that critical tab show?

Thanks

Mike Smith said...

Anonymous,

Actually, if you add any web part to a view page in SP 2010 the ribbon is hidden. The reason is that the additional web part might be another list web part and the ribbon for it would be different.

If you click in the list web part the ribbon will be displayed.

The work around is to not use the CEWP. Edit the page using SharePoint Designer and add the JavaScript just before the end tag for the PlaceHolderMain tag.

Mike

Anonymous said...

Wow Mike !! this is Fabulous,i worked this for entire site collection, looks very cool. Thank you so much for information

Zozo said...

Hi,

I need to move "next" & "previous" button on top.

Please help me

Anonymous said...

Thanks Mike...worked great, especially for someone who doesn't know how to write code.

Anonymous said...

Awesome! worked perfectly! thank you.

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.