Wednesday, January 30, 2008

Controlling the "add web part" menu

Hi, Recently I had a customer that asked me to control what web parts users can add to a web part zone.
First, I thought of setting up a “suggested web parts” group for that zone, and go to web part gallery and associate the web parts he wanted to this group.
To make your web part zone focus on a specific web part group, open the page in SharePoint Designer and set the zone's property “QuickAdd-GroupNames” to: "MyGroup"
(should turn out like: <WebPartPages:WebPartZone runat="server".... QuickAdd-GroupNames="MyGroup"..... >)
Also - to hide current site lists from the popup window, in the same manner set QuickAdd-ShowListsAndLibraries="false".
Although this makes it easier to add these web parts (not having to search the list for them) the customer insisted on have 2-3 web parts "as one click to add each".
What he meant was, to have along the "add web parts" menu something like: Add CalendarPlus Add Picture Library Viewer....
Well, I found out this can be done by some javascript manupulation.
We have to follow these steps:
1 - Get the uniqueId of the web part zone.
2 - Get the add web part popup result for adding the web part you want.
3 - Call __doPostBack(uniqueId,result);
4 - Done!
Wait... I know... the web part zone id is pretty easy to figure out... but...
How can you tell the dialog result?!?
Well, I have a trick that will allow you to create a popup with that information...
1 - Open the page in sharepoint designer
2 - Add this script block at the end of the file (before the body tag ends)
function __doPostBack2(eventTarget, eventArgument)
alert("zone id = "+eventTarget + "\ndialog result = " + eventArgument);
__original(eventTarget, eventArgument);
var __original = __doPostBack;
__doPostBack = __doPostBack2;
Now, we found out all the information we needed...
All we have left to do is to add above every web part zone a new "add WP" button using the zone's ID and the web part's id you found out earlier.

The HTML would look something like this:
<input onclick="__doPostBack(this.uniqueId,this.wpid);" type="button" value="Add Calendar" uniqueid="ctl00$PlaceHolderMain$Left$ctl00" wpid="lvwpguid:06c0b9b5%252D3a45%252D404a%252Db834%252D0677341aad5c;%25D7%259C%25D7%2595%25D7%2597%2520%25D7%25A9%25D7%25A0%25D7%2594">

Well, I was amazed it actually works, but it does!

Hope it’s not too complicated, it’s been a challenge trying to put it into words…

1 comment:

Paisley said...

I tried adding this property to my webpartzone in spd - but it didn't show in the intellisense choices and gave me a red squiggly when I added it manually - is there something I need to refernce in order to see it? - a feature to be activated? - I am trrying to use this on a publishing site...