Pages

8/07/2010

SharePoint 2010: Change the “Add New Item” (and other) messages for a web part

 

This a SharePoint 2010 update to the SharePoint 2007 article found here: http://techtrainingnotes.blogspot.com/2009/07/sharepoint-change-add-new-message-for.html

 

The Goal

To change from “Add new item”:

    AddNew1

to “Click here to add team tasks”:

    AddNew2

 

ID’s used by web parts

Each web part has a different ID and some share a common ID. You will need to modify the following code examples with the ID used by your list. Note that some lists share the same ID, like Tasks and KPIs.

Note: Many of these IDs have changed in 2010!

If the ID you need is not in this table, display the web page with the web part, use the browser’s View Page Source option and search for the message text. Then look backward in the <A> tag and find the ID.

Web Part Default message ID for “.getElementById” in the code below
Announcements Add new announcement “idHomePageNewAnnouncement” (changed in 2010)
Links Add new link "idHomePageNewLink" 
Calendar Add new event "idHomePageNewEvent"
Picture Library Add new picture “idHomePageNewItem”  *  (changed in 2010)
KPI List Add new item "idHomePageNewItem"  *  (changed in 2010)
Tasks Add new item "idHomePageNewItem"  *  (changed in 2010)
Project Tasks Add new item "idHomePageNewItem"  *  (changed in 2010)
Document Library Add new document "idHomePageNewDocument"  (changed in 2010)
Wiki Add new Wiki page "idHomePageNewWikiPage"   (changed in 2010)
Discussion Add new discussion "idHomePageNewDiscussion"  (changed in 2010)
Custom List Add new item “idHomePageNewItem” *   (changed in 2010)

* = Shared with another list

 

If there is only one “Add new” message on a page

I.e. there is only one web part on the page that uses that ID then we can use this simple code:

<script>
  document.getElementById("idHomePageNewItem").innerHTML="your new text goes here"
</script> 

 

But the IDs won’t always work!

SharePoint can build pages with multiple elements with the same ID. This is not proper HTML, but, they do it…

So we will need something a little more complex:

<script>
var ttnA = document.getElementsByTagName('A');
for (var j=0; j<ttnA.length; j++)
{
  if (ttnA[j].id == 'idHomePageNewItem')
  {
    ttnA[j].innerHTML='your new text goes here'
  }
}
</script>

This code runs through all of the Anchor (<A>) tags in the page looking for the ones with the ID we need and then changes the text as needed.

What if you only wanted to change the second web part that uses that ID to a new message? Then try this:

<script>
var ttnA = document.getElementsByTagName('A');
var ttnCounter = 0;
for (var j=0; j<ttnA.length; j++)
{
  if (ttnA[j].id == 'idHomePageNewItem')
  {
    ttnCounter ++;
    if (ttnCounter == 2)
      { ttnA[j].innerHTML='your new text goes here' }
  }
}
</script>

 

Where should you add the code?

Add the code to the page using SharePoint Designer

  1. Edit the page using SharePoint Designer
  2. Find the “<asp:content” tag for PlaceHolderMain and then find the matching end tag (“</asp:content>”)
  3. Just before the end tag, add the JavaScript listed above

Or add a Content Editor Web Part to the page

This is the easiest for testing, and does not require SharePoint Designer. In SharePoint 2007 this worked equally well for web part pages and View pages. In SharePoint 2010, adding a web part to a View page causes the View selector dropdown to be hidden, so the SharePoint Designer approach might be better for View pages. The CEWP works just fine for web part pages and the new “wiki style” home pages. The only got’ya is that they have removed the Source Editor button from the CEWP. If you use the Edit HTML button in the Ribbon you may find that your HTML and JavaScript gets modified by their editor. Best practice with the 2010 CEWP is to store your HTML and JavaScript in a text file in a library and then use the CEWP’s link to a file option.

  1. Create a text file with the JavaScript code and upload to a library
  2. In the library right-click the file, click Properties and copy the URL of the file
  3. Add an Content Editor Web Part (CEWP) below the web part you want to customize
  4. From the CEWP click web part’s dropdown arrow and click Edit Web Part
  5. Paste the URL you copied into the Content Link box

 

