Nov 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.

        Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c
        Yet Another Web Part is Born
          This Web Part was added through declarative logic
        Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c
        Watch My Gears Run

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