2/24/2011

SharePoint: Prevent the Closing of web parts

 

The following works in both SharePoint 2007 and 2010.

 

If you have worked with SharePoint for a while you probably have learned that in most cases Closing a web part is “bad” and Deleting a web part is “good”.  (Do a web search on “SharePoint web part close vs delete”.)

 

So how can you prevent the closing of a web part?

How about five ways…

 

1) Every time you add a web part go to the Advanced section of the properties panel and uncheckmark “Allow Close”  (too much work)

image

 

2) Add a little JavaScript to prevent the Close:

image

 

3) Add a little JavaScript to disable the Close Option

image

 

4) Add a little JavaScript to hide the Close Option (probably the best choice)

image

 

5) Add a CSS style to hide the Close option (easy to do, but it leaves a blank line)

image

 

 

I prefer #4!

 

 

The JavaScript to prevent the close and display a popup:

Add the following to your site’s master page just before the </BODY> tag (A SharePoint Designer task!) or in a Content Editor Web Part (last one on the page) if you just want this for a single page.

<script>
  // JavaScript to block the closing of web parts
  // TechTrainingNotes.blogspot.com

  var oldClose = MSOLayout_RemoveWebPart;
  MSOLayout_RemoveWebPart = function (x)
  {
    alert("Closing of web parts has been disabled");
  }
</script>

 

 

The JavaScript to Disable or Hide the Close option:

Add the following to your site’s master page just before the </BODY> tag (A SharePoint Designer task!) or in a Content Editor Web Part (last one on the page) if you just want this for a single page.

<script>
// hide or disable the web part Close option
// TechTrainingNotes.blogspot.com

_spBodyOnLoadFunctionNames.push('hideWebPartClose');

function hideWebPartClose()
{
  var x = document.getElementById("MSOMenu_Close")

  // uncomment the next line to just disable (gray out)
  // x.disabled=true

  // uncomment the next line to hide the close option
  x.hidden=true

}
</script>

 

The CSS to hide the Close option:

Add this to your master page, your linked style sheet file or to a Content Editor Web Part.

<style>
  #MSOMenu_Close
  {
    display:none
  }
</style>

 

.

No comments:

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.