Hide the “+” image?

If you want to get rid of the little icon just before the “New Item” text then just add one more line of code after where you change the message:

     ttnA[j].innerHTML='Click here to add team tasks';
     ttnA[j].previousSibling.previousSibling.style.display='none';

 

 image    image

 

Hide the entire message?

No tricks needed!  Just edit the web part and change the “Toolbar Type” to “No Toolbar”. (The “New” link we’ve been working with is the “Summary Toolbar”.)

image

 

.

38 comments:

Jimbo Alba said...

Thanks for the post Mike. It's simple and direct to the point.

Maureen S said...

Great post - I'm trying to accomplish the opposite. I am working with very long lists and our users are in need of the "Add New" option at the top of the list. I'm new to the collection sites and group that I'm working with so while gathering requirements, I'm finding longer and longer lists that are all set to the default view.

Is there an easy way to customize the list web part and add an additional (or move the existing) "Add New" item option to the top of the list to avoid users having to scroll to the bottom? Any direction to this information is VERY appreciated :) Thank you for a great article!

Mike Smith said...

Maureen,

> add an additional (or move the existing) "Add New" item option to the top of the list

All you need is the URL to the page and know how to add a link to the page above the list...

Right-click the existing "Add new" link and click Copy Shortcut. You now have the URL to the page.

If you are in a Wiki style page (default.aspx, etc), edit the page.
Otherwise, add a Content Editor Web Part (CEWP) to the page and edit it.

Type the "add" text ("Add new announcement"), then select it, then click the HyperLink button in the ribbon and paste the link you copied earlier.

Mike

Sal said...

Hi, although this looked very promising, no matter what I did, it did not work for me. I used a content editor web part exactly as you instruct on a web part page with a web part of a document library and it did not change the text.

Mike Smith said...

Sal,

First... are you using SharePoint 2010?

And make sure you move the Content Editor Web Part below the web part you want to customize.

If you want to test to see if you have the ID right, go the web part page, then in the browser's address bar, replace the page's URL with the following:

javascript:alert( document.getElementById("idHomePageNewAnnouncement") )

If when you press Enter you get a popup with anything but "null" then you have a correct ID.

To see if you have working JavaScript, add the following just before the /script tag:

alert('JavaScript ran')

When you refresh the page you should see a popup with that message.

Mike

audunms said...

Hi,
thanks for an excellent article, providing a more robust solution than others.
However, if you insert this code in a list view, the tab giving access to the list settings dissapears and you can no longer edit the list. Somehow this code intefers with the OOB JavaScript on the page.

Mike Smith said...

> audunms

The code does not interfere with the ribbon when added to a non-View page. What you are describing is common to view pages, and according to Microsoft, is "by design". If you add a web part to a view page, MS no longer considers it to be a view and hides view related features. You can still edit the list by first clicking any item in the list. That will wake up the ribbon.

You can work around this problem by editing the view page in SharePoint Designer and adding the code just before the end tag () of the PlaceHolderMain tag.

Mike

lio said...

Is there a way to replace/change the graphic icon before the 'Add new Item" - tried to look for a css but except the A.ms-addnew which controls the text itself, I cannot find a way to change the icon. thx all for your help.

Mike Smith said...

lio,

Hiding the icon is easy:

<style>
.ms-addnew img
{
display:none;
}
</style>


Changing the icon require a trick using a background image:

<style>
.ms-addnew img
{
display:none;
}

.ms-addnew a
{
background:url(_layouts/images/titlegraphic.gif) no-repeat;
padding-left: 25pt;
}
</style>

Change the URL to the path to your picture and set the padding to a bit more than your image width.

