3

How can I set a web part so it will be automatically appended to a web part zone when a specific page layout is selected?

The solutions I have tried so far is:

  1. Using <ZoneTemplate> under <WebPartZone> and putting my webpart in there
  2. Adding .webpart-code directly into <AllUsersWebPart> where I define the page layout, in elements.xml
  3. Put the web part on the page layouts programmatic when creating site structure (deployment script)
  4. Add a hook that hooks in and adds the web part

Problems with the solutions:

  1. The web part seems to become static, and can not be removed after it's created.
  2. would have to put .webpart-code in both a .webpart-file as well as in elements.xml (I want it to be stand alone as well as set by default). And making a mess in my elements.xml.
  3. Web part will only be deployed first time, so when creating a new page with the page layout, the web part will not be included.
  4. We do not know how to do this, since page layout is set after creating the page. We do have hooks for setting default page layout based on where in the structure the page is created, here we can append the web part as well, but then the web part will only be added if you create a page on a specific location.
1
  • Question to solution 3: Do you mean the page layouts or the page instances? I would believe that adding the web parts by code to the page layouts should behave exactly like solution 1 or 2. Jan 24, 2012 at 14:55

3 Answers 3

1

Since you are using SP2010 you should be able to use the WebProvisioned event to hook in extra functionality such as the one you ask.

In SP2007 I would use any of the above methods you mention depending on what i needed. The last one I would achieve by using a custom provisioning provider. Check out my other post on this here.

2
  • 1
    Hello. Do you have an example of how WebProvisioned works when adding a new Page to an existing Site?
    – eirikb
    Jul 19, 2010 at 14:00
  • 1
    for adding a webpart to a page when you create a page, you should look at the ItemCreated event on the "Pages" library (if using a publishing site).
    – Mauro
    Sep 10, 2010 at 13:35
0

I have been using your method 1. and 2. extensively. I cannot follow the problem you describe for solution 1. Apart from that have both solutions (1. and 2.) the drawback that all web parts are duplicated each time the feature deploying the pagelayouts is reactivated and you need some clean-up code to fix this.

0
0

I've been using number 1 way (on the original post) and it works perfectly. Web parts don't become static unless you have defined web part properties to disallow moving or closing web part, or if you don't have enough permissions.

4
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. Oct 16, 2012 at 17:44
  • 1
    Yes it did as 1. is the answer.
    – Jussi Palo
    Oct 16, 2012 at 22:17
  • 1
    As we've recently discovered, using this method causes a potential issue if the page layout ever becomes unghosted. The web parts in the page layout become permanently embedded within the AllWebparts table and cannot be removed by any Microsoft supported means. The result is that the web parts are injected into any page that you assign the layout to, even if you've edited the page layout itself and have removed the web parts from it, and have reset the page layout to the site definition.
    – SF1Dev
    Jan 1, 2013 at 20:27
  • 1
    We've run into the same issue that Bruce describes. We accidentally auto-customized pages by including some SP Designer attributes in the page declaration, and after removing that and redeploying the same page layouts we now have double web parts, one set un-editable. Some correlation with the web part maintenance view of the page layout file, but not all the duplicates are there. Removing web parts from the page layout in that view makes no difference, however. I think the only fix is to completely nuke the page layout then redeploying the page layout with a different name. Sep 6, 2014 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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