How do I add a preconfigured XML web part to a site definition? Part 1 of 3 on the road to sticky left hand navigation in SharePoint Portal Server.

So our 1st technical challenge to overcome from “The Road to Sticky Left Hand Navigation in SharePoint Portal Server 2003” is how to add a pre-configured XML web part to a site definition.  I would have thought this would have been previously documented but then I remembered this is SharePoint and nothing is documented.  <grin>  Ok, that might be an exaggeration but all of the examples I could find about adding a web part to a site definition used the image web part as an example.  So let’s quickly look at that code.  If you open the onet.xml for the STS definition (C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\TEMPLATE\1033\STS\XML\onet.xml)from a default SharePoint install you will see:


<AllUsersWebPart WebPartZoneID=”Right” WebPartOrder=”1″>
                   <![CDATA[
                   <WebPart xmlns=”http://schemas.microsoft.com/WebPart/v2″ xmlns:iwp=”http://schemas.microsoft.com/WebPart/v2/Image”>
                        <Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
                        <TypeName>Microsoft.SharePoint.WebPartPages.ImageWebPart</TypeName>
                        <FrameType>None</FrameType>
                        <Title>Site Image</Title>
                        <iwp:ImageLink>/_layouts/images/homepage.gif</iwp:ImageLink>
                   </WebPart>
                   ]]>
                </AllUsersWebPart>


Ok so that is our example text.  Now what?  Well let’s see if we can figure out how to plug into that format the information that is needed to render an XML Viewer web part.  Well, I tried the guessing game for a little bit and realized I would have a better shot at getting my dog to answer the phone than I would at guessing the combination.  Then it occurred to me… the best way to see the code it takes to render that web part is to configure one the way I want and then export it.  So I export the configured part and save it as XML Web Part.dwp.  Then I open that file with WordPad (for some reason WordPad does a much better job than notepad) and here is what I found:


<?xml version=”1.0″ encoding=”utf-8″?>
<WebPart xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://schemas.microsoft.com/WebPart/v2″>
  <Title>XML Web Part</Title>
  <FrameType>None</FrameType>
  <Description>Use for XML, and XSL Transformation of the XML.</Description>
  <IsIncluded>true</IsIncluded>
  <ZoneID>{7AC0DCA3-098D-40FF-B6C3-D945910AC119}</ZoneID>
  <PartOrder>1</PartOrder>
  <FrameState>Normal</FrameState>
  <Height />
  <Width />
  <AllowRemove>true</AllowRemove>
  <AllowZoneChange>true</AllowZoneChange>
  <AllowMinimize>true</AllowMinimize>
  <IsVisible>true</IsVisible>
  <DetailLink />
  <HelpLink />
  <Dir>Default</Dir>
  <PartImageSmall />
  <MissingAssembly />
  <PartImageLarge>/_layouts/images/msxmll.gif</PartImageLarge>
  <IsIncludedFilter />
  <Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
  <TypeName>Microsoft.SharePoint.WebPartPages.XmlWebPart</TypeName>
  <XMLLink xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml”>http://portal.abc.local/SiteImages/links.xml</XMLLink>
  <XML xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml” />
  <XSLLink xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml”>http://portal.abc.local/SiteImages/links.xsl</XSLLink>
  <XSL xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml” />
  <PartStorage xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml” />
</WebPart>


Now believe it or not all we have to do is play the match game.  (Wasn’t life much simpler back in the day when you just had to draw a line from the yellow square to the red square because they were the same shape.)  So for example the first section is Web Part.  In the onet.xml file you have:


<WebPart xmlns=”http://schemas.microsoft.com/WebPart/v2″ xmlns:iwp=”http://schemas.microsoft.com/WebPart/v2/Image”>


And in the DWP you get:


<WebPart xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://schemas.microsoft.com/WebPart/v2″>


So I just build my XML Web Part in the onet.xml file in this manner.  Following the match game I ended up with:


<AllUsersWebPart WebPartZoneID=”Right” WebPartOrder=”1″>
  <![CDATA[
     <WebPart xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://schemas.microsoft.com/WebPart/v2″>
        <Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
        <TypeName>Microsoft.SharePoint.WebPartPages.XmlWebPart</TypeName>
        <FrameType>None</FrameType>
        <Title>XML Web Part</Title>
      </WebPart>
   ]]>
 </AllUsersWebPart>


