/// <summary> /// Applies the list type changes. /// </summary> /// <param name="web">The web.</param> /// <param name="cqwp">The content query web part.</param> /// <param name="listType">Type of the list.</param> private static void ApplyListTypeChanges(SPWeb web, ContentByQueryWebPart cqwp, string listType) { if (listType == null) { return; } using (SPWeb rootWeb = web.Site.RootWeb) { SPListTemplateCollection listTemplates = rootWeb.ListTemplates; SPListTemplate template = listTemplates[listType]; if (template == null) { throw new SPException("List template (type) not found."); } cqwp.BaseType = string.Empty; cqwp.ServerTemplate = Convert.ToString((int)template.Type, CultureInfo.InvariantCulture); bool isGenericList = template.BaseType == SPBaseType.GenericList; bool isIssueList = template.BaseType == SPBaseType.Issue; bool isLinkList = template.Type == SPListTemplateType.Links; cqwp.UseCopyUtil = !isGenericList ? isIssueList : (isLinkList ? false : true); } }
/// <summary> /// Adjusts the web part. /// </summary> /// <param name="cqwp">The CQWP.</param> /// <param name="list">The list.</param> /// <param name="web">The web.</param> internal static void AdjustWebPart(ContentByQueryWebPart cqwp, SPList list, SPWeb web) { if (list.ContentTypes["Listing"] != null) { // The list is a special list - it was upgraded from v2 and corresponds // to the grouped listings web part so we need to set some additional // properties that cannot be set via the browser. ApplyListTypeChanges(web, cqwp, "Links"); cqwp.AdditionalGroupAndSortFields = "Modified,Modified;Created,Created"; cqwp.DataColumnRenames = "SummaryTitle,Title;Comments,Description;URL,LinkUrl;SummaryImage,ImageUrl"; cqwp.SortByFieldType = "Number"; cqwp.ChromeType = PartChromeType.None; cqwp.CommonViewFields = "SummaryTitle,Text;Comments,Note;URL,URL;SummaryImage,URL;SummaryIcon,URL;SummaryType,Integer;_TargetItemID,Note"; cqwp.FilterType1 = "ModStat"; cqwp.Xsl = "<xsl:stylesheet xmlns:x=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:cmswrt=\"http://schemas.microsoft.com/WebPart/v3/Publishing/runtime\" exclude-result-prefixes=\"xsl cmswrt x\" > <xsl:import href=\"/Style Library/XSL Style Sheets/Header.xsl\" /> <xsl:import href=\"/Style Library/XSL Style Sheets/ItemStyle.xsl\" /> <xsl:import href=\"/Style Library/XSL Style Sheets/ContentQueryMain.xsl\" /> </xsl:stylesheet>"; cqwp.FilterValue1 = "Approved"; cqwp.ShowUntargetedItems = false; cqwp.FilterField1 = list.Fields["Approval Status"].Id.ToString(); cqwp.Filter1ChainingOperator = ContentByQueryWebPart.FilterChainingOperator.And; cqwp.ItemLimit = -1; cqwp.SortBy = list.Fields["Order"].Id.ToString(); cqwp.SortByDirection = ContentByQueryWebPart.SortDirection.Asc; cqwp.GroupByDirection = ContentByQueryWebPart.SortDirection.Asc; cqwp.Description = "Show listings in the portal."; cqwp.GroupBy = "SummaryGroup"; cqwp.Hidden = false; } using (SPWeb parentWeb = list.ParentWeb) cqwp.WebUrl = parentWeb.ServerRelativeUrl; cqwp.ListGuid = list.ID.ToString(); cqwp.ListName = list.Title; }
public static void ExampleOne(string siteURL) { using (SPSite site = new SPSite(siteURL)) { //Get a CbqQueryVersionInfo object from a ContentByQueryWebPart and use it to get a CrossListQueryInfo ContentByQueryWebPart cbq = new ContentByQueryWebPart(); CbqQueryVersionInfo versionInfo = cbq.BuildCbqQueryVersionInfo(); CrossListQueryInfo queryInfo = versionInfo.VersionCrossListQueryInfo; // Create a new CrossListQueryCache object with the queryInfo we just got CrossListQueryCache crossListCache = new CrossListQueryCache(queryInfo); SiteDataResults results = crossListCache.GetSiteDataResults(site, false); } }
/// <summary> /// Replaces the content of a <see cref="ContentByQueryWebPart"/> web part. /// </summary> /// <param name="web">The web that the file belongs to.</param> /// <param name="file">The file that the web part is associated with.</param> /// <param name="settings">The settings object containing user provided parameters.</param> /// <param name="wp">The web part whose content will be replaced.</param> /// <param name="regex">The regular expression object which contains the search pattern.</param> /// <param name="manager">The web part manager. This value may get updated during this method call.</param> /// <param name="wasCheckedOut">if set to <c>true</c> then the was checked out prior to this method being called.</param> /// <param name="modified">if set to <c>true</c> then the web part was modified as a result of this method being called.</param> /// <returns>The modified web part. This returned web part is what must be used when saving any changes.</returns> internal static WebPart ReplaceValues(SPWeb web, SPFile file, Settings settings, ContentByQueryWebPart wp, Regex regex, ref SPLimitedWebPartManager manager, ref bool wasCheckedOut, ref bool modified) { if (string.IsNullOrEmpty(wp.ListGuid) && string.IsNullOrEmpty(wp.WebUrl) && string.IsNullOrEmpty(wp.GroupBy)) return wp; bool isListGuidMatch = false; if (!string.IsNullOrEmpty(wp.ListGuid)) isListGuidMatch = regex.IsMatch(wp.ListGuid); bool isWebUrlMatch = false; if (!string.IsNullOrEmpty(wp.WebUrl)) isWebUrlMatch = regex.IsMatch(wp.WebUrl); bool isGroupByMatch = false; if (!string.IsNullOrEmpty(wp.GroupBy)) isGroupByMatch = regex.IsMatch(wp.GroupBy); if (!isListGuidMatch && !isWebUrlMatch && !isGroupByMatch) return wp; string listGuidContent = wp.ListGuid; string webUrlContent = wp.WebUrl; string groupByContent = wp.GroupBy; string listGuidResult = listGuidContent; string webUrlResult = webUrlContent; string groupByResult = groupByContent; if (!string.IsNullOrEmpty(listGuidContent)) listGuidResult = regex.Replace(listGuidContent, settings.ReplaceString); if (!string.IsNullOrEmpty(webUrlContent)) webUrlResult = regex.Replace(webUrlContent, settings.ReplaceString); if (!string.IsNullOrEmpty(groupByContent)) groupByResult = regex.Replace(groupByContent, settings.ReplaceString); if (isListGuidMatch) Logger.Write("Match found: File={0}, WebPart={1}, Replacement={2} => {3}", file.ServerRelativeUrl, wp.Title, listGuidContent, listGuidResult); if (isWebUrlMatch) Logger.Write("Match found: File={0}, WebPart={1}, Replacement={2} => {3}", file.ServerRelativeUrl, wp.Title, webUrlContent, webUrlResult); if (isGroupByMatch) Logger.Write("Match found: File={0}, WebPart={1}, Replacement={2} => {3}", file.ServerRelativeUrl, wp.Title, groupByContent, groupByResult); if (!settings.Test) { if (file.CheckOutType == SPFile.SPCheckOutType.None) { file.CheckOut(); wasCheckedOut = false; } // We need to reset the manager and the web part because a checkout (now or from an earlier call) // could mess things up so safest to just reset every time. manager.Web.Dispose(); // manager.Dispose() does not dispose of the SPWeb object and results in a memory leak. manager.Dispose(); manager = web.GetLimitedWebPartManager(file.Url, PersonalizationScope.Shared); wp.Dispose(); wp = (ContentByQueryWebPart)manager.WebParts[wp.ID]; if (isListGuidMatch) wp.ListGuid = listGuidResult; if (isWebUrlMatch) wp.WebUrl = webUrlResult; if (isGroupByMatch) wp.GroupBy = groupByResult; modified = true; } return wp; }
/// <summary> /// Adjusts the web part. /// </summary> /// <param name="web">The web.</param> /// <param name="wp">The web part.</param> /// <param name="manager">The web part manager.</param> /// <param name="listUrl">The list URL.</param> /// <param name="listType">Type of the list (list template).</param> /// <param name="siteUrl">The site URL.</param> internal static void AdjustWebPart(SPWeb web, WebPart wp, SPLimitedWebPartManager manager, string listUrl, string listType, string siteUrl) { ContentByQueryWebPart cqwp = wp as ContentByQueryWebPart; if (cqwp == null) { throw new SPException("Web part is not a Content Query web part."); } if (listUrl != null) { SPList list = Utilities.GetListFromViewUrl(listUrl); if (list == null) { throw new SPException("Specified List was not found."); } AdjustWebPart(cqwp, list, web); } else if (siteUrl != null) { try { using (SPWeb web2 = web.Site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(siteUrl)]) { if (!web2.Exists) { throw new SPException(string.Format("The web {0} does not exist.", siteUrl)); } } } catch (ArgumentException) { throw new SPException(siteUrl + " either does not exist is is invalid or does not belong to the web part's container site collection."); } catch (SPException) { throw new SPException(siteUrl + " either does not exist is is invalid or does not belong to the web part's container site collection."); } catch (FileNotFoundException) { throw new SPException(siteUrl + " either does not exist is is invalid or does not belong to the web part's container site collection."); } cqwp.WebUrl = siteUrl; cqwp.ListGuid = string.Empty; cqwp.ListName = string.Empty; } else { cqwp.WebUrl = string.Empty; cqwp.ListGuid = string.Empty; } if (listType != null) { ApplyListTypeChanges(web, cqwp, listType); } manager.SaveChanges(cqwp); }
/// <summary> /// Applies the list type changes. /// </summary> /// <param name="web">The web.</param> /// <param name="cqwp">The content query web part.</param> /// <param name="listType">Type of the list.</param> private static void ApplyListTypeChanges(SPWeb web, ContentByQueryWebPart cqwp, string listType) { if (listType == null) return; using (SPWeb rootWeb = web.Site.RootWeb) { SPListTemplateCollection listTemplates = rootWeb.ListTemplates; SPListTemplate template = listTemplates[listType]; if (template == null) throw new SPException("List template (type) not found."); cqwp.BaseType = string.Empty; cqwp.ServerTemplate = Convert.ToString((int)template.Type, CultureInfo.InvariantCulture); bool isGenericList = template.BaseType == SPBaseType.GenericList; bool isIssueList = template.BaseType == SPBaseType.Issue; bool isLinkList = template.Type == SPListTemplateType.Links; cqwp.UseCopyUtil = !isGenericList ? isIssueList : (isLinkList ? false : true); } }