我正在配置发布页面作为功能的一部分,并在页面上放置单个列表视图Web部件(请参阅下面的代码)。这一切都完美无缺。
<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>
唯一的问题是,每次通过Visual Studio重新部署我的功能时,列表视图Web部件都会重复(即另一个部件被添加到Web部件区域)。
此问题仅出现以影响带有&lt; View ...&gt;的Web部件标签。使用&lt; AllUsersWebPart ...&gt;配置的Web部件标签没有重复。
如何防止这种情况?
答案 0 :(得分:3)
您可以添加功能接收器并添加Web部件(如果尚不存在),而不是在XML中添加Web部件。 标签是什么意思?
答案 1 :(得分:1)
查看Waldek Mastykarz的blog entry。它下面的C#代码应该与您要查找的类似。
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);
}
}
}
}
答案 2 :(得分:0)
SharePoint 2013用户请注意。您应该将ReplaceContent = True添加到标记中。将解决重复的webpart问题。