I want to implement a collapsing/expanding feature to all my webparts in a certain page and tried the code shown here: http://moblog.bradleyit.com/2009/08/sharepoint-expand-or-collapse-web-parts.html but for some reason it didn't work for me at all. other JS codes usually work as intended. I really don't know why this one isn't working.

here http://usermanagedsolutions.com/SharePoint-User-Toolkit/Pages/Bookmarklet-Expand-Collapse-Web-Parts.aspx

I see a working example of a code bookmarklet the question is if I can place the code in a CEWP and define it somehow to work on page load rather then on click.


  • Maybe you're working with SP 2010 and the code was for SP 2007? – Christophe Dec 21 '11 at 21:22
  • the opposite would be correct. but your point remains valid. although this code was posted on 2009 so my guess is that it is meant for SP 2007. – Jake Dec 22 '11 at 0:13
  • If you're on SP 2007 see my updated answer. – Christophe Dec 22 '11 at 0:58

I have used that piece of code (http://moblog.bradleyit.com/2009/08/sharepoint-expand-or-collapse-web-parts.html) before (in SP2007) and it has worked. As it is, it will execute on load (as per the spBodyOnLoad...

  1. Make sure you have the web part names are correct - do some debugging to confirm this.
  2. Try and invoke the methods manually instead of letting sharepoint do it. Then you can see what it is doing.

Internet Explorer Development tools come in very handy to determine why JavaScript is not behaving!

  • mmm... if it worked for you on SP2007 it most work for me! maybe I made some mistake... I'll recheck my code. also what did you mean by "1. Make sure you have the web part names are correct - do some debugging to confirm this." from the code I understood that the only webparts names you should provide to the script are the ones that you want the script to skip and not collapse. – Jake Dec 22 '11 at 0:20
  • Yep. But you can confirm that it is behaving correctly on your page iteractively using the dev tools. – Russell Dec 22 '11 at 0:29
  • In the end I managed to make this bit of code work: moblog.bradleyit.com/2009/08/… I tested every raw of the code with the dev tools you recommended and found out I had to make some minor changes to the code. – Jake Dec 26 '11 at 23:11

I am going to publish a solution for this in the User Toolkit next month.

For now, the solution is still in draft state and can be found here:


I'll be happy to respond to your questions if you want to try it out.

[Edit] My initial solution, published 3 years ago, for SP 2007:


The new one on Codeplex should work on both SP 2007 and SP 2010.

[Edit 2] The bookmarklet should also work, you just need to reformat it with script tags instead of "javascript:".

  • Added two edits. Three solutions in three different locations, I definitely need to do some cleanup... – Christophe Dec 22 '11 at 1:09

My guess is that the code isn't working because SharePoint likes to mess with code that is directly input into the CEWP.

Instead of putting that code directly into a CEWP create a txt file that contains all of the code. Place that txt file somewhere you can access it (like SiteAssests). Then in the CEWP options reference the location of the txt file.

That should prevent SharePoint from messing with the code, which may solve your issue.

  • Thank you for your suggestion. I will try it tomorrow and see how that works for me. – Jake Dec 22 '11 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.