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:
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:
- Open Notepad and paste in the JavaScript from below
- Save the Notepad file with a name like "MoveAddNewItem.html"
- Upload this Notepad file to a library such as "Site Assets", "Site Files" or other library
- Go to that library, right-click this file and copy the Shortcut (the URL to the file)
- 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.
- Save your changes and test the result
Steps to make this change for all pages:
- Using SharePoint Designer, edit your master page
- 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:
This is awesome! It worked for us on the first try! THANKS so much.
This worked great but after a few seconds, it disappears and is back at the bottom. Suggestions? Thx.. Mike
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
No, no changes. Maybe our corporate people have something in place that's causing it. Bummer. I was all excited... Mike
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
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
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.
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
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
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
Wow Mike !! this is Fabulous,i worked this for entire site collection, looks very cool. Thank you so much for information
Hi,
I need to move "next" & "previous" button on top.
Please help me
Thanks Mike...worked great, especially for someone who doesn't know how to write code.
Awesome! worked perfectly! thank you.
Post a Comment