For example i use this code:
PublishingPage publicProfilePage = sps.AddPublishingPage("Public.aspx", layout);
var wpm = publicProfilePage.ListItem.File.GetLimitedWebPartManager(PersonalizationScope.Shared);
WebPartsHelper.AddWebPart(wpm, "ProfileWebPart", "MainLeftColumn", 0);
and the static method AddWebPart:
public static void AddWebPart(SPLimitedWebPartManager wpManager, WebPartParam webPart, string zoneId, int zoneIndex)
{
XDocument webPartXmlSettings = GetWebPartFile(wpManager, webPart.WebPartName);
if (webPartXmlSettings != null)
{
try
{
XmlReader xtr = webPartXmlSettings.CreateReader();
var errorMsg = string.Empty;
System.Web.UI.WebControls.WebParts.WebPart newWebPart = wpManager.ImportWebPart(xtr, out errorMsg);
// Web part values initialization.
object values = webPart.WebPartValues;
if (values != null)
{
Type webPartType = newWebPart.GetType();
foreach (PropertyInfo p in values.GetType().GetProperties())
{
PropertyInfo wpProperty = webPartType.GetProperty(p.Name);
if (wpProperty != null)
{
wpProperty.SetValue(newWebPart, p.GetValue(values, null), null);
}
}
}
newWebPart.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
wpManager.AddWebPart(newWebPart, zoneId, zoneIndex);
}
catch (Exception ex)
{
SPDiagnosticsService.Local.WriteTrace(0,
new SPDiagnosticsCategory("WebPartsHelper.AddWebPart() Error", TraceSeverity.Medium, EventSeverity.Error),
TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
}
}
}