用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. We will be discontinuing the Nitrous Development Platform and Cloud IDE on November 14th, 2016.

    我表示我很难过 Nitrous We will be discontinuing the Nitrous Development Platform and Cloud IDE on November ...

  2. std::map

    1.例: map<int,string> m_mapTest; m_mapTest.insert(make_pair(1,"kong")); m_mapTest.ins ...

  3. Android开发者必备的42个链接

    http://mobile.51cto.com/ahot-426035.htm Android开发者必备的42个链接 下面收集了42个帮助大家学习Android的内容链接,部分内容是面向初学者的,帮助 ...

  4. java mail api 使用

    所需要的jar包: http://pan.baidu.com/s/1qWGZRJm 如果遇到这个错误:在windows防火墙允许 javaw.exe访问网络.或者关闭防火墙 FATAL ERROR i ...

  5. HttpWebRequest.GetResponse 方法

    GetResponse 方法返回包含来自 Internet 资源的响应的 WebResponse 对象. 实际返回的实例是 HttpWebResponse,并且能够转换为访问 HTTP 特定的属性的类 ...

  6. gtk+-3.21.4 static build step in windows XP

    In recent days the weather is very hot Unable to sleep properly Under the state of daze research gtk ...

  7. Django~urls.py--->views.py

    The 'r' in front of each regular expression string is optional but recommended. It tells Python that ...

  8. cmd命令查看局域网内计算机信息

    ping [计算机名] ping -a [IP] nbtstat -a [IP] net view arp -a nslookup www.baidu.com 查看当前dns地址 tracert [I ...

  9. C Primer Plus_第一章_概览_复习题与编程练习

    REVIEW 1.就编程而言,可移植性表示什么? me 一个系统上编写的程序经过很少改动或者不需改动就可以在另一个系统上运行.如果修改是必须的,则通常只改变伴随主程序的一个头文件中的几项内容即可.(P ...

  10. 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题(转载)

    1.问题复现 spring 3.0 + hibernate 3.2 spring mvc使用注解方式:service使用@service注解 事务使用@Transactional 事务配置使用 < ...