In traditional SharePoint implementation, we usually create a SharePoint page, add webpartzone, and then add webpart on it. However, in some cases, we also need to use code, add a webpart to the corresponding webpartzone. The following describes how to perform this operation.
The operation is actually very simple. The general principle is: Use the object model to find the page for adding webpart, find webpart, specify the Id of webpartzone, configure WebPart, and update the page. Here, I simply paste the code and.
1. I create a new console program, so I will not overwrite it; add two methods in it;
Method 1:Public static void addWebpart (string SiteUrl, string PageUrl)
Overview: Based on the page address and site address, find the page for adding webpart and add the webpart;
Method 2:Public static System. Web. UI. WebControls. WebParts. WebPart CreateWebPart (SPWeb, SPLimitedWebPartManager webPartManager)
Introduction: Go to the webpart library and find our webpart;
2. Simple cut. Let's take a look at the referenced header file;
3. source code of the two methods:
1 public static void addWebpart(string SiteUrl, string PageUrl) 2 { 3 SPSite site = new SPSite(SiteUrl); 4 SPWeb web = site.OpenWeb(); 5 SPFile thePage = web.GetFile(PageUrl); 6 SPLimitedWebPartManager theWebPartManager = thePage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); 7 System.Web.UI.WebControls.WebParts.WebPart webPart = CreateWebPart(web, theWebPartManager); 8 theWebPartManager.AddWebPart(webPart, "Left", 1); 9 return;10 }
4. The source code of method 2 is as follows:
1 public static System. web. UI. webControls. webParts. webPart CreateWebPart (SPWeb, SPLimitedWebPartManager webPartManager) 2 {3 string webPartName = "MSContentEditor. dwp "; 4 SPQuery qry = new SPQuery (); 5 qry. query = String. format (CultureInfo. currentCulture ,"
{0}
", WebPartName); 6 SPList webPartGallery = null; 7 if (null = web. parentWeb) 8 {9 webPartGallery = web. getCatalog (SPListTemplateType. webPartCatalog); 10} 11 else12 {13 webPartGallery = web. site. rootWeb. getCatalog (SPListTemplateType. webPartCatalog); 14} 15 SPListItemCollection webParts = webPartGallery. getItems (qry); 16 XmlReader xmlReader = new XmlTextReader (webParts [0]. file. openBinaryStream (); 17 s Tring errorMsg; 18 ContentEditorWebPart webPart = (ContentEditorWebPart) webPartManager. ImportWebPart (xmlReader, out errorMsg); 19 string Mycontent = "here is the content added by the Code... "; 20 XmlDocument xmlDoc = new XmlDocument (); 21 XmlElement xmlElement = xmlDoc. createElement ("MyElement"); 22 xmlElement. innerText = Mycontent; 23 webPart. content = xmlElement; 24 25 return webPart; 26}
For more information about the source code, refer to method 1 theWebPartManager. the second parameter of AddWebPart (webPart, "Left", 1) is the Id of the webpartzone you want to insert. I am the Left column of the two columns, so it is Left;
In the second method, the returned WebPart may be System. web. UI. webControls. webParts. some parameters of the WebPart type will not exist, because I tested the Content Editor here, so it is converted to the ContentEditorWebPart type, otherwise there will be no ContentEditorWebPart Content attribute;
The above are some of the problems encountered during your use;
5. Perform the effects before and after the console, such:
(Page before execution)
(Page after execution)
6. On the editing page, check the area to which the Content Editor belongs. It is indeed the left column. The content editor is correctly added!
The above is all the content to be introduced in this article. The length is short and a little large. Haha, I hope to help anyone in need. Thank you!