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);
}
}