Sunday, November 30, 2008

Deploying web parts to customized site pages.

There are two ways to deploy web part pages to a site. one is using the elements.xml to define what is inside the web part zone, like the following.

<File Url="WebPartPage.aspx" Name="WebPartPage03.aspx" Type="Ghostable" > <!-- Add a Web Part to left zone --> <AllUsersWebPart WebPartZoneID="Left" WebPartOrder="0"> <![CDATA[ <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" xmlns:cewp="http://schemas.microsoft.com/WebPart/v2/ContentEditor"> <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly> <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName> <Title>Yet Another Web Part is Born</Title> <FrameType>TitleBarOnly</FrameType> <cewp:Content> This Web Part was added through declarative logic </cewp:Content> </WebPart> ]]> </AllUsersWebPart> <!-- Add a Web Part to right zone --> <AllUsersWebPart WebPartZoneID="Right" WebPartOrder="0"> <![CDATA[ <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image"> <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly> <TypeName>Microsoft.SharePoint.WebPartPages.ImageWebPart</TypeName> <FrameType>None</FrameType> <Title>Watch My Gears Run</Title> <iwp:ImageLink>/_layouts/images/GEARS_AN.GIF</iwp:ImageLink> </WebPart> ]]> </AllUsersWebPart> </File>

Another way is to adding the web parts using code during the feature activating events, like the following.

SPFile page = site.GetFile("SitePages/WebPartPage02.aspx"); SPLimitedWebPartManager mgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared); // add ContentEditorWebPart to Left Zone ContentEditorWebPart wp1 = new ContentEditorWebPart(); wp1.Title = "My Most Excellent Title"; wp1.ChromeType = PartChromeType.TitleOnly; wp1.AllowClose = false; XmlDocument doc = new XmlDocument(); string ns1 = "http://schemas.microsoft.com/WebPart/v2/ContentEditor"; XmlElement elm = doc.CreateElement("Content", ns1); elm.InnerText = "This Web Part was added through code"; wp1.Content = elm; mgr.AddWebPart(wp1, "Left", 0); // add ImageWebPart Web Part to Right Zone ImageWebPart wp2 = new ImageWebPart(); wp2.ChromeType = PartChromeType.None; wp2.ImageLink = @"/_layouts/images/IPVW.GIF"; mgr.AddWebPart(wp2, "Right", 0);

No comments:

Post a Comment