Ok, consider we have this environment:
- We have farm-level WSP file (this also could be sandboxed in this situation, but for clarity, let's consider it is a farm one).
- This WSP contains some lists and libraries definitions/instances, and also some visual webparts.
- Also, WSP file contains at least one feature in it.
- This WSP is added into your farm, deployed to specified site collection and feature is activated there. So, lists and libraries are created and webparts are added into the gallery.
- After all this done, we want to instantiate a webpart 'Webpart1' at 'default.aspx' site page.
From this point, we can add some code to the feature receiver (into FeatureActivated), to accomplish our goal. This code will look smth like this:
// prepare the webpart
var webPart = new WebPart1();
// ...here you can initialize some webpart properties, if needed
// fetch SPFile object for default.aspx page
SPFile page = web.RootFolder.Files["default.aspx"];
// ...here you probably need to check out the page if it is a publishing site
// get LimitedWebPartManager object for the page
var webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
// add our webpart into the 'Main' webpart zone, using zone index = 1
webPartManager.AddWebPart(webpart, "Main", 1);
// ...do not forget to commit your changes here if you are using publishing site
// save our changes
- How to: Work with Web Parts on a Page