Mike

Anonymous said...

I had inherited a site with custom code. This is exactly to pointer I needed. Thank you for your post.
Doo4fun.

Anonymous said...

THANKS! works like a magic!

In the same time, if you want to get rid of the "recent changes" links in the upper right menu you can add the following to the text file:

< style type="text/css">
.s4-recentchanges
{
display:none;
}
< /style>

Unknown said...

Thanks for this. It works very well - I have saved the webpart as a template and can apply it to webpart zones as required. Sweet!

Any way to alter the text "There are no items to show in this view of the 'Announcements' List"?

Mike Smith said...

Ishv,

Yes, see here an example:
http://techtrainingnotes.blogspot.com/2009/08/sharepoint-change-no-items-message-in.html

Also my book as more info on doing this on other web parts.


and also take a look at this:
http://techtrainingnotes.blogspot.com/2009/08/sharepoint-change-no-items-message-in.html

Mike

epaloski said...

Mike -- you are a lifesaver! Thanks so much for this post.

Alan Sutherland said...

You genius!

One question, if I have 4 web parts on one page and want a different 'add' text for each one, what would be the iteration in the script?

I've tried cutting and pasting some of the lines, but it hasn't worked for me

Mike Smith said...

Alen Roy,

The second example would be your starting place. Just add an IF for each web part.

if (ttnCounter == 1)
{ ttnA[j].innerHTML='your new text for #1 goes here' }
if (ttnCounter == 2)
{ ttnA[j].innerHTML='your new text for #2 goes here' }
if (ttnCounter == 3)
{ ttnA[j].innerHTML='your new text for #3 goes here' }
if (ttnCounter == 4)
{ ttnA[j].innerHTML='your new text for #4 goes here' }

Mike

Anonymous said...

Hi Mike,
Do you have code for closing a sharepoint form once it's been submitted? Once the form is submitted it just goes to the Infopath library rather than closing the form.

Mike Smith said...

Anonymous,

No, not at hand, but I have an idea. I working on the list of JavaScript handlers that are used to launch various activities, including InfoPath. Check back in a few weeks.

Mike

maria said...

Oh my God! Thanks so much! You are so great!
It has always been very hard to find solutions that don't require Designer, Infopath or other stuff(as I am not allowed to use anything external)

Mike Smith said...

maria,

Thanks for your kind comments!

> It has always been very hard to find solutions that don't require Designer, Infopath or other stuff(as I am not allowed to use anything external)

Then you would really like my book! ;-)

Mike

Elaine S. said...

Thank you so much for posting these intructions - this is exactly what we wanted to accomplish. To make it work, I had to add the actual java script to the CWEP in addition to the content link. Any idea why?

Thanks again!

Mike Smith said...

Elaine S,

Actually, you should not be able to add both a linked content file, and inline code to the CEWP. Which version of SharePoint are you using? 2007 or 2010?

Make sure the CEWP is below the web part you are trying to change.

Mike

Brahmi said...

Hey Guess Im getting the same problem as Sal.
When I run this script - javascript:alert( document.getElementById("idHomePageNewAnnouncement") )
it does return null.
How exactly do I find the Id of "Add Document" in sharepoint 2010?

Brahmi said...

Mike
Figured it out.
Works perfectly!
Thanks

Mike Smith said...

Brahmi,

Looks like you got it sorted out!

The two most common problems are:

The JavaScript (and the CEWP) are not below the list being updated.

There is more than one message with the same ID. If that's the case then you need to pick one:

document.getElementById("idHomePageNewAnnouncement") )[0]

or

var txt = document.getElementById("idHomePageNewAnnouncement") )

then do something with txt[0] (or txt[1]...)

Mike

Amy said...

Hi Mike - thank you so much for this post, it was incredibly helpful and allowed me to move the "Add new discussion" to the top of the webpart. My team will be happy that they don't have to scroll all the way down!

