Create a webpart page with an webpart with in it programmatically

这一生的挚爱 提交于 2019-12-02 01:41:02

问题


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.

Here is my code so far (where I create a welcome page, not a webpart page):

using (SPSite site = new SPSite("http://v99-sp-public/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    PublishingSite pSite = new PublishingSite(site);
                    SPContentType ctype = pSite.ContentTypes["Welcome Page"];
                    PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
                    PageLayout pageLayout = pageLayouts["/_catalogs/masterpage/welcomesplash.aspx"];
                    PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
                    PublishingPageCollection pPages = pWeb.GetPublishingPages();
                    PublishingPage pPage = pPages.Add("Klanten2.aspx", pageLayout);
                    SPListItem newpage = pPage.ListItem;
                    newpage["Title"] = "Klanten";

                    newpage.Update();

                    newpage.File.CheckIn("Checkin");
                    newpage.File.Publish("Publisch");
                 }
              }

Please help me out,

Thanks.


回答1:


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(<Url to your .webpart file here>)));

SPLimitedWebPartManager wpm = web.GetLimitedWebPartManager(<URL to your page>, 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, <Name of WebpartZone here, e.g. "Header">, <Zone Index here>);
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.




回答2:


OK, did a little digging... here is my solution.

using(SPSite site = new SPSite("http://my.dev.com"))
{
    using(SPWeb web = site.OpenWeb())
    {
        SPFile page = web.GetFile("SitePages/Welcome.aspx");
        using(SPLimitedWebPartManager manager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
        {
            string errMsg = string.Empty;
            SPFile myWebPart = web.GetFile("_catalogs/wp/myWebPart.webpart");
            XmlTextReader read = newXmlReader(myWebPart.OpenBinaryStream());
            var wp = manager.ImportWebPart(read, out errMsg);
            manager.AddWebPart(wp, "<Webpart Zone>", 1);
            manager.SaveChanges(wp);
        }  
    }
}


来源:https://stackoverflow.com/questions/8428466/create-a-webpart-page-with-an-webpart-with-in-it-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!