Import Web part with client object model
Function call
ImportWebPartToPage(webUrl, "/SitePages/Home.aspx", @"C:\Users\Administrator\Downloads\CEWP.dwp");
public static void ImportWebPartToPage(string webUrl, string pageUrl, string webpartFilePath)
{
ClientContext context = new ClientContext(webUrl);
Web web = context.Web;
context.Load(web, w => w.ServerRelativeUrl);
context.ExecuteQuery();
string pageRelativeUrl = web.ServerRelativeUrl + pageUrl;
File page = context.Web.GetFileByServerRelativeUrl(pageRelativeUrl);
LimitedWebPartManager manager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
XmlTextReader xtr = new XmlTextReader(webpartFilePath);
StringBuilder sb = new StringBuilder();
while (xtr.Read())
{
var tmpObj = sb.AppendLine(xtr.ReadOuterXml());
}
string newXml = sb.ToString();
if (xtr != null)
{
xtr.Close();
}
WebPartDefinition webpart = manager.ImportWebPart(newXml);
manager.AddWebPart(webpart.WebPart, "Left", 1);
context.ExecuteQuery();
}