Monday, July 30, 2012

Disable Publish button using Javascript

I wont suggest you to use the following approach to disable Publish button of a  NewItem.aspx page. The suggested approach is to configure security by excluding "Approve Items" permission for a permission level and assign it to SharePoint Group.


I agree that wont be the case every time - MyTeam faced same challenge today where we had a specific requirement to disable publish option from NewItem Page. Here is the quick and easy way to do it.


1. Save the following script in a .js file

<script language = "javascript"> 
varButtonClassName ="ms-ButtonHeightWidth"
varButtonClassName = new RegExp("(^|\\s)" + varButtonClassName + "(\\s|$)") ;  
varRibbonClassName ="ms-cui-ctl-large" 
varRibbonClassName = new RegExp("(^|\\s)" + varRibbonClassName + "(\\s|$)") ; 
varPublishText = "Publish"   
varPublishText = new RegExp("(^|\\s)" + varPublishText + "(\\s|$)")  ; 
elements = document.getElementsByTagName("*");  
for (var l=0, ll=elements.length; l<ll;l+=1)          
{                 
node = elements[l];     
if(varButtonClassName.test(node.className))    
   if(varPublishText.test(node.value)) node.disabled = true;     }    if(varRibbonClassName.test(node.className))    
   if(varPublishText.test(node.value)) node.disabled = true;     } 
} 
</script>
2. Upload the .js file into a library in the site
3. Edit NewItem.aspx from SharePoint UI and add a Content Editor Web Part  below the data entry web part
4. Edit the web part and refer the uploaded .js file to it
5. Save web part properties
6. Exit page edit
7. Refresh the  page ......... See the magic :) 

No comments:

Post a Comment