Partager via


Extending CQWP with Toolpart- Adding functionality like PageFilter, Pagination - Part 1

Content Query Web Part (CQWP) is one of the most usable OOTB web parts in SharePoint. It has been hugely used in publishing sites for aggregating data and presenting through XSLT. As everyone knows that CQWP is most powerful webpart for aggreating data in SharePoint, still we need extra functionality like pagination, page filter which is not in OOTB. This post share little information about how can we achieve through extending the CQWP.

Extending CQWP

These are more important steps while extending user friendly CQWP.

  • Creating Tool Part for CQWP
  • Processing data
  • Passing data to XSLT

Creating Tool Part for CQWP

When extending new functionality to CQWP, new functionality should be easily configurable thourgh toolpart for Content Authors. ContentByQueryWebPart is public class which can be extended , but ContentByQueryToolPart is a sealed class which could not be extended. But it can be achived by adding our ToolPart class to GetToolParts() override method and returning the ToolPart[] arrary with our toolpart class.

  1. Extend CQWP and make MyCQWP : ContentByQueryWebPart
  2. Create new MyToolPart class inherting from Microsoft.SharePoint.WebPartPages.ToolPart. (See this how to create tool part in MSDN)
  3. Add your controls in tool part class(Example Adding "More Link" text and "More Link" Url, or Filtering based on some field....)
  4. Add this Tool part into MyCQWP by adding into tool part array.      

       public override Microsoft.SharePoint.WebPartPages.ToolPart[] GetToolParts()        {            List<ToolPart> parts = new List<ToolPart>();            parts.AddRange(base.GetToolParts());            MyToolPart myToolPart = new MyToolPart ();            parts.Add(myToolPart);            return parts.ToArray();        }

 Processing data

Next step is processing the data in CQWP with additionaly fuctionality which we provided in Tool part. CQWP give a delegate where it gives the resultset as a DataTable, where we need use for our custom functionality. We should register our delegate in OnInit method
 

 protected override void OnInit(EventArgs e
 {
base.OnInit(e);
this.ProcessDataDelegate += this.ProcessDataItems;
}

 

After registering the delegate our custom logic should go to this method

public DataTable ProcessDataItems(DataTable data) {////Process the DataTable....try{//// put your custom functionality for processing DataTable like filtering, massaging data itmes}catch{}return data;}

Now CQWP will display the massaged data in page.

Passing data to XSLT

 

 

 We have achieved the CQWP to display our own data. Now next question comes, how to display our own text inside CQWP like "More Link" and "More  Link Url", which we have configured ToolPart. For that we shoudl touch the "

protected override void ModifyXsltArgumentList(ArgumentClassWrapper argList) function, where we have to add our params into it.

 protected override void ModifyXsltArgumentList(ArgumentClassWrapper argList) {
base.ModifyXsltArgumentList(argList);
if (argList != null) 
{
argList.AddParameter("MoreUrl", string.Empty, this.MoreUrl);
argList.AddParameter("MoreText", string.Empty, this.MoreText);
}
}

 

 After adding this, we have to handle it in XSLT file, Open the "ContentQueryMain.xsl" in and add your params

<xsl:param name="MoreUrl" />
<xsl:param name="MoreText" />

After this you can use this variable as like other variable in CQWP. I will explain how can we achieve Pagination in CQWP in the next post..

Happy coding...

Thanks