Click here to Skip to main content
Click here to Skip to main content

SharePoint Customization Tricks - Part 3

By , 31 Jan 2009
 

Introduction

This multipart series of articles is intended to help you getting ramped up with SharePoint Customization. It's about modifying the default SharePoint user experience, list forms customization, branding, skinning SharePoint portals, etc.

In Part 1, I introduced a generic function that you can use to hide the list view toolbar menu items (e.g. New Item, Upload, Edit in Grid view, etc.). In Part 2, I showed you how to remove items from the list form toolbar. If you haven't read both posts yet, I would encourage you do to that first (http://www.ayman-elhattab.blogspot.com).

After posting the previous articles, I received a lot of e-mails from SharePointers asking how to rename the items in the list form toolbar. That's why I decided to write Part 3 in the series. After I finished writing the article and taking some snapshots, another SharePointer asked me on MSDN Forums if it's possible to remove the Text while retaining the images, for instance, removing "Delete Item" while retaining the (X) rendered beside it. Today I'll show you to do both tasks by JavaScript.

Let's get started!

Trick #4: Renaming List Form Toolbar Items!

Sometimes, you need to rename some items in the toolbar rendered at the top of DispForm.aspx to match the List Name. For instance, you have a custom list named "Programs" that stores the courses offered by a university. It would be better to have "New Program" rather than the default "New Item" and "Edit Program" rather than "Edit Item". Sometimes, you just need to totally delete the text and just leave the images with a tooltip that explains the action performed on clicking them.

Before

trick4_be4.JPG

After

trick4_after.JPG

The following function can be used to achieve both tasks, just call the function passing the old and new names of the item. For instance if you need to rename "Add Item" to "Add Program", use renameFormMenuItems("Add Item","Add Program");. In case you need to remove the text and leave the image, call the function passing an empty string as the new name as follows: renameFormMenuItems("Delete Item","");.

renameFormMenuItems("New Item","New Program");
renameFormMenuItems("Edit Item","Edit Program");
renameFormMenuItems("Delete Item","");
function renameFormMenuItems(oldName,newName)
{ 
 var anchorTag;
 var allAnchorTags = document.getElementsByTagName('a');    
 
 if(oldName.length!=0)
 { 
  for (var j = 0; j < allAnchorTags.length; j++)
  {
   anchorTag= allAnchorTags[j];    
 
   if (anchorTag.innerText.indexOf(oldName)!=-1)
   {                   
      
     anchorTag.innerText=newName; 
     try
     {
      if(newName.length!=0)
       {              
    anchorTag.parentNode.previousSibling.firstChild.
          firstChild.alt=newName;      
      }
      else
      {
         anchorTag.parentNode.previousSibling.firstChild.
    firstChild.alt=oldName;     
      }
     }
     catch(err)
     {
     }          
   }     
  }
 }
}

Kindly note that the function is case sensitive, so "New Item" will work while "nEw iTeM" is not gonna work.

Nice and easy, isn't it?

Now a question is imposing itself, how can we use this function? The answer is the content editor web part; please refer to trick #3 in the previous article for more information.

What about hiding the toolbar altogether?

trick4_remove.JPG

There are a lot of solutions that imply unghosting the page using SharePoint Designer, but I don't prefer those solutions! Again, let's perform this task using JavaScript. I've included two functions in the zip file, one for renaming the toolbar items and the other for hiding the toolbar altogether! Waiting for your feedback.

History

  • 31st January, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ayman M. El-Hattab
Architect
Egypt Egypt
Member
Ayman El-Hattab is a Regional Technology Solution Professional focusing on helping software professionals and organizations build better SharePoint Solutions using Microsoft Application Lifecycle Management (ALM) technologies and tools. Ayman has been in this role since 2010 and has presented at many conferences all over the Middle East & Africa about ALM, SharePoint, C#, asp.net and Business Intelligence technologies. Ayman is also a Microsoft Most Valuable Professional [SharePoint MVP] , ALM Ranger, published author and an enthusiastic speaker who enjoys working with the online and offline developer communities all over the world. Ayman is the founder of MEA ALM Community & SharePoint4Arabs, community lead at Egypt SharePoint User Group and an organizer of several SharePoint Saturday events. Outside of work, Ayman can be found watching soccer games, playing xbox or watching documentary movies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionRe: Customization tricks pt 3memberSyed Abas20 Oct '09 - 17:22 
LOve your work. But how about if I want to make the toolbar appear only if the current user is [Me].
 
