用Feature的方式删除SharePoint2010的Page中重复的WebPart
用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的更多相关文章
- 用PowerShell脚本删除SharePoint 的 Page中的WebPart
编写PowerShell脚本可以删除page中所有的webpart,也可以根据webpart的属性信息去删除特定的webpart. 下面的PowerShell脚本便是删除对应page中所有的webpa ...
- [oracle]删除一张表中重复数据,保留其id字段最小的sql
1.表数据结构如下 select * from test t , 'jerry'); , 'jerry'); , 'jerry'); , 'tom'); , 'tom'); , 'jake'); , ...
- leetcode 26 80 删除已排序数组中重复的数据
80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...
- leetcode 删除一张表中重复邮箱的数据,并且保留最小id 的 那条
/* create view testview as SELECT subject,MIN(Id) as id FROM test GROUP BY subject; select * FROM te ...
- 如何删除github wiki page
title: 如何删除github wiki page toc: false date: 2019-02-23 10:08:41 categories: methods tags: github wi ...
- Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串
C#中要如何才能删除一个字符串数组中的空字符串呢?随着微软对C#不断发展和更新,C#中对于数组操作的方式也变得越来越多样化.以往要实现过滤数组中的空字符串,都是需要实行循环的方式来排除和过滤.C#3. ...
- 如何删除 SQL Server 表中的重复行
第一种:有主键的重复行,就是说主键不重复,但是记录的内容重复比如人员表tab ,主键列id,身份证编号idcard当身份证重复的时候,保留最小id值的记录,其他删除delete a from tab ...
- 【SqlServer】【问题收集】删除同一张表中完全相同的记录
1 概述 在Sqlserver中,当通过SqlServer设计器删除同一张表中两条完全相同的记录时,会弹出如下提示: 点击“是” 弹出如下提示,不让删除 2 问题解决 这个问题很简单,用DEL ...
随机推荐
- 【leetcode】Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- iOS FMDB 不需要关闭
以前做了一个应用,里面用到了FMDB,进行每一次操作前,都open,完成操作后都close.因为我是参考他们以前的代码.程序初期没发现什么问题,程序完成后,各种卡顿就出现了!即使我是放在新线程里操作的 ...
- opencart 引入 TWIG 模板引擎
1.首先将 twig 包放入 system\library 目录. 2.在 system/startup.php 文件最后添加引入语句. require_once(DIR_SYSTEM . 'lib ...
- Python—>Mysql—>Dbvisualizer
MySQLdb: https://pypi.python.org/pypi/MySQL-python/1.2.4 import MySQLdb 1.Download Connector/Python: ...
- 【xml】利用OpenCV解析
看到一篇讲的很清楚的博客:http://blog.csdn.net/jarvischu/article/details/8481510
- 【mongo】Can't take a write lock while out of disk space错误
今天遇到了这个错误,各种无法操作.找了很久的方案,都没用.最终发现,原来是我虚拟机硬盘满了......清除了些没用的东西,就恢复了.
- [聊天框]让DIV的滚动条自动滚动到最底部 - 4种方法
要制作一个在线聊天的程序,在做最后的修饰时,需要对获得的信息即时滚动以保证用户总能看到最新消息. 聊天程序是基于AJAX设计的,没有用框架,消息容器是一个DIV,所以问题就在于如何控制DIV的滚动条. ...
- oracle默认配置ora文件位置
unix:$ORACLE_HOME/dbsnt:c:\Oracle\ora81\database create spfile from pfile = '/home/oracle/initora11g ...
- hdu1141(二进制数位,二分,打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1141 题意:××公司是制造computer的,1960年它造的computer是4bit的,之后每10 ...
- Java观察者模式(Observer模式)
Java深入到一定程度,就不可避免的碰到设计模式(design pattern)这一概念,了解设计模式,将使自己对java中的接口或抽象类应用有更深的理解.设计模式在java的中型系统中应用广泛,遵循 ...