locked
Add filter to CQWP using query string. RRS feed

  • Question

  • I have a CQWP (MOSS 2007) and wish to add filter to the CQWP using what is in my query string.

    Suppose my query string is like http://<<sitename>>/mypage.aspx?ID=1

    THen i want to add filter to my CQWP based on what is currently present in my query string.


    I am using MOSS 2007
    Wednesday, May 15, 2013 7:21 AM

Answers

  • I finally managed to  add filter to CQWP  on basis of query string in "MOSS 2007" .

    The possible way to do this is through object model .

    public class MYCQWP : ContentByQueryWebpart   //inherit from  ContentByQueryWebpart class

    {

                            

     protected override void OnLoad(EventArgs e)  //inside its OnLoad
            {

                if (string.IsNullOrEmpty(this.Page.Request.QueryString["ID"]))
                    return;


                this.Filter1ChainingOperator = FilterChainingOperator.Or;

                //  get the filtering field by name .In my case its 
                string filterFieldName = this.Page.Request.QueryString["ID"].ToString();

                // since the field name isn't suffucient for the CQWP, get the field's GUID
                Guid filterFieldID = Microsoft.SharePoint.SPContext.Current.Item.Fields["ID"].Id;
                // get the filtering field value
                string filterFieldValue = this.Page.Request.QueryString["ID"].ToString();

                // set the filtering info
                this.FilterField1 = filterFieldID.ToString();
                this.FilterValue1 = filterFieldValue;
            }

    }

    ->>Deploy and add this webpart , and you'll see inside the Toolpane the filter automatically set to the value from the Query string.

    For eg, if your Query string is   something.aspx?ID=1

    then the filter will be set to ID is equal to 1.

    Regards,
    Ankit

    Friday, May 17, 2013 11:51 AM

All replies

  • Hi Ankit,

    PageQueryString allows to configure the Content Query Webpart dynamically using the query string in the url.

    For example:

    To configure the CQWP to retrieve different categories of announcements based on the query string value.

    Add a CQWP to the page and configure Query section as follows :

    • Source: Show items from all sites in this site collection
    • List Type: Announcements
    • Content Type:Show items of this content type group : My Content Types
    • Content Type:Show items of this content type: Company Announcements
    • In additional filters Select "Announcement Category" filed is equal to [PageQueryString : Category]

    In your case you can use [PageQueryString : ID].

    Hope this helps.

    -----------

    Regards

    Ankur

    • Proposed as answer by RajKumar26 Thursday, May 16, 2013 6:47 AM
    Wednesday, May 15, 2013 9:59 AM
  • Hi Ankit0109,

    You can try Ankur’s suggestion. This will allow the content query web part to get filter from query string. If have any more questions, please feel free to ask.

    Thanks,

    Entan Ming

    TechNet Subscriber Support in forum

    If you have any feedback on our support, please contacttnmff@microsoft.com .

    Entan Ming
    TechNet Community Support

    Thursday, May 16, 2013 6:43 AM
    Moderator
  • Hi Ankur,
    I appreciate your suggestion.

    But as I mentioned , I'm working with MOSS 2007 and ' [PageQueryString : Category] ' isnt supported by the OOB CQWP
    Friday, May 17, 2013 11:37 AM
  • I finally managed to  add filter to CQWP  on basis of query string in "MOSS 2007" .

    The possible way to do this is through object model .

    public class MYCQWP : ContentByQueryWebpart   //inherit from  ContentByQueryWebpart class

    {

                            

     protected override void OnLoad(EventArgs e)  //inside its OnLoad
            {

                if (string.IsNullOrEmpty(this.Page.Request.QueryString["ID"]))
                    return;


                this.Filter1ChainingOperator = FilterChainingOperator.Or;

                //  get the filtering field by name .In my case its 
                string filterFieldName = this.Page.Request.QueryString["ID"].ToString();

                // since the field name isn't suffucient for the CQWP, get the field's GUID
                Guid filterFieldID = Microsoft.SharePoint.SPContext.Current.Item.Fields["ID"].Id;
                // get the filtering field value
                string filterFieldValue = this.Page.Request.QueryString["ID"].ToString();

                // set the filtering info
                this.FilterField1 = filterFieldID.ToString();
                this.FilterValue1 = filterFieldValue;
            }

    }

    ->>Deploy and add this webpart , and you'll see inside the Toolpane the filter automatically set to the value from the Query string.

    For eg, if your Query string is   something.aspx?ID=1

    then the filter will be set to ID is equal to 1.

    Regards,
    Ankit

    Friday, May 17, 2013 11:51 AM