Add Web Part to Page Programmatically

4 Jan

I use the following code to programmatically add web parts to my pages.

I frequently see similar code, but no one is ever using the xml from the web part library to do the import, and as such the properties are not set to what was exported/defined in the web part xml.


XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream());

WebPart webPart = manager.ImportWebPart(xmlReader, out errorMessage);




One Reply to “Add Web Part to Page Programmatically”

Leave a Reply

Your email address will not be published. Required fields are marked *