I do have one question - when I click the new "Add new discussion" link, it takes me to a new page to enter my post. Is it possible to keep the original functionality of the form opening directly on the page?

Thanks again for your help.

Anonymous said...

I am displaying a list on the homepage using GridView.. I wanted to include an Add New link but, I cannot figure out how to show/hide the link depending on the user's permissions... is it possible?

Mike Smith said...

Anonymous,

> I wanted to include an Add New link but, I cannot figure out how to show/hide the link depending on the user's permissions... is it possible?

Yes. Two ways...

First way:

First you need to discover the link. On a page where you see the Add New link, right-click the link, select properties and copy the URL. For one of my lists it looks like this:

https://myservername/sites/mystiesname/_layouts/listform.aspx?PageType=8&ListId={CB24DD78-DE7F-427D-87AC-6F9692CDECCD}&RootFolder=

Add this to a Content Editor Web Part (CEWP) as a hyperlink.

<a href="https://microsmithinc.sharepoint.com/sites/training/_layouts/listform.aspx?PageType=8&ListId={CB24DD78-DE7F-427D-87AC-6F9692CDECCD}&RootFolder="> Click here to add a new item </a>

If you want to who can click the link you will need add a SharePoint Security Trimmed Control. But... server controls will not work in a CEWP. You will need edit the page that holds the web part. It would look something like this:

<Sharepoint:SPSecurityTrimmedControl runat="server" Permissions="ManageLists">

<a href="https://microsmithinc.sharepoint.com/sites/training/_layouts/listform.aspx?PageType=8&ListId={CB24DD78-DE7F-427D-87AC-6F9692CDECCD}&RootFolder="> Click here to add a new item </a>

</SharePoint:SPSecurityTrimmedControl>

Second way:

If the "Add New" is already being displayed for your list then you use the Security Trimmed Control to wrap up some JavaScript to show or hide the link.

I have a chapter in my book on security realted customizations that covers the use of this control.


Mike

Anonymous said...

Dieses Stück von Code verursacht das Fehlermeldung in SP-Designer:
sollte man stattdessen dies ohne leerspace anwenden.

Aber und dann fünzt ganzes nicht.
:(
ich mache das in Designer
Seite -> Listename -> AllItems.aspx
füge js-code ==> geht nicht!

gautamsheth said...

Hi Mike, I need to remove "+add new item" from all the document libraries in the site. So should i insert it on each AllItems.aspx page ? Could you please guide me further .

I understood the post till javascript portion , after that i lost it . I need to know where exactly should i insert ?

Parija said...

How to change "Add new item" in Asset Library?

Mike Smith said...

Parija,

The ID is the same as for a regular library: "idHomePageNewItem"

Mike

Shae said...

Thank you for this post! It was just what I needed!

Anonymous said...

Hi, sorry for digging this 'old' topic. I got a problem on the 'Add new discussion' link. When I clicked on the link, filled in the popup window, then clicked 'Save'. Nothing happened at all. No new topic was added to the discussion board. But the button 'New item' on the left top of the screen worked fine. I have to add new topics through that button in an inconvenient way for now. Do you have any ideas on what's wrong with my site?

Mike Smith said...

Anonymous,

Did you make the customizations in this article, or are you just asking in general?

Mike

Mark said...

Excellent article, thanks Mike!

I was wondering how to add a querystring to the "Add new item" linl URL. I am using sublists and want to pass the ID of the parent list to the new sublist record being created. Any idea how to do this?

Thanks again,
Mark

Haider said...

Mike,

Great post. I did have a question. I do see that this covers text change for the main page ("idHomePageNewDocument"), how can we apply this change to the text in Document Libraries? What is the id for document libraries?

Thank you

Mike Smith said...

Haider,

The id is the same for the web part on any web part page. While the ID includes "HomePage", the ID is the same on every page where the web part is used.

Mike

Post a Comment

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.