博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Feature的方式删除SharePoint2010的Page中重复的WebPart
阅读量:6362 次
发布时间:2019-06-23

本文共 7372 字,大约阅读时间需要 24 分钟。

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

代码如下所示:

public class SupportCenterDuplicatedWebpartRemovalFeatureReceiver : FnxFeatureReceiver    {        ///         /// Event receiver for feature activation        ///         /// Feature properties passed from SharePoint        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());            }        }        ///         /// reset web feature(create webpartentries) and site feautre(add webpart into pages)        ///         ///         ///         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);            }        }        ///         /// activate or deactivate site feature        ///         ///         ///         ///         ///         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);        }        ///         /// Delete duplicated all webparts        ///         ///         private void DeleteDuplicatedWebparts(SPWeb thisWeb)        {            //delete all webpart.            Collection
existWebpartTitles = new Collection
(); 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 - 1; i >= 0; 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(); } ///
/// Delete all webpartentries in KS project /// ///
private void DeleteWebpartEntries(SPWeb rootWeb) { // delete Web Part template files List
FilesToDelete = new List
(); // 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(); } } } }

 

。。。。。。

转载地址:http://jfsma.baihongyu.com/

你可能感兴趣的文章
原创:形象的讲解angular中的$q与promise
查看>>
hdu4424 Conquer a New Region 并查集/类似最小生成树
查看>>
在标签中打开新文件
查看>>
数据类型转换(初学)
查看>>
分页类及调用-PHP
查看>>
iOS开发-音乐播放(AVAudioPlayer)
查看>>
201521123104 《Java程序设计》第5周学习总结
查看>>
windows+mysql集群搭建-三分钟搞定集群
查看>>
JS字符串大小写转换实现方式
查看>>
event事件
查看>>
js获取微信code
查看>>
PHP输出中文乱码的解决方法
查看>>
Intellisense for the NHibernate XML Schemas
查看>>
dart 异步事件执行流程分析(二)
查看>>
windows平台升级ORACLE11.2.0.1到11.2.0.4
查看>>
Task list 20130311
查看>>
当传统企业遇上大数据
查看>>
ubuntu16.04 开启ipv6支持
查看>>
TCP/IP协议栈 --- 网络层(IP 首部 和分片)
查看>>
<script language="JavaScript"> or <script type="text/javascript"> ?
查看>>