用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 2769

    http://poj.org/problem?id=2796 题意:求n个数的和乘以这n个数中的最小值的积最大的数,以及其范围. 思路:求每一个数两边的比其大的数的和,再乘以这个数.还有一个范围,用单 ...

  2. Application.AddMessageFilter(this);

    开发环境:windows 8(x64), vs2013 只要“项目属性-调试”中选中“启用Visual Studio承载进程“,在VS2013中用F5调试,调用Application.AddMessa ...

  3. EF-实体更新

    1.数据库表增加字段,EF更新视图后,对应的实体对象没有新增的字段原因:edmx文件右键属性设置了 保存时转换相关的文本模板-false...正确的应该是rue 2. 更改视图后(或者更改字段类型?) ...

  4. 20145213《Java程序设计》第九周学习总结

    20145213<Java程序设计>第九周学习总结 教材学习总结 "五一"假期过得太快,就像龙卷风.没有一点点防备,就与Java博客撞个满怀.在这个普天同庆的节日里,根 ...

  5. cocos2d-x 第三篇 基本概念介绍

    场景(scene): 也有人叫做屏幕或舞台,是一个独立的程序流,一个程序可以有很多场景但当前运行的场景就只有一个.比如游戏中可以有介绍场景,菜单场景,第一关场景,过场1场景,第二关场景,胜利场景等.一 ...

  6. UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

    Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...

  7. September 19th 2016 Week 39th Monday

    We come nearest to the great when we are great in humility. 我们最为谦逊的时候越接近伟大. When you are powerful en ...

  8. Mysql之INFORMATION_SCHEMA解析1

    INFORMATION_SCHEMA库是Mysql提供的一个系统库,保存了数据库的原数据,方便用户监控与管理Msyql. 现在单说与INNODB相关的库:INNODB_SYS_TABLES,INNOD ...

  9. Innodb之拷贝InnoDB表从一服务器到另一台服务器

    将Innodb类型的表从一台服务器拷贝到另一台服务器,或从一个库拷贝到另一个库. 前提是:innodb_file_per_table =ON. 1 先在目标服务器(库)上创建一个相同的表结构. 如: ...

  10. cf118A(水题)

    题意就是讲给出的字符串元音字母去掉,在每个辅音字母前加点,且小写输出...注意y也要去掉(以我英语挂科的水平也知道y是辅音字母)... 水题.. 直接上代码好了... #include <ios ...