Share via


CbqQueryCache Class

Caches the CbqQueryVersionInfo for a specified Content Query Web Part. This class cannot be inherited.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Publishing.CbqQueryCache

Namespace:  Microsoft.SharePoint.Publishing
Assembly:  Microsoft.SharePoint.Publishing (in Microsoft.SharePoint.Publishing.dll)

Syntax

'Declaration
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public NotInheritable Class CbqQueryCache
'Usage
Dim instance As CbqQueryCache
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public sealed class CbqQueryCache

Remarks

Use the FetchCbqQueryInfoFromCache() method to instruct the Content Query Web Part to get the site, Web, page, or Web Part GUID and store it in the UserQueryVersionInfo property of this CbqQueryCache instance.

Examples

using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Publishing.WebControls;
using Microsoft.SharePoint.Publishing;

namespace CodeSampleNamespace
{
    public class CodeSample : LayoutsPageBase
    {
        protected System.Web.UI.WebControls.Panel panel;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Example 1
            {
                string webUrl = Request.QueryString["webUrl"];
                Guid pageGuid = new Guid(Request.QueryString["pageUniqueId"]);
                Guid webpartGuid = new Guid(Request.QueryString["wpStorageKey"]);
                int cacheFeedTime = 800;

                // Retrieve Content Query Web Part information from the  cache.
                CbqQueryCache queryCache = new CbqQueryCache(this.Page.Cache, webUrl, pageGuid, webpartGuid, cacheFeedTime);

                // Execute Query
                CbqQueryVersionInfo userCbqQuery = queryCache.UserQueryVersionInfo;
                CrossListQueryCache xlqCache = new CrossListQueryCache(userCbqQuery.VersionCrossListQueryInfo);
                DataTable data =  xlqCache.GetSiteData(SPContext.Current.Site);

                // Perform ProcessData delegate that is specified on this web part
                if (userCbqQuery.ProcessDataDelegate != null)
                {
                    data = userCbqQuery.ProcessDataDelegate(data);
                }

                // Create a new Content Query Web Part.
                ContentByQueryWebPart cbq = new ContentByQueryWebPart();

                // Store the data in the new Content By Query Web Part.
                cbq.Data = data;

                panel.Controls.Add(cbq);
            }


            // Example 2
            {
                string webUrl = Request.QueryString["webUrl"];
                Guid pageGuid = new Guid(Request.QueryString["pageUniqueId"]);
                Guid webpartGuid = new Guid(Request.QueryString["wpStorageKey"]);
                int cacheFeedTime = 800;

                // Retrieve Content By Query Web Part information from the cache.
                CbqQueryCache query = new CbqQueryCache(this.Page.Cache, webUrl, pageGuid, webpartGuid, cacheFeedTime);

                // Retrieve Information about the current user query.
                CbqQueryVersionInfo userCbqQuery = query.UserQueryVersionInfo;

                // Set Query Information on a new CBQ web part
                ContentByQueryWebPart cbq = new ContentByQueryWebPart();
                cbq.FeedTitle = userCbqQuery.FeedTitle;
                cbq.FeedDescription = userCbqQuery.FeedDescription;
                cbq.UseCopyUtil = userCbqQuery.UseCopyUtil;
                cbq.DataColumnRenames = userCbqQuery.DataColumnRenames;
                cbq.ProcessDataDelegate = userCbqQuery.ProcessDataDelegate;

                CrossListQueryInfo crossListInfo = userCbqQuery.VersionCrossListQueryInfo;
                cbq.QueryOverride = crossListInfo.Query;
                cbq.WebsOverride = crossListInfo.Webs;
                cbq.ViewFieldsOverride = crossListInfo.ViewFields;
                cbq.UseCache = crossListInfo.UseCache;
                cbq.WebUrl = crossListInfo.WebUrl;

                if (crossListInfo.RowLimit == uint.MaxValue)
                {
                    cbq.ItemLimit = -1;
                }
                else
                {
                    cbq.ItemLimit = (int)crossListInfo.RowLimit;
                }

                // The List Override takes a string.Format. Therefore, you must encode { and } before passing it back.
                string listOverride = crossListInfo.Lists;
                listOverride = listOverride.Replace("{", "{{");
                listOverride = listOverride.Replace("}", "}}");
                cbq.ListsOverride = listOverride;

                // Set items that may affect Audience Targetting.
                cbq.ShowUntargetedItems = crossListInfo.ShowUntargetedItems;
                cbq.FilterByAudience = crossListInfo.FilterByAudience;
                if (crossListInfo.GroupByAudience)
                {
                    cbq.GroupBy = "{" + FieldId.AudienceTargeting.ToString() + "}";
                }
                if (crossListInfo.GroupByAscending)
                {
                    cbq.GroupByDirection = ContentByQueryWebPart.SortDirection.Asc;
                }

                panel.Controls.Add(cbq);
            }

      }

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

CbqQueryCache Members

Microsoft.SharePoint.Publishing Namespace

CbqQueryCache

GetSiteData

UserQueryVersionInfo