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.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Publishing;
using System.IO;

public partial class CBQCachePage : System.Web.UI.Page
{

    protected override void OnLoad(EventArgs e)
    {
        // For this to work the page has to have the "web", "page", and "wp" query parameters passed to it
        base.OnLoad(e);
        string webUrl = Request.QueryString["web"];
        Guid pageGuid = new Guid(Request.QueryString["page"]);
        Guid webpartGuid = new Guid(Request.QueryString["wp"]);

        CbqQueryVersionInfo queryVersionInfo = null;
        try
        {
            // Try and create a new CbqQueryCache and get the Query from it and display it on the page
            CbqQueryCache queryCache = new CbqQueryCache(this.Page.Cache, webUrl, pageGuid, webpartGuid, 120, Request.Url.Query);
            this.queryLabel.Text = queryCache.UserQueryVersionInfo.VersionCrossListQueryInfo.Query;
        }
        catch (InvalidOperationException)
        {
            queryVersionInfo = null;
        }
        catch (FileNotFoundException)
        {
            queryVersionInfo = null;
        }
        catch (ArgumentOutOfRangeException)
        {
            queryVersionInfo = null;
        }

        // User either has no rights
        // Or the web part does not exist
        if (queryVersionInfo == null)
        {
            Response.StatusCode = 404;
            Response.End();
        }
    }
}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);}      }
Imports SystemImports System.GlobalizationImports System.WebImports System.Web.UIImports System.Web.UI.WebControlsImports System.DataImports System.Collections.GenericImports Microsoft.SharePointImports Microsoft.SharePoint.WebControlsImports Microsoft.SharePoint.WebPartPagesImports Microsoft.SharePoint.UtilitiesImports Microsoft.SharePoint.ApplicationPagesImports Microsoft.SharePoint.Publishing.WebControlsImports Microsoft.SharePoint.PublishingNamespace CodeSampleNamespace    Public Class CodeSample        Inherits LayoutsPageBase        Protected panel As System.Web.UI.WebControls.Panel        Protected Overrides Sub OnLoad(ByVal e As EventArgs)MyBase.OnLoad(e)' Example 1    Dim webUrl As String = Request.QueryString("webUrl")    Dim pageGuid As New Guid(Request.QueryString("pageUniqueId"))    Dim webpartGuid As New Guid(Request.QueryString("wpStorageKey"))    Dim cacheFeedTime As Integer = 800    ' Retrieve Content Query Web Part information from the  cache.    Dim queryCache As New CbqQueryCache(Me.Page.Cache, webUrl, pageGuid, webpartGuid, cacheFeedTime)    ' Execute Query    Dim userCbqQuery As CbqQueryVersionInfo = queryCache.UserQueryVersionInfo    Dim xlqCache As New CrossListQueryCache(userCbqQuery.VersionCrossListQueryInfo)    Dim data As DataTable = xlqCache.GetSiteData(SPContext.Current.Site)    ' Perform ProcessData delegate that is specified on this web part    If userCbqQuery.ProcessDataDelegate IsNot Nothing Then        data = userCbqQuery.ProcessDataDelegate(data)    End If    ' Create a new Content Query Web Part.    Dim cbq As New ContentByQueryWebPart()    ' Store the data in the new Content By Query Web Part.    cbq.Data = data    panel.Controls.Add(cbq)' Example 2    Dim webUrl As String = Request.QueryString("webUrl")    Dim pageGuid As New Guid(Request.QueryString("pageUniqueId"))    Dim webpartGuid As New Guid(Request.QueryString("wpStorageKey"))    Dim cacheFeedTime As Integer = 800    ' Retrieve Content By Query Web Part information from the cache.    Dim query As New CbqQueryCache(Me.Page.Cache, webUrl, pageGuid, webpartGuid, cacheFeedTime)    ' Retrieve Information about the current user query.    Dim userCbqQuery As CbqQueryVersionInfo = query.UserQueryVersionInfo    ' Set Query Information on a new CBQ web part    Dim cbq As New ContentByQueryWebPart()    cbq.FeedTitle = userCbqQuery.FeedTitle    cbq.FeedDescription = userCbqQuery.FeedDescription    cbq.UseCopyUtil = userCbqQuery.UseCopyUtil    cbq.DataColumnRenames = userCbqQuery.DataColumnRenames    cbq.ProcessDataDelegate = userCbqQuery.ProcessDataDelegate    Dim crossListInfo As CrossListQueryInfo = 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 = UInteger.MaxValue Then        cbq.ItemLimit = -1    Else        cbq.ItemLimit = CInt(Fix(crossListInfo.RowLimit))    End If    ' The List Override takes a string.Format. Therefore, you must encode { and } before passing it back.    Dim listOverride As String = 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 Then        cbq.GroupBy = "{" & FieldId.AudienceTargeting.ToString() & "}"    End If    If crossListInfo.GroupByAscending Then        cbq.GroupByDirection = ContentByQueryWebPart.SortDirection.Asc    End If    panel.Controls.Add(cbq)        End Sub    End ClassEnd Namespace

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

#ctor(Cache, String, Guid, Guid, Int32)

GetSiteData

UserQueryVersionInfo