The idea
The home page is a natural place to expose the content of your SharePoint site. However, often this page becomes over packed, and this affects the user experience.
One way to improve readability is to append an expand/collapse button to each Web Part. At page upload, only the main Web Parts are expanded, but the user has the option to open other Web Parts he/she is interested in.
The idea is not from me, a script for SharePoint 2003 was posted on a blog a couple years ago [Placeholder for the credits – unable to locate the original post at this time]. But this script doesn’t work on SharePoint 2007.
This week, a reader reminded me about this, and asked me if I had a solution for SharePoint 2007. So here it is! Note that I have written my script from scratch using a different method, specific to SharePoint 2007.
Warning! Don’t abuse of this: even though the content of the Web Parts is not visible, it is still loaded with the page, and too many Web Parts will impact performance.
Below an example with a calendar: usually, the week view and month view are too big for the home page. One way to deal with it is to use the minimized state by default.
Collapsed:

Expanded:

Of course, another option would be to use the technique described here.
The method
This is simply another use of the CSS display property: the display is set to “none” to hide a Web Part.
The code
Drop a CEWP anywhere on your page and bind it to this code:
<script type="text/javascript">
//
// Expand/Collapse Buttons
// Feedback and questions: Christophe@PathToSharePoint.com
//
function WPToggle(thisId, ImageId)
{
if (document.getElementById(thisId).style.display=="none")
{
document.getElementById(thisId).style.display="";
document.getElementById(ImageId).src = "/_layouts/images/minus.gif";
}
else
{
document.getElementById(thisId).style.display="none";
document.getElementById(ImageId).src = "/_layouts/images/plus.gif";
}
}
function ExpandCollapseBody()
{
var i = 1;
var WPid = "WebPartWPQ1" ;
var WPtitleid = "WebPartTitleWPQ1" ;
var Toggleid = "ToggleImage1" ;
do
{
try
{
document.getElementById(WPtitleid).innerHTML = '<IMG id="' + Toggleid + '" onClick="WPToggle(\'' + WPid + '\',\'' + Toggleid + '\')" alt="Expand/Collapse" style="margin:6px 5px 0px 2px; float:left; cursor:pointer;" src="/_layouts/images/minus.gif" />' + document.getElementById(WPtitleid).innerHTML ;
if (document.getElementById(WPid).style.display=="none")
{
document.getElementById(Toggleid).src = "/_layouts/images/plus.gif";
}
}
catch(err) {}
i = i + 1;
WPid = "WebPartWPQ" + i ;
WPtitleid = "WebPartTitleWPQ" + i;
Toggleid = "ToggleImage" + i;
} while (document.getElementById(WPid))
}
_spBodyOnLoadFunctionNames.push("ExpandCollapseBody()");
</script>
How about the default minimize/restore option?
Each Web Part comes with a customization menu in its top-right corner, including a minimize/restore option. The expand/collapse buttons have a different role:
- they can be used by readers and contributors, while the minimize/restore option is for Web designers.
- the changes are not saved; the next time the page is uploaded, the Web Parts are back to their initial state.

