Putting Custom Web Parts in ONET.xml

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&gt;
<property name="Parameters" type="string">&lt;ReportParameters&gt;&lt;Parameter Name="CaseId" MultiValue="false" Value="[Case Id]" /&gt;&lt;/ReportParameters&gt;</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.

Advertisements
Putting Custom Web Parts in ONET.xml

7 thoughts on “Putting Custom Web Parts in ONET.xml

  1. Dan Hillman says:

    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.

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

  3. Dan Hillman says:

    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.

  4. PRaj says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.