用Feature的方式删除SharePoint2010的Page中重复的WebPart。

代码如下所示:

public class SupportCenterDuplicatedWebpartRemovalFeatureReceiver : FnxFeatureReceiver
{
/// <summary>
/// Event receiver for feature activation
/// </summary>
/// <param name="properties">Feature properties passed from SharePoint</param>
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
base.FeatureActivated(properties);
if (properties == null)
{
throw new ArgumentNullException("properties");
} SPFeatureScope scope = properties.Feature.Definition.Scope;
SPSite parentSite = null;
SPWeb thisWeb = null;
SPWeb rootWeb = null; // Note: Scope should be "Web" for Support Center Pages feature but handler is here in case of future deployment scope change
if (scope == SPFeatureScope.Site)
{
parentSite = properties.Feature.Parent as SPSite;
if (parentSite != null)
{
rootWeb = parentSite.RootWeb;
thisWeb = rootWeb;
}
}
else if (scope == SPFeatureScope.Web)
{
thisWeb = properties.Feature.Parent as SPWeb; if (thisWeb != null)
{
parentSite = thisWeb.Site; if (parentSite != null)
{
rootWeb = parentSite.RootWeb;
}
}
} try
{
if (rootWeb != null)
{
if (thisWeb != null)
{
// we only add the navaigation to support subsite; otherwise the 2nd level menu will mess up
if (!thisWeb.Url.ToLower().Contains(KSConstants.SUPPORTER_SITE_URL.ToLower())) return;
DeleteDuplicatedWebparts(thisWeb);
DeleteWebpartEntries(rootWeb);
ResetFeatures(parentSite);
}
}
}
catch (SPException ex)
{
Trace.WriteLine(ex.Message);
LoggingService.LogError("Features", ex.ToFormattedExceptionString());
}
} /// <summary>
/// reset web feature(create webpartentries) and site feautre(add webpart into pages)
/// </summary>
/// <param name="thisWeb"></param>
/// <param name="parentSite"></param>
private void ResetFeatures(SPSite parentSite)
{
Guid siteFeatureId = new Guid("668ce347-97bb-4048-ae96-95f2c3a4cc42");
var siteFeature = parentSite.Features.SingleOrDefault(sf => sf.DefinitionId == siteFeatureId);
if (siteFeature == null)
{
ActivateOrReactiveSiteFeature(parentSite, siteFeatureId, true, true);
}
else
{
ActivateOrReactiveSiteFeature(parentSite, siteFeatureId, false, true);
ActivateOrReactiveSiteFeature(parentSite, siteFeatureId, true, true);
}
} /// <summary>
/// activate or deactivate site feature
/// </summary>
/// <param name="site"></param>
/// <param name="featureGuid"></param>
/// <param name="activate"></param>
/// <param name="force"></param>
private void ActivateOrReactiveSiteFeature(SPSite site, Guid featureGuid, bool activate, bool force)
{
if (activate)//if feature is not activated
{
site.Features.Add(featureGuid, force);
//activate feature
}
else site.Features.Remove(featureGuid, force);
} /// <summary>
/// Delete duplicated all webparts
/// </summary>
/// <param name="thisWeb"></param>
private void DeleteDuplicatedWebparts(SPWeb thisWeb)
{
//delete all webpart.
Collection<string> existWebpartTitles = new Collection<string>();
SPList pageList = thisWeb.Lists["Pages"];
foreach (SPListItem item in pageList.Items)
{
SPFile file = thisWeb.GetFile(thisWeb.Url + "/" + item.Url);
if (file.CheckOutType != Microsoft.SharePoint.SPFile.SPCheckOutType.None)
{
file.UndoCheckOut();
}
file.CheckOut();
if (file != null)
{
existWebpartTitles.Clear();
using (SPLimitedWebPartManager webpartsMng = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
for (int i = webpartsMng.WebParts.Count - ; i >= ; i--)
{
//delete condition:
//1. if it is "Content Top 5 WebPart", webpart with same catalog will be deleted.
//2. if it is not "Content Top 5 WebPart", webpart with same title will be deleted.
if (webpartsMng.WebParts[i].Title.Equals("Content Top 5 WebPart", StringComparison.Ordinal))
{
string catagory = (webpartsMng.WebParts[i].WebBrowsableObject as FundsNetwork.KnowledgeAndSupport.WebParts.Top5WebPart.Top5WebPart).Category;
if (!existWebpartTitles.Contains(catagory))
{
existWebpartTitles.Add(catagory);
continue;
}
}
else
{
if (!existWebpartTitles.Contains(webpartsMng.WebParts[i].Title))
{
existWebpartTitles.Add(webpartsMng.WebParts[i].Title);
continue;
}
}
webpartsMng.DeleteWebPart(webpartsMng.WebParts[i]);
}
}
}
file.CheckIn("Delete personal Webparts.");
}
thisWeb.Update();
} /// <summary>
/// Delete all webpartentries in KS project
/// </summary>
/// <param name="rootWeb"></param>
private void DeleteWebpartEntries(SPWeb rootWeb)
{
// delete Web Part template files
List<SPFile> FilesToDelete = new List<SPFile>();
// figure out which Web Part template files need to be deleted
SPList WebPartGallery = rootWeb.Lists["Web Part Gallery"];
if (WebPartGallery != null)
{
foreach (SPListItem WebPartTemplateFile in WebPartGallery.Items)
{
if (WebPartTemplateFile.File.Name.Contains("KnowledgeAndSupport"))
{
FilesToDelete.Add(WebPartTemplateFile.File);
}
}
// delete Web Part template files
foreach (SPFile file in FilesToDelete)
{
if (file.CheckOutType != Microsoft.SharePoint.SPFile.SPCheckOutType.None)
{
file.UndoCheckOut();
}
file.CheckOut();
file.Delete();
file.Update();
rootWeb.Update();
}
}
}
}