Appreciate very much your reply.
QuestionJS Hackmemberdirq18 Mar '09 - 2:51 
Just wondering...
 
Is this the only way to do this? Or is there a way to rename without using JavaScript? Can something be done with a custom list schema or features?
 
Otherwise, it's a clever solution and I'll probably use it if I can't find a more robust way of changing the menu text.
 
Thanks,
 

AnswerRe: JS HackmemberAyman M. El-Hattab4 Apr '09 - 12:42 
Hello Dirq,
No I couldn't find any other way to do the trick ..
Thanks a lot for your nice comment Smile | :)
 
Ayman M. El-Hattab
Microsoft Certified SharePoint Specialist
ITWorx Egypt
http://ayman-elhattab.blogspot.com

GeneralCannot get hide menu items to workmemberMember 36793193 Feb '09 - 0:44 
Hi
 
I can get the rename menu items code to work, however, when i use the hide menu items code that you gave in part two of your series of articles it doesnt seem to work, all the menu items are still visible.
 
I just pasted the hide menu items code into the same CEWP on the page however it doesnt seem to have any effect to the menu items. Also i note in your article that you you say there is not edit page option, however from my 'site actions' menu bar i have an option to edit page, am i in the wrong place?
 
Could you please give some advice on what i am doing wrong.
 
Many thanks
 
Kevin
GeneralRe: Cannot get hide menu items to workmemberAyman M. El-Hattab3 Feb '09 - 0:57 
Kevin,
How is everything ?
I'm glad that you got the rename menu items code to work.
For Hiding the toolbar menu items, Did you surround the code by script tags ?
e.g : <script language="javascript" type="text/javascript">
// Code goes here
</script>
Please also makse sure that you inserted the content editor web part below the form and not above it.
 
Ayman M. El-Hattab
Microsoft Certified SharePoint Specialist
ITWorx Egypt
http://ayman-elhattab.blogspot.com

GeneralRe: Cannot get hide menu items to workmemberMember 36793193 Feb '09 - 1:02 
Hi
 
Thanks for such a quick reply.
 
I have to CEWP below the list item on the page, and it successfully works for the rename menu items. I then just paste the hide menu items code in place of the rename code.
 
This is the exact code that I am using:
 
<script language="javascript" type="text/javascript">
hideFormMenuItems("New Item","Alert Me");

function hideFormMenuItems()
{
var titleToHide="";
var anchorTag;
var allAnchorTags = document.getElementsByTagName('a');
for(var i = 0; i < hideFormMenuItems.arguments.length; i++ )
{
titleToHide = hideFormMenuItems.arguments[i];
if(titleToHide!='Alert Me')
{
for (var j = 0; j < allAnchorTags.length; j++)
{
anchorTag= allAnchorTags[j];
 
if (anchorTag.title.indexOf(titleToHide)!=-1)
{
anchorTag.parentNode.parentNode.parentNode.parentNode.
parentNode.style.display="none";
anchorTag.parentNode.parentNode.parentNode.parentNode.
parentNode.nextSibling.style.display="none";
break;
}
}
}
else
{
for (var k=0; k < allAnchorTags.length;k++)
{
anchorTag= allAnchorTags[k];
if (anchorTag.id.indexOf("SubscribeButton")!=-1)
{
anchorTag.parentNode.parentNode.parentNode.parentNode.
parentNode.style.display="none";
break;
}
}
}
}
 
var allSpanTags = document.getElementsByTagName("span");
var spanTag;
var toolbarRow;
var lastCell;
 
for(var m=0; m < allSpanTags.length;m++)
{
spanTag = allSpanTags[m];

if(spanTag.id=='part1')
{
toolbarRow = spanTag.childNodes[2].firstChild.firstChild;
lastCell = toolbarRow.lastChild.previousSibling;
while(lastCell.style.display=='none')
{
lastCell = lastCell.previousSibling;
}
if(lastCell.innerText == '|')
{
lastCell.style.display='none';
break;
}
}
}
</script>
 
BTW i must congratulate you on an excellent series of articles Smile | :) Smile | :) Smile | :)
 
Kevin
GeneralRe: Cannot get hide menu items to workmemberAyman M. El-Hattab3 Feb '09 - 1:18 
Thanks Kevin Smile | :)
tell me, did you get any javascript errors ?
 
Ayman M. El-Hattab
Microsoft Certified SharePoint Specialist
ITWorx Egypt
http://ayman-elhattab.blogspot.com

