Can't add web parts in ItemAdded event RRS feed

  • Question

  • I'm trying to build an event receiver that can add a web part to a page in the Pages library.  I have some code that does this but it only can add the web part in the ItemUpdating event.  The web part does not get added if I execute this code in the ItemAdded event.

          if (properties.ListItem != null)
            SPFile thisFile = properties.ListItem.Web.GetFile(properties.ListItem.File.Url);
            if (thisFile != null)
              SPLimitedWebPartManager webPartManager = thisFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
              ContentEditorWebPart defaultCEWPWebPart = new ContentEditorWebPart();
              defaultCEWPWebPart.Title = "Default Web Part";
              defaultCEWPWebPart.ChromeType = PartChromeType.TitleAndBorder;
              XmlDocument xmlDoc = new XmlDocument();
              XmlElement xmlElement = xmlDoc.CreateElement("p");
              xmlElement.InnerText = "Hi!";
              defaultCEWPWebPart.Content = xmlElement;
              /**web part settings ommited here**/
              webPartManager.AddWebPart(defaultCEWPWebPart, "Header", 1);

    Is there something special I need to do for this to work in ItemAdded?  This is on a publishing site.  Is there something special about the publishing site?

    Ray Riopel
    Monday, August 1, 2011 10:12 PM


All replies