用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 ...
随机推荐
- Unity3d 怪物死亡燃烧掉效果
效果 BurnToFadeOut.shader代码 Shader "BurnToFadeOut" { Properties { _StartColor ("Start C ...
- ios 关于使用异步网络请求时block回调的内存注意
在一个controller中,使用 NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest ...
- js数组转置
<script type="text/javascript"> var arr=[[1,2,3],[4,5,6],[7,8,9],[17,18,19]]; ...
- Python~win32com~Excel
import win32com.client #w=win32com.client.Dispatch("Word.Application") #w.Visible=1 o=win3 ...
- iOS 发送请求时获取cookie
Cookie: 记录者用户信息的保存在本地的用户数据,如果有会被自动附上 值得一提的是,在iOS中当你发送一个任意请求时,不管你愿不愿意,NSURLRequest都会自动帮你记录你所访问的URL上设置 ...
- python数据库(mysql)操作
http://fantefei.blog.51cto.com/2229719/1282443
- mysql无法启动
当在安装mysql服务时,有时会遇到恶心的PID错误而导致安装后无法启动以下为针对mysql-5.5版本在安装mysql时所遇到的问题的解决方法. 1.可能是/usr/local/mysql/data ...
- 电子现金、电子钱包、qPBOC、闪付、UPCash
一.关于金融IC卡领域的规范 由Europay.Mastercard.Visa三大国际信用卡组织联合制定的金融集成电路(IC)卡金融支付标准,称为EMV规范,其目的是为金融IC卡.金融终端.支付系统以 ...
- [Android Pro] InputStream.skip方法的思考
参考 : http://blog.csdn.net/gsyzhu/article/details/8102286 在java.io.InputStream类中定义了skip这个方法.在API中的描述如 ...
- Eclipse CDT launch failed.Binary not found in Linux/Ubuntu
转自:http://blog.csdn.net/abcjennifer/article/details/7573916 Linux下出现launch failed.Binary not found的解 ...