。。。。。。

用Feature的方式删除SharePoint2010的Page中重复的WebPart的更多相关文章

  1. 用PowerShell脚本删除SharePoint 的 Page中的WebPart

    编写PowerShell脚本可以删除page中所有的webpart,也可以根据webpart的属性信息去删除特定的webpart. 下面的PowerShell脚本便是删除对应page中所有的webpa ...

  2. [oracle]删除一张表中重复数据,保留其id字段最小的sql

    1.表数据结构如下 select * from test t , 'jerry'); , 'jerry'); , 'jerry'); , 'tom'); , 'tom'); , 'jake'); , ...

  3. leetcode 26 80 删除已排序数组中重复的数据

    80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...

  4. leetcode 删除一张表中重复邮箱的数据,并且保留最小id 的 那条

    /* create view testview as SELECT subject,MIN(Id) as id FROM test GROUP BY subject; select * FROM te ...

  5. 如何删除github wiki page

    title: 如何删除github wiki page toc: false date: 2019-02-23 10:08:41 categories: methods tags: github wi ...

  6. Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  7. C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串

    C#中要如何才能删除一个字符串数组中的空字符串呢?随着微软对C#不断发展和更新,C#中对于数组操作的方式也变得越来越多样化.以往要实现过滤数组中的空字符串,都是需要实行循环的方式来排除和过滤.C#3. ...

  8. 如何删除 SQL Server 表中的重复行

    第一种:有主键的重复行,就是说主键不重复,但是记录的内容重复比如人员表tab ,主键列id,身份证编号idcard当身份证重复的时候,保留最小id值的记录,其他删除delete a from tab ...

  9. 【SqlServer】【问题收集】删除同一张表中完全相同的记录

    1   概述 在Sqlserver中,当通过SqlServer设计器删除同一张表中两条完全相同的记录时,会弹出如下提示: 点击“是” 弹出如下提示,不让删除 2   问题解决 这个问题很简单,用DEL ...

随机推荐

  1. POJ 2479

    ---恢复内容开始--- http://poj.org/problem?id=2479 #include <stdio.h> #include <iostream> using ...

  2. Portal

    https://chenliang0571.wordpress.com/2013/12/08/openwrt-wifidog-wifi-hotspots/http://www.h3c.com.cn/M ...

  3. keepalived+haproxy构建高可用负载均衡

    一.环境介绍 我用的是centos6.7,内核版本为2.6.32-573.el6.x86_64,keepalived版本为keepalived-1.2.22,haproxy版本为haproxy-1.6 ...

  4. Qt 常用的功能

    1.程序重启 void Widget::reStart() {   qApp->closeAllWindows(); QProcess::startDetached(qApp->appli ...

  5. ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)

    这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...

  6. 关于C语言中的转义字符

    1.转义字符的分类 1. 1一般转义字符 这种转义字符,虽然在形式上由两个字符组成,但只代表一个字符.常用的一般转义字符为: \a     \n     \t     \v     \b     \r ...

  7. 【python】入门学习(八)

    异常处理: python在遇到问题时会自动引发异常,也可以用raise故意引发异常,异常种类必须是已有的 >>> raise IOError('This is a test.') T ...

  8. 【python】dict的注意事项

    1. key不能用list和set 由于列表是易变的,故不可做key.如果使用会报错 但是元组可以做key 2.遍历方法 for key in somedict: pass 速度快,但是如果要删除元素 ...

  9. vs win32 & MFC 指针默认位置

    一开始win32指针所在的位置是与debug文件夹同级的.即打开打开改程序的第一个文件夹这一级. MFC指针是在第二个debug下头,就是打开第二个project名词的文件夹下头,e.g., &quo ...

  10. SQL 映射的 XML 文件

    MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方. 对于所有的力量, SQL映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大 ...