private static bool DeleteWebPart(Web web, string serverRelativePageUrl, Guid storageKey) { bool isWebPartDeleted = false; LimitedWebPartManager limitedWebPartManager = null; try { var webPartPage = web.GetFileByServerRelativeUrl(serverRelativePageUrl); web.Context.Load(webPartPage); web.Context.ExecuteQueryRetry(); limitedWebPartManager = webPartPage.GetLimitedWebPartManager(PersonalizationScope.Shared); web.Context.Load(limitedWebPartManager.WebParts, wps => wps.Include(wp => wp.Id)); web.Context.ExecuteQueryRetry(); if (limitedWebPartManager.WebParts.Count >= 0) { foreach (WebPartDefinition webpartDef in limitedWebPartManager.WebParts) { Microsoft.SharePoint.Client.WebParts.WebPart oWebPart = null; try { oWebPart = webpartDef.WebPart; if (webpartDef.Id.Equals(storageKey)) { webpartDef.DeleteWebPart(); web.Context.ExecuteQueryRetry(); isWebPartDeleted = true; break; } } catch (Exception ex) { Logger.LogErrorMessage("[DeleteMissingWebparts: DeleteWebPart]. Exception Message: " + ex.Message + ", Exception Comments: Exception occured while deleting the webpart", true); ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, web.Url, "Webpart", ex.Message, ex.ToString(), "DeleteWebPart", ex.GetType().ToString(), Constants.NotApplicable); } finally { oWebPart = null; } } } } catch (Exception ex) { Logger.LogErrorMessage("[DeleteMissingWebparts: DeleteWebPart]. Exception Message: " + ex.Message + ", Exception Comments: Exception occure while fetching webparts using LimitedWebPartManager", true); ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, web.Url, "Webpart", ex.Message, ex.ToString(), "DeleteWebPart", ex.GetType().ToString(), Constants.NotApplicable); } finally { limitedWebPartManager = null; } return(isWebPartDeleted); }
public static void GetWebPartUsage(string webPartType, string webUrl, string outPutDirectory) { string exceptionCommentsInfo1 = string.Empty; ClientContext clientContext = null; bool headerWebPart = false; Web web = null; bool isfound = false; string webPartUsageFileName = outPutDirectory + "\\" + Constants.WEBPART_USAGE_ENTITY_FILENAME + timeStamp + Constants.CSVExtension; try { Logger.LogInfoMessage("[GetWebPartUsage] Finding WebPartUsage details for Web Part: " + webPartType + " in Web: " + webUrl); using (ClientContext userContext = Helper.CreateAuthenticatedUserContext(Program.AdminDomain, Program.AdminUsername, Program.AdminPassword, webUrl)) { web = userContext.Web; userContext.Load(web); userContext.ExecuteQuery(); clientContext = userContext; WebPartUsageEntity webPartUsageEntity = null; List <WebPartUsageEntity> webPartUsage = new List <WebPartUsageEntity>(); //Prepare Exception Comments exceptionCommentsInfo1 = "Web Url: " + clientContext.Web.Url + ", Web Part Type: " + webPartType; if (clientContext != null) { List list = AddWebPart.GetPageList(ref clientContext); if (list != null) { var items = list.GetItems(CamlQuery.CreateAllItemsQuery()); //make sure to include the File on each Item fetched clientContext.Load(items, i => i.Include( item => item.File, item => item["EncodedAbsUrl"])); clientContext.ExecuteQuery(); // Iterate through all available pages in the pages list foreach (var item in items) { try { Microsoft.SharePoint.Client.File page = item.File; String pageUrl = page.ServerRelativeUrl;// item.FieldValues["EncodedAbsUrl"].ToString(); Logger.LogInfoMessage("[GetWebPartUsage] Checking for the Web Part on the Page: " + page.Name); // Requires Full Control permissions on the Web LimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared); clientContext.Load(webPartManager, wpm => wpm.WebParts, wpm => wpm.WebParts.Include( wp => wp.WebPart.Hidden, wp => wp.WebPart.IsClosed, wp => wp.WebPart.Properties, wp => wp.WebPart.Subtitle, wp => wp.WebPart.Title, wp => wp.WebPart.TitleUrl, wp => wp.WebPart.ZoneIndex)); clientContext.ExecuteQuery(); foreach (WebPartDefinition webPartDefinition in webPartManager.WebParts) { Microsoft.SharePoint.Client.WebParts.WebPart webPart = webPartDefinition.WebPart; string webPartPropertiesXml = GetWebPartPropertiesServiceCall(clientContext, webPartDefinition.Id.ToString(), pageUrl); string WebPartTypeName = string.Empty; if (webPartPropertiesXml.Contains("WebPart/v2")) { XmlDataDocument xmldoc = new XmlDataDocument(); xmldoc.LoadXml(webPartPropertiesXml); WebPartTypeName = xmldoc.DocumentElement.GetElementsByTagName("TypeName").Item(0).FirstChild.Value; xmldoc = null; } else { webParts webPartProp = null; byte[] byteArray = Encoding.UTF8.GetBytes(webPartPropertiesXml); using (MemoryStream stream = new MemoryStream(byteArray)) { StreamReader streamReader = new StreamReader(stream); System.Xml.XmlReader reader = new XmlTextReader(streamReader); XmlSerializer serializer = new XmlSerializer(typeof(webParts)); webPartProp = (webParts)serializer.Deserialize(reader); WebPartTypeName = webPartProp.webPart.metaData.type.name; stream.Flush(); } byteArray = null; } string actualWebPartType = AddWebPart.GetWebPartShortTypeName(WebPartTypeName); // only modify if we find the old web part if (actualWebPartType.Equals(webPartType, StringComparison.CurrentCultureIgnoreCase)) { Logger.LogInfoMessage("[GetWebPartUsage] Found WebPart: " + webPartType + " in Page: " + page.Name); webPartUsageEntity = new WebPartUsageEntity(); webPartUsageEntity.PageUrl = pageUrl; webPartUsageEntity.WebPartID = webPartDefinition.Id.ToString(); webPartUsageEntity.WebURL = webUrl; webPartUsageEntity.WebPartTitle = webPart.Title; webPartUsageEntity.ZoneIndex = webPart.ZoneIndex.ToString(); webPartUsageEntity.WebPartType = actualWebPartType; FileUtility.WriteCsVintoFile(webPartUsageFileName, webPartUsageEntity, ref headerWebPart); isfound = true; } } } catch (Exception ex) { ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, webUrl, "WebPartUsage", ex.Message, ex.ToString(), "GetWebPartUsage()", ex.GetType().ToString(), exceptionCommentsInfo1); Logger.LogErrorMessage("[GetWebPartUsage] Exception Message: " + ex.Message + ", Exception Comment: " + exceptionCommentsInfo1); System.Console.ForegroundColor = System.ConsoleColor.Red; Logger.LogErrorMessage("[GetWebPartUsage] Exception Message: " + ex.Message); System.Console.ResetColor(); } } } } //Default Pages GetWebPartUsage_DefaultPages(webPartType, clientContext, outPutDirectory); //Default Pages if (isfound) { Logger.LogSuccessMessage("[GetWebPartUsage] WebPart Usage is exported to the file " + webPartUsageFileName, true); } } } catch (Exception ex) { ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, webUrl, "WebPartUsage", ex.Message, ex.ToString(), "GetWebPartUsage()", ex.GetType().ToString(), exceptionCommentsInfo1); Logger.LogErrorMessage("[GetWebPartUsage] Exception Message: " + ex.Message + ", Exception Comment: " + exceptionCommentsInfo1); System.Console.ForegroundColor = System.ConsoleColor.Red; Logger.LogErrorMessage("[GetWebPartUsage] Exception Message: " + ex.Message); System.Console.ResetColor(); } }
public static void GetWebPartUsage_DefaultPages(string webPartType, ClientContext clientContext, string outPutDirectory) { ExceptionCsv.WebUrl = clientContext.Web.Url; string exceptionCommentsInfo1 = string.Empty; string webPartUsageFileName = outPutDirectory + "\\" + Constants.WEBPART_USAGE_ENTITY_FILENAME + timeStamp + Constants.CSVExtension; bool headerWebPart = false; bool isfound = false; try { string webUrl = clientContext.Web.Url; Logger.LogInfoMessage("[START][GetWebPartUsage_DefaultPages]"); Logger.LogInfoMessage("[GetWebPartUsage_DefaultPages] Finding WebPartUsage details for Web Part: " + webPartType + " in Web: " + webUrl); WebPartUsageEntity webPartUsageEntity = null; List <WebPartUsageEntity> webPartUsage = new List <WebPartUsageEntity>(); if (clientContext != null) { clientContext.Load(clientContext.Web.RootFolder.Files); clientContext.ExecuteQuery(); foreach (Microsoft.SharePoint.Client.File page in clientContext.Web.RootFolder.Files) { exceptionCommentsInfo1 = "Web Url: " + clientContext.Web.Url + ", Web Part Type: " + webPartType + ", PageTitle: " + page.ServerRelativeUrl; try { if (Path.GetExtension(page.ServerRelativeUrl).Equals(".aspx", StringComparison.CurrentCultureIgnoreCase)) { String pageUrl = page.ServerRelativeUrl; Logger.LogInfoMessage("[GetWebPartUsage_DefaultPages] Checking for the Web Part on the Page: " + page.Name); // Requires Full Control permissions on the Web LimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared); clientContext.Load(webPartManager, wpm => wpm.WebParts, wpm => wpm.WebParts.Include( wp => wp.WebPart.Hidden, wp => wp.WebPart.IsClosed, wp => wp.WebPart.Properties, wp => wp.WebPart.Subtitle, wp => wp.WebPart.Title, wp => wp.WebPart.TitleUrl, wp => wp.WebPart.ZoneIndex)); clientContext.ExecuteQuery(); foreach (WebPartDefinition webPartDefinition in webPartManager.WebParts) { Microsoft.SharePoint.Client.WebParts.WebPart webPart = webPartDefinition.WebPart; string webPartPropertiesXml = GetWebPartPropertiesServiceCall(clientContext, webPartDefinition.Id.ToString(), pageUrl); string WebPartTypeName = string.Empty; if (webPartPropertiesXml.Contains("WebPart/v2")) { XmlDataDocument xmldoc = new XmlDataDocument(); xmldoc.LoadXml(webPartPropertiesXml); WebPartTypeName = xmldoc.DocumentElement.GetElementsByTagName("TypeName").Item(0).FirstChild.Value; xmldoc = null; } else { webParts webPartProp = null; byte[] byteArray = Encoding.UTF8.GetBytes(webPartPropertiesXml); using (MemoryStream stream = new MemoryStream(byteArray)) { StreamReader streamReader = new StreamReader(stream); System.Xml.XmlReader reader = new XmlTextReader(streamReader); XmlSerializer serializer = new XmlSerializer(typeof(webParts)); webPartProp = (webParts)serializer.Deserialize(reader); WebPartTypeName = webPartProp.webPart.metaData.type.name; stream.Flush(); } byteArray = null; } string actualWebPartType = AddWebPart.GetWebPartShortTypeName(WebPartTypeName); // only modify if we find the old web part if (actualWebPartType.Equals(webPartType, StringComparison.CurrentCultureIgnoreCase)) { Logger.LogInfoMessage("[GetWebPartUsage_DefaultPages] Found WebPart: " + webPartType + " in Page: " + page.Name + ", " + page.ServerRelativeUrl); webPartUsageEntity = new WebPartUsageEntity(); webPartUsageEntity.PageUrl = pageUrl; webPartUsageEntity.WebPartID = webPartDefinition.Id.ToString(); webPartUsageEntity.WebURL = webUrl; webPartUsageEntity.WebPartTitle = webPart.Title; webPartUsageEntity.ZoneIndex = webPart.ZoneIndex.ToString(); webPartUsageEntity.WebPartType = actualWebPartType; FileUtility.WriteCsVintoFile(webPartUsageFileName, webPartUsageEntity, ref headerWebPart); isfound = true; } } } } catch (Exception ex) { ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, webUrl, "WebPartUsage", ex.Message, ex.ToString(), "GetWebPartUsage_DefaultPages()", ex.GetType().ToString(), exceptionCommentsInfo1); Logger.LogErrorMessage("[GetWebPartUsage_DefaultPages] Exception Message: " + ex.Message + ", Exception Comment: " + exceptionCommentsInfo1); System.Console.ForegroundColor = System.ConsoleColor.Red; Logger.LogErrorMessage("[GetWebPartUsage_DefaultPages] Exception Message: " + ex.Message); System.Console.ResetColor(); } } } if (isfound) { Logger.LogInfoMessage("[GetWebPartUsage_DefaultPages] Default Pages WebPart Usage is exported to the file " + webPartUsageFileName); } Logger.LogInfoMessage("[END][GetWebPartUsage_DefaultPages]"); } catch (Exception ex) { System.Console.ForegroundColor = System.ConsoleColor.Red; Logger.LogErrorMessage("[GetWebPartUsage_DefaultPages] Exception Message: " + ex.Message); System.Console.ResetColor(); ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, ExceptionCsv.WebUrl, "WebPartUsage", ex.Message, ex.ToString(), "GetWebPartUsage_DefaultPages()", ex.GetType().ToString(), exceptionCommentsInfo1); } }
public bool Validate(PageCollection sourcePages, Microsoft.SharePoint.Client.ClientContext ctx) { int scount = 0; int tcount = 0; Web web = ctx.Web; ctx.Load(web, w => w.Url, w => w.ServerRelativeUrl); ctx.ExecuteQueryRetry(); foreach (var sourcePage in sourcePages) { string pageUrl = sourcePage.Url.ToString(); pageUrl = pageUrl.Replace("{site}", web.ServerRelativeUrl); Microsoft.SharePoint.Client.File file = web.GetFileByServerRelativeUrl(pageUrl); ctx.Load(file, page => page.ListItemAllFields, page => page.ListItemAllFields.RoleAssignments.Include(roleAsg => roleAsg.Member, roleAsg => roleAsg.RoleDefinitionBindings.Include(roleDef => roleDef.Name))); ctx.ExecuteQueryRetry(); if (file != null) { #region Page - Fields if (sourcePage.Fields.Count > 0) { scount = 0; tcount = 0; string sourceWikifield = sourcePage.Fields["WikiField"].ToString(); string targetwikiField = (string)file.ListItemAllFields["WikiField"]; if (sourceWikifield.Trim() != RemoveHTMLTags(targetwikiField).Trim()) { return(false); } } #endregion #region Page - Webparts if (!ctx.Web.IsNoScriptSite() && sourcePage.WebParts.Count > 0) { LimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared); ctx.Load(wpm.WebParts, wps => wps.Include(wp => wp.WebPart.Title, wp => wp.WebPart.Properties)); ctx.ExecuteQueryRetry(); if (wpm.WebParts.Count > 0) { foreach (var spwp in sourcePage.WebParts) { scount++; foreach (WebPartDefinition wpd in wpm.WebParts) { if (spwp.Title == wpd.WebPart.Title) { tcount++; //Page - Webpart Properties Microsoft.SharePoint.Client.WebParts.WebPart wp = wpd.WebPart; var isWebPropertiesMatch = CSOMWebPartPropertiesValidation(spwp.Contents, wp.Properties); if (!isWebPropertiesMatch) { return(false); } } else { return(false); } } } if (scount != tcount) { return(false); } } else { return(false); } } #endregion #region Page - Security scount = 0; tcount = 0; if (sourcePage.Security != null && file.ListItemAllFields.RoleAssignments.Count > 0) { bool securityResult = ValidateSecurityCSOM(ctx, sourcePage.Security, file.ListItemAllFields); if (!securityResult) { return(false); } } #endregion } else { return(false); } } return(true); }