59 comments
Comments feed for this article
October 28, 2008 at 10:46 am
Greg O
Wow! This is really useful. Another great post!
I’m afraid I don’t know javascript – would it be possible to have the same script but one that would collapse the webparts by default (and obviously present the plus element so users can expand the webparts they want)?
thanks,
Greg
October 28, 2008 at 12:10 pm
Robin
Once again Christophe to the rescue! Great Post!
Greg O, All you have to do is minimize all the web parts on your page while in edit mode. This will produce the result you want.
October 28, 2008 at 1:55 pm
Christophe
Thanks
As for your question, Greg, find these lines on the script (~line 35):
if (document.getElementById(WPid).style.display==”none”)
{
document.getElementById(Toggleid).src = “/_layouts/images/plus.gif”;
}
and replace them with:
document.getElementById(WPid).style.display=”none”;
document.getElementById(Toggleid).src = “/_layouts/images/plus.gif”;
But I agree with Robin: you can just keep the script as is and use the minimize/restore option (and do you really want to collapse all the Web Parts?)
October 28, 2008 at 3:52 pm
Greg O
Thanks for the amended code and the minimize suggestion. Sometimes the blindingly obvious solution escape your attention.
Thanks again,
Greg
November 1, 2008 at 2:38 am
Links for October 31, 2008 « Steve Mullen’s Blog
[...] from Christophe Expand/collapse buttons for your Web Parts Possibly related posts: (automatically generated)#270: Presto#155: Remembrance#154: AndanteGuest [...]
November 7, 2008 at 7:19 pm
Chad
Your ideas are great….keep up the great work
November 18, 2008 at 11:30 pm
Eli
Great post Christophe, I always find your articles useful, I was wondering though if you have ever played with expnding and collapsing discussion board list. back in WSS 2.0 by default in threaded view you had the plus minus and could expand and collapse each message. But for wss 3.0 they removed that and now you have to show all replies. after much searching i found nothing but i wondered if you know of a way to apply this article to the dicussion board page? any insight would be very appreciated. thanks again.
November 18, 2008 at 11:32 pm
Ben Bradley
Hi Christophe,
I found your solution here after getting about 75% of my own solution put together. Some of the techniques you used were extremely useful and helped me get past the remaining 25% much quicker.
You can see it here: http://moblog-banditben.blogspot.com/2008/11/expand-or-collapse-web-parts.html
November 19, 2008 at 12:32 am
Christophe
The main difference between the two scripts is that mine checks the default status of each Web Part (expanded or collapsed), while Ben’s collapses all Web Parts. Just a matter of taste…
January 27, 2009 at 2:12 pm
Anonymous
Is there a way to expand / collapse content within a wiki page? If so, how hard would it be to update the content if you implemented the code?
January 27, 2009 at 2:31 pm
Christophe
You mean within the text editor? I don’t see a way to do this. Outside the text editor: you can edit the wiki page and add Web Parts, like for any other Web Part page.
February 10, 2009 at 1:47 am
Anonymous
Yep – within the text editor. They have alot of information which they wish to present within each wiki and were looking for a way to expand / collapse content being displayed within the body of the wiki. Thanks anyway!
PS: I saw that there were a few Table of Contents add-ons for wikis. Do you know of anyone such add-ons which are pretty stable?
February 10, 2009 at 2:43 am
Christophe
Sorry, I haven’t tested any of these add-ons…
February 15, 2009 at 10:57 am
A Content Editor Web Part for every home! « Path to SharePoint
[...] This demo file includes 3 scripts: the cross-site menu, “Breaking News”, and the expand/collapse buttons. It interacts with: – the top level home page – the sub-sites (click on the tabs) – a site located [...]
February 23, 2009 at 2:15 am
Mark Miller, EndUserSharePoint
I have used Scriptaculous to do this and it works like a charm.
I created an FAQ page that had the answer embedded beneath each question. When the user initially comes to the page, they see nothing but questions. When they click on a question, the rest of the page slides down enough to display the answer to the question.
Probably easy to do with jQuery, I just happened to have the Scriptaculous script in front of me at the time.
Hope that helps.
Mark
February 23, 2009 at 2:42 am
Christophe
Thanks for the comment Mark.
As you mention, the code could easily be rewritten with jQuery.
However, for an application like the one I propose, plain JavaScript seems more efficient.
I think jQuery will be interesting as a generic solution in the next version of SharePoint, where the jQuery library will be included in the page load. Today, it is still an additional script call (a light one I agree), and only justified for specific purposes like animations or effects, or if you heavily rely on it in your page.
March 4, 2009 at 12:31 am
mike g
hi, thanks for the great idea. I would like to exclude one web part from being expanded/collapsed by default. How would I exclude one web part?
March 4, 2009 at 2:44 am
Christophe
Mike, what do you mean by this? Do you mean the initial state when the page loads? Or do you mean that for a specific Web Part you don’t wan’t to give the users the possibility to collapse it?
March 4, 2009 at 3:19 am
Christophe
@ Mark: to confirm my initial reply, today I came across this comment in a jQuery blog:
For jQuery fans, the post I quoted is worth a look:
http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/
March 4, 2009 at 2:48 pm
mike g
Christophe,
For a specific web part, by default on page load, it should not use the expand/collapse script.
I use your script, on page load, to minimize all web parts. (the third comment above) One web part however should not by default be minimized.
Hope I am more clearer now.
Thanks Christophe
March 6, 2009 at 3:10 am
Stu S
I am exteremly new at using SPD and customizing SP07. How can this be adapted to a FAQ creation? I used Rob Wilson’s method of 2 lists for a FAQ but the Grouping in SPD does not give me the option of a second level. Ideally I would like to Expand and collapse Starting at the Category, and then reveal the answer to the right of the Question. Here is the basic layout idea. I have seen very little on Creating FAQ’s in sharepoint.
Collapseable Category name (from article, using grouping)
Question
Answer (Revaled when question is clicked)
OR
Question Answer (Revaled when question is clicked)
Any help would be greatly appreciated.
Stu
March 6, 2009 at 3:12 am
Stu S
Sorry, forgot to post the link to the Method used for the Grouping.
http://sharepointblog.kellerschroeder.com/2009/01/creating-data-forms-in-sharepoint.html
April 8, 2009 at 1:23 am
Christophe
Follow-up on Mark Miller’s comment – I looked into jQuery to achieve the same effect, but it didn’t give a satisfying result. In particular, it turns out that the jQuery “toggle” function is buggy, there is a flickering effect in IE7.
April 16, 2009 at 9:12 pm
Zac
I cannot not get this to work with a Page Viewer Web part. Any Suggestions?
April 17, 2009 at 12:32 am
Christophe
Zac: the only requirement is that you display the Web Part title. Then the script will add a plus or minus sign next to it.
It should work for any OOTB Web Part.
April 20, 2009 at 1:14 pm
Zac
I have the web part title shown. The problem is that when chrome state is set to minimize the web part opens and it is a blank space. If the chrome state is normal then the web part appears and you can toggle open and close fine. However, I would like to have all of the web parts minimized on the page when it opens.
April 22, 2009 at 12:49 pm
myro
hello sir,
based on your post, i have created a web part that let’s you handle this trick:
http://www.myrocode.com/post/2009/04/22/Collapse-or-Expand-SharePoint-web-parts-in-your-pages-get-OpenCloseWps.aspx
I hope it can save time to people who need’s this feature
thank you.
April 28, 2009 at 12:34 am
cloudno9
Great, thanks for sharing this.
Here is an idea or suggestion, can i use this feature and add to quick launch bar on the left? and if you dont mind tell me how that would be great.
TIA
April 28, 2009 at 9:49 am
myro
why would you use the quick launch? i can’t understand why would put the js there.
if you need it on every page you should place it in the master page.
April 30, 2009 at 6:04 am
Christophe
cloudno9, see if this is what you’re looking for:
http://pathtosharepoint.wordpress.com/2008/09/23/stylescss-and-the-display-property/
April 30, 2009 at 2:32 pm
cloudno9
Hi Chris,
yes something similar. instead of expand/collapse the complete quicklaunch bar, if i can have the same button next to each quick launch menu item, such as (from your link above) if i can have expand/collapse sign next to “Pictures”, “Documents”… this way I can expand/collapse whats inside (submenu) the Documents.
I hope I am able to express what i was referring to
TIA
June 18, 2009 at 9:07 am
PetrK
Hi Christophe,
this yours functionality is perfect. It’s exactly what I need.
But I have one small problem. Is possible to change your code for following:
All WebParts are minimalized after first page load(default behaviour-it’s OK). But after expansion requested document library and sort documents by some column do not minimalize requested web part(leave webpart state – expanded).
For now is necessary to click again to expand button.
Please give me some advice. I’m not javascript programator.
thanks
Petr
June 19, 2009 at 7:45 am
PetrK
Hi, I do my question simple:
Is possible to change the code agains default behaviour to:
- the changes ARE saved; the next time the page is refreshed(by column sorting), the Web Parts REMEMBER to their initial(previous) state.
?
thanks
Petr
June 19, 2009 at 8:15 am
Christophe
PetrK: you would need to add a cookie to save the states. Not something easy if you are no JavaScript programmer. It could also become tricky if the site owner later makes changes to the page.
July 6, 2009 at 9:53 pm
Adam
Hello,
Great Post. I am no Javascript expert. Is there a change I could make to the code to apply this to a specific Web Part (and not all web parts?)
Thanks,
Adam
July 7, 2009 at 8:45 am
myro
sure… use my web part that is based on this javascript
http://www.myrocode.com/post/2009/04/22/Collapse-or-Expand-SharePoint-web-parts-in-your-pages-get-OpenCloseWps.aspx
July 24, 2009 at 5:43 pm
DonM
Great web part any idea how to make it work with some NOT OOTB web parts such as this one. http://www.amrein.com/apps/page.asp?Q=5740
July 29, 2009 at 8:40 am
PetrK
Hi, has anybody discovered solution of Zac problem(April 20, 2009 at 1:14 pm)?
The problem is that when chrome state is set to minimize the web part opens and it is a blank space.
I have same behaviour and I don’t know if it’s possible to solve it?
thanks
Petr
July 29, 2009 at 8:50 am
myro
my web part should do that job. did you tried? the link is located @myro
- July 7, 2009 at 8:45 am
August 3, 2009 at 8:14 am
PetrK
Hi myro,
I tried your solution but no success. I did installation of your webpart and add this webpart to page. I wrote name of my pageviewer webpart(PVWP) into Settings and clicked on OK. Sign [-] was appeared for my PVWP. (OK)
I changed chrome style for this PVWP from Normal to Minimalized. Next I clicked on OK and sign [+] was appeared for my PVWP. (OK)
After refresh of page I clicked on [+] sign and the PVWP was maximalized but no content of requested page was appeared. Only empty space. (PROBLEM)
Any help?
thanks
Petr
August 4, 2009 at 1:02 pm
myro
hello Petrk
right now, i’m writing you from an internet caffe, because i’m on holiday in croatia.
once i’ll get back in milan, i’ll try the wp on my pc with moss and write you back to help you out.
August 6, 2009 at 11:42 am
PetrK
Hi Myro,
thanks a lot for help. I will wait for your’s answer.
Petr
August 18, 2009 at 9:38 am
Nerdace
Excellent work. I don’t have time to sit down plan and write code and this has saved me alot of hassle. Much appreciated
September 3, 2009 at 1:52 pm
PetrK
Hi, has anybody same problem as me? (August 3, 2009 at 8:14 am PetrK) Do you have any solution?
Petr
October 6, 2009 at 8:23 pm
T.C.
hi,
this is a great trick! I was searching for a solution like this…. one question (that was also asked previously by ‘mike g’ on March 4, 2009 at 2:48 pm…. is there a way to apply the expand/collapse to specific list/webpart and not all on the page?
Thanks,
T.C.
October 7, 2009 at 1:52 am
Christophe
Well, yes, but you’d need to find a way to identify these Web Parts, and add the filter to the loop. A bulk customization of all Web Parts is far easier.
October 9, 2009 at 9:03 pm
T.C.
yeah, was thinking aong that line. thanks for the code!
October 19, 2009 at 12:07 pm
Haider
Well done, yet another charming article from Chris, it works perfectly however I’m having some issues: 1- the plus/minus sign appears above the wp title || 2- custom wps are not getting effected neither my in house made wp nor 3rd party wp.
Waiting for ur reply
Thanks
October 21, 2009 at 11:16 am
Christophe
1- this will depend on how much room you have for your WP title
2- the script works for Web Parts that follow the standard SharePoint structure.
November 5, 2009 at 1:19 pm
Paul
Hi – I’m having a problem with this code. It works fine on a test publishing page I set up and when I add it to the “live” default.aspx page it looks fine in the editing view – the [+] and [-] are all there. However, as soon as I publish the “live” page the [+] and [-] disappear. Any suggestions? I should mention that this live page has a lot of webparts on it.
November 6, 2009 at 2:41 pm
Christophe
Paul, could it be that your Web Part titles are hidden in view mode?
July 23, 2010 at 11:59 pm
Jenn
I’m having this same problem. Everything is fine when I’m logged in to my account, but when I log in with a contributor account you can’t see the + – buttons. Could it be an issue of permissions? I’m fairly certain the web part titles are not hidden.
November 5, 2009 at 2:45 pm
Sean Sharepoint
Thanks alot for the script, it is exactly what I was looking for, Good work! Question though, it is only working for about 6 web parts and I needed it for all web parts on the page, about 15. Any idea why its not working?
November 6, 2009 at 2:39 pm
Christophe
Maybe because the title is hidden for the other 9?
January 5, 2010 at 5:42 pm
Samual
Hi there, thanks for this great script. It’s perfect for the job in hand.
One quirk has popped up that I hope someone will be able to help me with.
I am using the collapse/expand to hide Excel Web Access webparts. The script has done the job but has also hidden the spreadsheet scroll bars. Now when the spreadsheet is revealed the user can only see as much as their monitor size allows!
Any help/solution would be greatly appreciated!
January 6, 2010 at 9:44 am
Samual
A little update, it seems that if I have the spreadsheet webpart as minimized the scroll bars do not load. If it’s set to normal they do…
January 18, 2010 at 4:50 pm
Lauren
This script is awesome (and easy to use!). Been trying to figure this out for a while. Thanks very much!
March 10, 2010 at 4:36 pm
C
Is it possible to set it so that if the user clicks the title of the webpart it expands/collapses the WP, instead of using the +/- buttons?
March 11, 2010 at 9:27 am
Christophe
Sure, but I didn’t do it because in SharePoint clicking on the title has another purpose.