/// <summary> /// Analyses a web for it's blog page usage /// </summary> /// <param name="cc">ClientContext instance used to retrieve blog data</param> /// <returns>Duration of the blog analysis</returns> public override TimeSpan Analyze(ClientContext cc) { try { // Is this a blog site if (cc.Web.WebTemplate.Equals("BLOG", StringComparison.InvariantCultureIgnoreCase)) { var web = cc.Web; base.Analyze(cc); BlogWebScanResult blogWebScanResult = new BlogWebScanResult() { SiteColUrl = this.SiteCollectionUrl, SiteURL = this.SiteUrl, WebRelativeUrl = this.SiteUrl.Replace(this.SiteCollectionUrl, ""), }; // Log used web template if (web.WebTemplate != null) { blogWebScanResult.WebTemplate = $"{web.WebTemplate}#{web.Configuration}"; } // Load additional web properties web.EnsureProperty(p => p.Language); blogWebScanResult.Language = web.Language; // Get the blog page list List blogList = null; var lists = web.GetListsToScan(); if (lists != null) { blogList = lists.Where(p => p.BaseTemplate == (int)ListTemplateType.Posts).FirstOrDefault(); } // Query the blog posts if (blogList != null) { CamlQuery query = CamlQuery.CreateAllItemsQuery(10000, new string[] { "Title", "Body", "NumComments", "PostCategory", "PublishedDate", "Modified", "Created", "Editor", "Author" }); var pages = blogList.GetItems(query); cc.Load(pages); cc.ExecuteQueryRetry(); if (pages != null) { blogWebScanResult.BlogPageCount = pages.Count; blogWebScanResult.LastRecentBlogPageChange = blogList.LastItemUserModifiedDate; blogWebScanResult.LastRecentBlogPagePublish = blogList.LastItemUserModifiedDate; foreach (var page in pages) { string pageUrl = null; try { if (page.FieldValues.ContainsKey(FileRefField) && !String.IsNullOrEmpty(page[FileRefField].ToString())) { pageUrl = page[FileRefField].ToString().ToLower(); } else { //skip page continue; } BlogPageScanResult blogPageScanResult = new BlogPageScanResult() { SiteColUrl = this.SiteCollectionUrl, SiteURL = this.SiteUrl, WebRelativeUrl = blogWebScanResult.WebRelativeUrl, PageRelativeUrl = pageUrl, }; if (page.FieldValues.ContainsKey(FileRefField) && !String.IsNullOrEmpty(page[FileRefField].ToString())) { blogPageScanResult.PageTitle = page["Title"].ToString(); } // Add modified information blogPageScanResult.ModifiedBy = page.LastModifiedBy(); blogPageScanResult.ModifiedAt = page.LastModifiedDateTime(); blogPageScanResult.PublishedDate = page.LastPublishedDateTime(); if (blogPageScanResult.ModifiedAt > blogWebScanResult.LastRecentBlogPageChange) { blogWebScanResult.LastRecentBlogPageChange = blogPageScanResult.ModifiedAt; } if (blogPageScanResult.PublishedDate > blogWebScanResult.LastRecentBlogPagePublish) { blogWebScanResult.LastRecentBlogPagePublish = blogPageScanResult.PublishedDate; } if (!this.ScanJob.BlogPageScanResults.TryAdd($"blogScanResult.PageURL.{Guid.NewGuid()}", blogPageScanResult)) { ScanError error = new ScanError() { Error = $"Could not add blog page scan result for {blogPageScanResult.SiteColUrl}", SiteColUrl = this.SiteCollectionUrl, SiteURL = this.SiteUrl, Field1 = "BlogPageAnalyzer", }; this.ScanJob.ScanErrors.Push(error); } } catch (Exception ex) { ScanError error = new ScanError() { Error = ex.Message, SiteColUrl = this.SiteCollectionUrl, SiteURL = this.SiteUrl, Field1 = "BlogPageAnalyzer", Field2 = ex.StackTrace, Field3 = pageUrl }; this.ScanJob.ScanErrors.Push(error); } } } } if (!this.ScanJob.BlogWebScanResults.TryAdd($"blogScanResult.WebURL.{Guid.NewGuid()}", blogWebScanResult)) { ScanError error = new ScanError() { Error = $"Could not add blog web scan result for {blogWebScanResult.SiteColUrl}", SiteColUrl = this.SiteCollectionUrl, SiteURL = this.SiteUrl, Field1 = "BlogWebAnalyzer", }; this.ScanJob.ScanErrors.Push(error); } } } catch (Exception ex) { ScanError error = new ScanError() { Error = ex.Message, SiteColUrl = this.SiteCollectionUrl, SiteURL = this.SiteUrl, Field1 = "BlogPageAnalyzerMainLoop", Field2 = ex.StackTrace }; this.ScanJob.ScanErrors.Push(error); } finally { this.StopTime = DateTime.Now; } // return the duration of this scan return(new TimeSpan((this.StopTime.Subtract(this.StartTime).Ticks))); }
public override TimeSpan Analyze(ClientContext cc) { // Get site usage information List <string> propertiesToRetrieve = new List <string> { "LastModifiedTime", "ModifiedBy", "DetectedLanguage", "SPWebUrl", "Path", "Title", "ViewsRecent", "ViewsRecentUniqueUsers", "ViewsLifeTime", "ViewsLifeTimeUniqueUsers" }; Uri rootSite = new Uri(this.SiteCollectionUrl); string pathFilter = $"{rootSite.Scheme}://{rootSite.DnsSafeHost}/portals/personal/*"; Dictionary <string, BlogWebScanResult> tempWebResults = new Dictionary <string, BlogWebScanResult>(); var results = this.ScanJob.Search(cc.Web, $"path:{pathFilter} AND ContentTypeId:\"0x010100DA3A7E6E3DB34DFF8FDEDE1F4EBAF95D*\"", propertiesToRetrieve); if (results != null && results.Count > 0) { foreach (var result in results) { string url = result["SPWebUrl"]?.ToLower().Replace($"{rootSite.Scheme}://{rootSite.DnsSafeHost}".ToLower(), ""); DateTime lastModified = DateTime.MinValue; if (result["LastModifiedTime"] != null) { DateTime.TryParse(result["LastModifiedTime"], out lastModified); } BlogWebScanResult scanResult = null; BlogPageScanResult blogPageScanResult; if (tempWebResults.ContainsKey(url)) { // Increase page counter tempWebResults[url].BlogPageCount += 1; // Build page record blogPageScanResult = AddBlogPageResult($"{rootSite.Scheme}://{rootSite.DnsSafeHost}".ToLower(), url, result, lastModified); } else { scanResult = new BlogWebScanResult { SiteColUrl = result["SPWebUrl"], SiteURL = result["SPWebUrl"], WebRelativeUrl = url, WebTemplate = "POINTPUBLISHINGPERSONAL#0", BlogType = BlogType.Delve, BlogPageCount = 1, LastRecentBlogPageChange = lastModified, LastRecentBlogPagePublish = lastModified, Language = 1033 }; tempWebResults.Add(url, scanResult); // Build page record blogPageScanResult = AddBlogPageResult($"{rootSite.Scheme}://{rootSite.DnsSafeHost}".ToLower(), url, result, lastModified); } if (blogPageScanResult != null) { if (!this.ScanJob.BlogPageScanResults.TryAdd($"blogScanResult.PageURL.{Guid.NewGuid()}", blogPageScanResult)) { ScanError error = new ScanError() { Error = $"Could not add delve blog page scan result for {blogPageScanResult.SiteColUrl}", SiteColUrl = this.SiteCollectionUrl, SiteURL = this.SiteUrl, Field1 = "DelveBlogPageAnalyzer", }; this.ScanJob.ScanErrors.Push(error); } } } // Copy the temp scan results to the actual structure foreach (var blogWebResult in tempWebResults) { if (!this.ScanJob.BlogWebScanResults.TryAdd($"blogScanResult.WebURL.{Guid.NewGuid()}", blogWebResult.Value)) { ScanError error = new ScanError() { Error = $"Could not add delve blog web scan result for {blogWebResult.Value.SiteColUrl}", SiteColUrl = this.SiteCollectionUrl, SiteURL = this.SiteUrl, Field1 = "DelveBlogWebAnalyzer", }; this.ScanJob.ScanErrors.Push(error); } } } return(base.Analyze(cc)); }