Create a webpart page with an webpart with in it programmatically

前端 未结 2 775
-上瘾入骨i
-上瘾入骨i 2021-01-22 01:34

I want to create several webpart pages (programmatically) with custom web parts in it. I have searched the internetz but couldn\'t find anything that I could get to work.

<
2条回答
  •  情话喂你
    2021-01-22 02:30

    You can add webparts by using the SPLimitedWebPartManager and your webPart defininition URL. This example should get you started:

    XmlTextReader reader = new XmlTextReader(new StringReader(web.GetFileAsString()));
    
    SPLimitedWebPartManager wpm = web.GetLimitedWebPartManager(, Syste.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
    
    WebPart wp = (WebPart) wpm.ImportWebPart(reader, out errMsg);
    wp.Title = "My Title for this webpart";
    
    wpm.AddWebPart(wp, , );
    wpm.SaveChanges(wp);
    

    After you fill in the blanks this code will put a WebPart on your publishing page. In the end the most important functions are SPLimitedWebPartManager.ImportWebPart and SPLimitedWebPartManager.AddWebPart as you instantiate the WebPart manager for the publishing page.

提交回复
热议问题