0
votes

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.

1

1 Answers

0
votes

I believe you are not overriding the correct event in this case.

Try to override FeatureActivated instead, as the web parts only become available once the feature has been activated.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    // do stuff  
}

If you still experience the error you mentioned while deploying your solution, try the following:

  • close visual studio
  • restart the "Sharepoint 2010 User Code Host" service
  • restart the "Windows Management Instrumentation" service
  • open visual studio and try to deploy your solution again