GeneralRe: Cannot get hide menu items to workmemberMember 36793193 Feb '09 - 1:47 
No i am not getting any javascript errors whatsoever.
 
The page renders fine, just no hidden menu items Frown | :( Frown | :( Frown | :(
 
I am using IE7 could that be an issue?
 
Thanks
 
Kevin
GeneralRe: Cannot get hide menu items to workmemberMember 36793193 Feb '09 - 2:06 
Hi there
 
I tried one last time, and downloaded the code in the zip file instead of copying and pasting from the article and it worked like a treat.
 
Thank you so much for this code as I have a project where this will be used extensively.
 
Thanks again so much Smile | :) Smile | :) Smile | :)
 
Kevin
GeneralRe: Cannot get hide menu items to workmemberAyman M. El-Hattab3 Feb '09 - 2:47 
gr8 Smile | :)
I'm glad that it worked.
I'm afraid, this javascript is only created and tested for SharePoint 2007, I've never tried to use it on SharePoint 2003.
Stay tuned for other articles in the series ..
Bye
 
Ayman M. El-Hattab
Microsoft Certified SharePoint Specialist
ITWorx Egypt
http://ayman-elhattab.blogspot.com

GeneralRe: Cannot get hide menu items to workmemberMember 36793193 Feb '09 - 2:42 
Hi
 
One final point, how would i get the hide menu items code to work in SharePoint 2003. I can get the rename menu items code to work by using the same steps as you outlined for MOSS 2007, however, the hide menu items code does not seem to work in SharePoint 2003.
 
Any further help would be very much appreciated.
 
Kind regards
 
Kevin
QuestionTrying to ger rid of the deletememberAmy Thompson2 Feb '09 - 7:28 
Hi, I am trying to use your tips, but having some trouble.
I added a content query web part - I have a custom List Form on the page. I added the javascript code
(hideFormMenuItems("Delete Item");
function hideFormMenuItems()
...)
in the content query web part.
Maybe I missed something, but it doesn't even appear to run the javascript (I added some alerts and they don't show). There are no js errors, so I know that is ok.
 
My goal is to remove the Delete Item and X completely.
Any ideas?
thanks
Amy
AnswerRe: Trying to ger rid of the deletememberAyman M. El-Hattab2 Feb '09 - 12:27 
hi Amy,
surround the javascript code with the script tags as follows :
<script language="javascript" type="text/javascript">
//java script code
// Your code goes here
</script>
refer to part 2 in the series for the correct positioning of the Content Editor Web Part
let me if you need any help
 
Ayman M. El-Hattab
Microsoft Certified SharePoint Specialist
ITWorx Egypt
http://ayman-elhattab.blogspot.com

GeneralRe: Trying to ger rid of the deletememberAmy Thompson3 Feb '09 - 7:14 
Hi again
I do have the correct javascript around the code. I also do have the content editor web part below my dataformwebpart.
 
The difference however with my situation, is I am trying to do this in a custom EditForm rather than in a DispForm..... So, wondering if you have any tips to get it to work in the EditForm.
I have hidden/closed the ListForm webpart and the custom list form is where I would like to modify the toolbar (remove delete).
 
Thanks alot
Amy
GeneralRe: Trying to ger rid of the deletememberAyman M. El-Hattab3 Feb '09 - 11:06 
try to download the function rather than copying and pasting it
and try again
waiting for your feedback
 
Ayman M. El-Hattab
Microsoft Certified SharePoint Specialist
ITWorx Egypt
http://ayman-elhattab.blogspot.com

GeneralRe: Trying to ger rid of the deletememberAmy Thompson4 Feb '09 - 4:49 
I did get the function from the downloaded file. Just tried it again to be sure. Still no luck.
Appreciate any help . thanks
Generalpretty gudmemberlovedotnet1 Feb '09 - 18:58 
hii...thanks for the article..its gud..i am a fan of ur articles..i read all the three parts...waiting for the next part Smile | :)
 
regards,
pradeep. Smile | :)
GeneralRe: pretty gudmemberAyman M. El-Hattab1 Feb '09 - 20:21 
Thanks alot pradeep
please let me know your ideas and expectations for the following parts Smile | :)
 
Ayman M. El-Hattab
Microsoft Certified SharePoint Specialist
ITWorx Egypt
http://ayman-elhattab.blogspot.com

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 31 Jan 2009
Article Copyright 2009 by Ayman M. El-Hattab
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid