It seems the standard method for adding a SharePoint 2007 Web Part to a page layout is by using the <AllUsersWebPart> tag in the <Module> tag of the page layout feature. However when I tried this method, I would receive an "No parameterless constructor defined for this object" exception that I could not resolve. So I came up with a different way of doing it that I actually like better. I can go directly to the page layout aspx file and add the Web Part there.
First, I need to register the Web Part assembly just as if I were adding a custom control to a standard asp.net page.
<%@ Register tagprefix="MyTag" namespace="My.NameSpace" assembly="My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f300fd2d8ae223d7" %>
Then, I can declare the Web Part control in the Web Part Zone of my choosing.
<WebPartPages:WebPartZone PartChromeType="None" ID="Zone1" runat="server" Title="Zone1">
<ZoneTemplate>
<MyTag:MyWebPart runat="server" PartOrder="1" ></MyTag:MyWebPart>
</ZoneTemplate>
</WebPartPages:WebPartZone>
What's nice about this approach is that if I want to remove this Web part or add another, I can simple copy the aspx file to the proper 12 hive location and its ready to go. Instead of having to uninstall and re-install the feature.
3 comments:
Is it possible to EDIT the webpart, if you follow this approach?.. I implemented this logic to add a custom webpart to a custom aspx page (that resides in '_layouts' folder). The webpart displays, but I am unable to edit it.
Hey Anon,
I am able to edit the webpart. I can even delete it. However, I am using a page layout. (Ironically named since they do NOT reside in the _layout folder.) I do not think application pages (custom aspx) are editable in that manner. This would be due to the fact they they exist as only one file and not as multiple instances of a file like the page layouts.
That functions only if you add only one page layout because the webpart needs a unique id.
Post a Comment