So yesterday I was trying to add a content editor webpart to a page I was deploying through ONET.XML. I wondered how to add custom web parts, and how you’d know what the XML for them was.
Turns out it isn’t very hard. An example:
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="SourceCode.Solutions.GenericCaseWebParts.CaseSSRSReportViewer" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="TitleUrl" type="string" />
<property name="TitleIconImageUrl" type="string" />
<property name="ReportServerUrl" type="string">http://vm-moss:8088/ReportServer</property>
<property name="Parameters" type="string"><ReportParameters><Parameter Name="CaseId" MultiValue="false" Value="[Case Id]" /></ReportParameters></property>
<property name="ChromeState" type="chromestate">Normal</property>
<property name="Title" type="string">Context Info</property>
<property name="ReportPath" type="string">/Reports/Case Details</property>
<!-- Snip -->
</properties>
</data>
</webPart>
</webParts>
As you can see, at the top we define the class to use, then we set various properties (I’ve trimmed that set quite a lot). But how did I get the XML? Well… I set the web part up in SharePoint and exported the web part to a file; this was the XML I needed to put into my ONET.xml. Copy and paste… job done.
Let me pose this to you, and realize i’m a newb. I am creating a site definition, and in the onet.xml I’m adding servarl ReportViewerWebParts to a report page. Inside the CData section there is a ReportPath node that defines where the rdl file exists using an absolute url. When I provision a new site I’d like to update the ReportPath to point to a location on the new site. However, I have not found a way to access the ReportPath through code. During site provisioning, the ReportViewerWebPart doesn’t seem to be accessible (if I try to get it through the SPLimitedWebPartManager, all I get is an ErrorWebPart). Now the way I am calling the site creation code is through an Infopath form on the parent site – the user fills in some information for the new site, which is used in the call to create the new site. Am I breaking any rules by updating ReportPath value in the onet.xml file before I call my site creation code? I know it kind-of works in my tests, but sometimes it appears that the onet.xml is cached, as the new site sometimes had reportpaths to the previously created site rather than the new site. If I haven’t erred grievously, do you know how to force the site provisioning code to not used a cached version of the onet.xml? thanks for any guidance you can provide.
Hmm. Interesting one, that – and something I have to figure out later this month!
Where is your code to get the SPLimitedWebPartManager running? Is it in a feature receiver that is stapled to the site definition? That way, you should be confident that the Web Part should exist before you try to access it.
Otherwise, I don’t know – but I will have to figure this one out myself.
I’m using the SPWebProvisioningProvider suggested by Connell so that I can get the site constructed before doing any custom code. The basic code outline is
public override void Provision(SPWebProvisioningProperties props)
{
Microsoft.Office.Server.Diagnostics.PortalLog.LogString(“Beginning CPDCMSiteDef provision code.”);
SPWeb elevWeb = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Apply the actual Web template for the CPD CM site
// get elevated web – sometimes sharepoint doesn’t like to do stuff inside the elevpriv block
elevWeb = props.Web;
elevWeb.ApplyWebTemplate(CPDCMSiteDef#0);
});
elevWeb.AllowUnsafeUpdates = true;
elevWeb.Site.AllowUnsafeUpdates = true;
… all of the site pieces are in the onet.xml of the CPDCMSiteDef#0 template. After the site has been created above, I run the custom code, which is where I have tried to use the SPLimitedWebPartManager to get at the ReportViewerWebPart. ListViewWebParts on the same page are accessible but the ReportViewerWebPart doesn’t seem to be.
Can you please tell me where exactly should the webparts node be placed on the onet.xml? a screenshot of the node structure of the onet.xml would be very helpful.
Thanks.
Sure. It’s within a File node of one the Module nodes of your ONET.xml. This page has an example:
http://msdn.microsoft.com/en-gb/library/ms474369.aspx
You’re looking for the ‘AllUsersWebPart’ element:
http://msdn.microsoft.com/en-us/library/dd585727.aspx
(Unless you’re wanting to use a ListView web part – they’re a bit different):
http://www.novolocus.com/2009/05/13/pointing-listviewwebparts-at-lists/
How do you export a web part to a file?
Edit the page > Modify Web Part > Export…
Doesn’t work with ListViews