1.1. Configuring the WebPart During Deployment
In the previous example, you just wrote an RSSWebPart
and deployed and used it successfully. Using it and configuring it,
however, was done by the end user through the browser. Sometimes during
deployment, you may also want to add a custom SitePage with WebPartZones
on it, configure the WebPart automatically during deployment, and drop
it in one of the WebPartZones of the SitePage. I'm going to extend this
example next by adding a SitePage that has WebPartZones in it. I will
configure this WebPart to the CNN RSS feed instead of my blog, and drop
this configured WebPart into one of the WebPartZones on the custom
SitePage.
Back in the solution in Visual Studio 2010, add the
new SharePoint item, and choose to add a new module called WebPartPage.
Rename the sample.txt file to WebPartPage.aspx. Note that the associated
element.xml also fixes itself when you renamed the sample.txt. Also,
ensure that the RSSWebParts feature includes the WebPartPage module that
you just added. Open the WebPartPage.ASPX file, and change it to
include the code shown in Listing 5.
Example 5. WebPartPage.aspx Code Containing WebPartZones
<%@ Page language="C#" MasterPageFile="˜masterurl/default.master"
meta:WebPartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document"
Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,
Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
<table cellpadding="4" cellspacing="0" border="0" width="100%">
<tr>
<td height="100%" width="50%">
<WebPartPages:WebPartZone runat="server" ID="Left" Title="Left"/>
</td>
<td height="100%" width="50%">
<WebPartPages:WebPartZone runat="server" ID="Right" Title="Right"/>
</td>
</tr>
</table>
</asp:Content>
|
At this time, if you were to package and deploy the solution, you would get a site page with WebPart zones at http://sp2010/WebPartPages/WebPartPage.aspx.
But don't deploy yet! Before you deploy, using a feature receiver, you
will preconfigure this WebPartPage to contain an instance of the
RSSWebPart displaying top stories from CNN.
To do this, add a feature event receiver to the RSSWebParts feature and add the code shown in Listing 6 to the feature event receiver.
Example 6. Code for the Feature Event Receiver
[Guid("34ed6466-8f0e-4330-b910-5a7c8a7d0feb")]
public class RSSWebPartsEventReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPFile file = (properties.Feature.Parent as
SPSite).RootWeb.GetFile("/WebPartPage/WebPartPage.aspx");
SPLimitedWebPartManager lwpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
lwpm.AddWebPart(
new RSSWebPart.RSSWebPart()
{
Title = "CNN News",
RSSUrl = "http://rss.cnn.com/rss/cnn_topstories.rss "
},
"Left",
1);
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPFolder folder = (properties.Feature.Parent as SPSite).RootWeb.GetFolder("/WebPartPage");
folder.Delete();
}
}
|
Now, go ahead and package and deploy the solution one more time. After deployment is completed visit http://sp2010/WebPartPage/WebPartPage.aspx. You should see a CNN News WebPart running, as shown in Figure 11.
Overall, you were able to configure and deploy a WebPart and add it to a WebPartZone, using a feature receiver.