Now I knew there was no chance of this working because I hadn’t given it enough information yet.  I hadn’t told the web part where to get the XML or XSL file from yet.  I also noticed while I was playing the match game I no match for the line:


<iwp:ImageLink>/_layouts/images/homepage.gif</iwp:ImageLink>


So in place of that line I put in the sections that I thought would create my links for me:


<XMLLink xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml”>http://portal.abc.local/SiteImages/links.xml</XMLLink>
  <XML xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml” />
  <XSLLink xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml”>http://portal.abc.local/SiteImages/links.xsl</XSLLink>
  <XSL xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml” />
  <PartStorage xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml” />


As you can see here there are links for the XML file and XSL file that I had put in a document library called SiteImages.  I did this so the user could quickly modify the files to affect the navigation for updates in the future. 


So now I took this junk of code and in put it in onet.xml file:


<AllUsersWebPart WebPartZoneID=”Right” WebPartOrder=”1″>
 <![CDATA[
    <WebPart xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://schemas.microsoft.com/WebPart/v2″>
          <Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
          <TypeName>Microsoft.SharePoint.WebPartPages.XmlWebPart</TypeName>
          <FrameType>None</FrameType>
          <Title>XML Web Part</Title>
          <XMLLink xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml”>http://portal.abc.local/SiteImages/links.xml</XMLLink>
          <XML xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml” />
          <XSLLink xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml”>http://portal.abc.local/SiteImages/links.xsl</XSLLink>
          <XSL xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml” />
          <PartStorage xmlns=”http://schemas.microsoft.com/WebPart/v2/Xml” />
    </WebPart>
   ]]>
</AllUsersWebPart>


Did a quick IISReset and created a new site.  Viola a pre-configured XML web part.  Now if I wanted to get tricky I could use the article How to create a site definition that creates a picture library and populates it with an image. To create the document library with the XML and XSL file already included. 


Here is a link to an onet.xml file saved as a word doc with this code already placed in it.  I have highlighted the code in blue so you can quickly scroll through and find it.  Also, if you would like to use a preconfigured XML and XSL file to test with (and to demonstrate how to make one with built in images and links) you can find them here.


A special thank you goes to Kevin Pine at SharePoint Solutions.  He helped me work through these issues.  (He is also the one who did the entire site definition for SharePoint911.com.)  Anyway, he has a special course for teaching these types of customizations to SharePoint that he wrote “SharePoint 2003 Extreme Makeover”.  And if you sneak over to this LINK for a limited time you can grab yourself a $200 discount on the course and the chance to win an IPod.  SHHH!  It is a secret. 



Shane Young
SharePoint Help



