If you are using automated deployments, you will not need to manually add webpart to the page, but you can complete the operation with the program.
provides two methods for adding a WebPart to the page, where two of the areas to be noted are WebPart areas, which must be the ID of the zone
Zoneindex is the order in which this webpart is placed, from top to bottom 1,2,3,4,5,6.
1: #region Add webpart to page
2:private string Addwebparttopage (SPWeb Web, String pageurl, String webpartname, string zoneid, int zoneindex)
3: {
4:using (SPLimitedWebPartManager webpartmanager = web. Getlimitedwebpartmanager (
5:pageurl, personalizationscope.shared))
6: {
7:using (System.Web.UI.WebControls.WebParts.WebPart WebPart = CreateWebPart (Web, Webpartname, Webpartman Ager))
8: {
9:splimitedwebpartcollection webparts = Webpartmanager.webparts;
10:webpartmanager.addwebpart (WebPart, ZoneID, Zoneindex);
11:return webpart.id;
12:}
13:}
14:
15:}
16:
17:private System.Web.UI.WebControls.WebParts.WebPart CreateWebPart (SPWeb Web, string webpartname, SPLIMITEDWEBP Artmanager WebPartManager)
18: {
19:spquery qry = new SPQuery ();
20:qry. Query = String.Format (CultureInfo.CurrentCulture, "<where><eq><fieldref name=\ FileLeafRef\"/> <value type=\ "file\" >{0}</Value></Eq></Where> ", webpartname);
21st:
22:splist webpartgallery = null;
23:
24:if (Null = = web. Parentweb)
25: {
26:webpartgallery = web. GetCatalog (
27:splisttemplatetype.webpartcatalog);
28:}
29:else
30: {
31:webpartgallery = web. Site.RootWeb.GetCatalog (
32:splisttemplatetype.webpartcatalog);
33:}
34:
35:splistitemcollection webparts = Webpartgallery.getitems (qry);
56|
37:xmlreader XmlReader = new XmlTextReader (webparts[0). File.openbinarystream ());
38:string errormsg;
39:system.web.ui.webcontrols.webparts.webpart WebPart = Webpartmanager.importwebpart (XmlReader, out errormsg );
40:
41:return WebPart;
42:}
A: #endregion
How to use
1:addwebparttopage (Oweb, Olist.defaultviewurl, "Webpartname.webpart", "Main", 1);