Sharepoint 2010 using programs to add WebPart to a page

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.