8 Comments so far

  1.   Ryan on March 29th, 2006          Reply

    Shane –

    Great article, thanks.

    We’ve done something kinda similiar in basically customizing a link list to give our users collapseable navigation, etc.

    I wonder – we should be able to include our customized DVWP in the site definition as well, shouldn’t we?

  2.   Shane on March 29th, 2006          Reply

    Ryan – You should be able to include a configured DVWP. I would start by exporting the web part so you have a DWP file and then try to figure out things from there. Email me if you need help. Shane

  3.   Du lich Viet nam on October 19th, 2006          Reply

    Van phong du lich, cong ty du lich, trung tam lu hanh quoc te A Chau: du lich nuoc ngoai, du lich Viet Nam, du lich Thai Lan, du lich Trung Quoc, Du lich Singapore, du lich Malaixia

  4.   Andy H on March 16th, 2007          Reply

    Hi Shane,

    I’m trying to add the sharepoint standard “Links” web part to the ONET.XML file and aren’t having much luck.

    This isn’t made very easy by the fact that the “Links” web part won’t let me export it to a DWP file – the option simply isn’t there to export it. The only reference to the webpart I can find is Microsoft.SharePoint.Portal.WebControls.QuickLinksMicroView but I’m not sure if this is the correct one.

    Any ideas?

    Cheers

    Andy

  5.   Rqjezvnq on July 13th, 2009          Reply

    5RG1lX

  6.   everythingbrewingonseminarystreet.com on September 13th, 2009          Reply

    This includes newspapers and paper packaging, reputedly highly biodegradable yet one can find nearly whole copies of newspapers from decades past in landfills. , toysnotmadeinchina.org, [url=”http://nlernca.bestinternetdancer.com/toysnotmadeinchina.org.html”]toysnotmadeinchina.org[/url], http://nlernca.bestinternetdancer.com/toysnotmadeinchina.org.html toysnotmadeinchina.org, %-], minigirlstgp.biz, [url=”http://loojztw.angelfire.com/minigirlstgp.biz.html”]minigirlstgp.biz[/url], http://loojztw.angelfire.com/minigirlstgp.biz.html minigirlstgp.biz, dloli, godaven.com, [url=”http://ydluwvq.is-the-boss.com/godaven.com.html”]godaven.com[/url], http://ydluwvq.is-the-boss.com/godaven.com.html godaven.com, sjzh, alpharetta.ga.us, [url=”http://btucetd.jesuisopen.fr/alpharetta.ga.us.html”]alpharetta.ga.us[/url], http://btucetd.jesuisopen.fr/alpharetta.ga.us.html alpharetta.ga.us, %OO, http://www.castelandesigns.com, [url=”http://uulgcum.angelfire.com/www.castelandesigns.com.html”]www.castelandesigns.com[/url], http://uulgcum.angelfire.com/www.castelandesigns.com.html http://www.castelandesigns.com, 821, winrar 2.8, [url=”http://fquhvgv.thewomanizer.net/winrar-2.8.html”]winrar 2.8[/url], http://fquhvgv.thewomanizer.net/winrar-2.8.html winrar 2.8, :DD, prepmagazine.us, [url=”http://baiegvk.calimerou.fr/prepmagazine.us.html”]prepmagazine.us[/url], http://baiegvk.calimerou.fr/prepmagazine.us.html prepmagazine.us, 203557, copywriting.net, [url=”http://rlgkbtz.i-was-in-paris.com/copywriting.net.html”]copywriting.net[/url], http://rlgkbtz.i-was-in-paris.com/copywriting.net.html copywriting.net, xzn, epdeltakitchen.com, [url=”http://evaejsa.thewomanizer.net/epdeltakitchen.com.html”]epdeltakitchen.com[/url], http://evaejsa.thewomanizer.net/epdeltakitchen.com.html epdeltakitchen.com, 30181, http://www.undercovertv.com, [url=”http://uymbjsy.angelfire.com/www.undercovertv.com.html”]www.undercovertv.com[/url], http://uymbjsy.angelfire.com/www.undercovertv.com.html http://www.undercovertv.com, weaqcf, spy.rs, [url=”http://uizsiio.angelfire.com/spy.rs.html”]spy.rs[/url], http://uizsiio.angelfire.com/spy.rs.html spy.rs, hpjpvu, http://www.thinnerscales.com, [url=”http://eieaobs.i-was-in-paris.com/www.thinnerscales.com.html”]www.thinnerscales.com[/url], http://eieaobs.i-was-in-paris.com/www.thinnerscales.com.html http://www.thinnerscales.com, 9390, http://www.powerdynesuperchargers.com, [url=”http://tsooose.angelfire.com/www.powerdynesuperchargers.com.html”]www.powerdynesuperchargers.com[/url], http://tsooose.angelfire.com/www.powerdynesuperchargers.com.html http://www.powerdynesuperchargers.com, 814128, http://www.tredsafe.com, [url=”http://cbagajg.i-was-in-paris.com/www.tredsafe.com.html”]www.tredsafe.com[/url], http://cbagajg.i-was-in-paris.com/www.tredsafe.com.html http://www.tredsafe.com, 41215,

  7.   Buy Ambien on February 17th, 2010          Reply

    morse vary perceive ldrs coupling satisfy inevitable saga hawaii practicality acquirer
    saramartisakis kulturenostro

  8.   Kumar Sah on March 7th, 2012          Reply

    Thanks. The dwp file creation provides the syntax to create different types of webparts.

Leave a Reply