4
votes

I am provisioning a publishing page as part of a feature and placing a single list view web part on the page (see code below). This all works perfectly sp far.

<Elements>
    <Module>
        <File Path="default.aspx" Url="BulletinBoard.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE">
            <Property Name="Title" Value="Bulletin Board" />
            <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/ListNewsletterStyle.aspx" />
            <Property Name="ContentType" Value="Page" />
            <Property Name="PublishingPageImage" Value="" />
            <View List="Lists/BulletinBoard" BaseViewID="2" WebPartZoneID="Main" WebPartOrder="1">
                <![CDATA[
                <webParts>
                    <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
                        <metaData>
                            <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" />
                            <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
                        </metaData>
                        <data>
                            <properties>
                                <property name="Title">Active Announcements</property>
                                <property name="ChromeType">None</property>
                            </properties>
                        </data>
                    </webPart>
                </webParts>
                ]]>
            </View>
        </File>
    </Module>
</Elements>

The only problem is that each time I redeploy my feature through Visual Studio, the list view web part is duplicated (i.e. another one is added to the web part zone).

This problem only appears to affect web parts with the <View ...> tag. Web parts provisioned with the <AllUsersWebPart ...> tag are not being duplicated.

How do I prevent this?

3

3 Answers

3
votes

You can add a feature receiver and add the webpart if it not alreay exists in stead of adding the webpart in XML. What do you mean by tag?

1
votes

Check out this blog entry from Waldek Mastykarz. It has the C# code below that should be similar to what you are looking for.

using (SPSite site = new SPSite("http://sharepoint"))
{
  SPList list = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
  SPListItemCollection items = list.Items;
  List<string> webParts = new List<string>();

  // find the right Page Layout
  foreach (SPListItem item in items)
  {
    if (item.Name.Equals("CustomPageLayout.aspx",
      StringComparison.CurrentCultureIgnoreCase))
    {
      SPFile file = item.File;
      // get the Web Part Manager for the Page Layout
      SPLimitedWebPartManager wpm =
        file.GetLimitedWebPartManager(PersonalizationScope.Shared);
      // iterate through all Web Parts and remove duplicates
      while (wpm.WebParts.Count > 0)
      {
        StringBuilder sb = new StringBuilder();
        XmlWriterSettings xws = new XmlWriterSettings();
        xws.OmitXmlDeclaration = true;
        XmlWriter xw = XmlWriter.Create(sb, xws);
        System.Web.UI.WebControls.WebParts.WebPart wp =
          wpm.WebParts[0];
        wpm.ExportWebPart(wp, xw);
        xw.Flush();
        string md5Hash = getMd5Hash(sb.ToString());
        if (webParts.Contains(md5Hash))
          wpm.DeleteWebPart(wp);
        else
          webParts.Add(md5Hash);
      }
    }
  }
}
0
votes

Note to SharePoint 2013 users. You should add ReplaceContent=True to the tag. The duplicate webpart issue will be resolved.