When working with SharePoint solutions – you often provision web parts and other components to the site. Recently I have been provisioning several web parts on the page programmatically using Visual Studio 2010 module and ran into an issue where my few of my web parts were not added to the page despite me doing all of the proper feature activation and everything defined in a module.
Here is how my Elements.xml was defined in a Visual Studio module:
<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”HomePage” Url=”$Resources:osrvcore,List_Pages_UrlName;” Path=”">
<File Url=”Default.aspx” Type=”GhostableInLibrary” Path=”HomePage\default.aspx”>
<Property Name=”Title” Value=”Home” /><AllUsersWebPart ID=”MyWebpart” WebPartOrder=”1″ WebPartZoneID=”Middle”>
<![CDATA[
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.microsoft.com/WebPart/v2">
<Title>Content Editor</Title>
<FrameType>Default</FrameType>
<Description>Allows authors to enter rich text content.</Description>
<PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
</WebPart>
]]>
</AllUsersWebPart><AllUsersWebPart ID=”MyWebpart” WebPartOrder=”1″ WebPartZoneID=”Middle”>
<![CDATA[
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.microsoft.com/WebPart/v2">
<Title>Content Editor</Title>
<FrameType>Default</FrameType>
<Description>Allows authors to enter rich text content.</Description>
<PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
</WebPart>
]]>
</AllUsersWebPart>
</File>
</Module>
</Elements>
As you can see above we have 2 content editor web parts added and both of the web part XML instances were obtained by using an Export feature in the SharePoint site. However, only one of the web parts showed up.
The problem was that although both webparts when added to the portal were named differently (Content Editor [1] and [2]) their actual title remained to be Content Editor in the property of the XML.
SharePoitn uses title to identify the web part of the page and in this case the newer web part (whatever the order is) replaces the other instance.
So, conclusion – don’t discard web part title when provisioning your web parts!
Good luck!
