Currently I have a sharepoint solution containing custom webparts for a sharepoint list's New/Edit/Disp forms. As of now, I point the list at these webparts by manually navigating to these pages and inserting them via the gui. My question now is, how do I automate this process via C#? Is it possible to replace the default list item forms with my own custom webparts? My attempt is below, per this link.
I have created an event receiver to handle this
Code:
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
using (SPSite sp_Site = new SPSite("http://root/site/Lists"))
{
string err = string.Empty;
using (SPWeb sp_Web = sp_Site.OpenWeb())
{
XmlTextReader reader = new XmlTextReader(new StringReader(sp_Web.GetFileAsString("http://root/_catalogs/wp/myWebPart.webpart")));
SPLimitedWebPartManager wpm = sp_Web.GetLimitedWebPartManager("http://root/site/Lists/MyList/DispForm.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
WebPart wp = (WebPart)wpm.ImportWebPart(reader, out err);
wpm.AddWebPart(wp, "Main", 0);
wpm.SaveChanges(wp);
}
}
I can't currently debug this because the error Error occurred in deployment step 'Add Solution': <nativehr>0x80070002</nativehr><nativestack></nativestack>
Am I doing this correctly? Is there another way? Any input is much appreciated.