How can I access through code the Grouping property for a Content Query Web Part in SharePoint 2010?

600 views Asked by At

I am using the SPLimitedWebPartManager to access the webpart properties for a Content Query Web Part, in order to programatically modify the "Grouped by" property for the CQWP... Unfortunately I am not sure how to access that specific property, and I seem to be able to modify the Title, Description, etc... but not the Grouping / Sorting properties... Any clue on how to do this?

Here is the code:

SPFile ofile = page.File;

SPLimitedWebPartManager wpColl = ofile.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.User);
int cont = wpColl.WebParts.Count;

Console.WriteLine("    - " + page.Name);

for (int i = 0; i < cont; i++)
{
    System.Web.UI.WebControls.WebParts.WebPart wp1 = wpColl.WebParts[i];

    Console.WriteLine("        Personal: " + wp1.ToString());
    Console.WriteLine("        - Title: " + wp1.Title);
    Console.WriteLine("        - ID: " + wp1.ID);

    if (wp1.Title.Equals("CQWP Title"))
    {
        wp1.Title = "WebPart_CQWP";
        Console.WriteLine("        - New Title " + wp1.Title);

        // ----- here I would like to add the same thing to update the Group By property (or any other property which would be useful in order to configure the CQWP (list, type of document we are filtering, etc...) ---- how can I do that?
    }

    ofile.Update();
    wpColl.SaveChanges(wp1);
}

Thank you!

1

There are 1 answers

0
Aidenn On

Ok, found how to do it. I needed to cast this webpart as a CQWP actually in order to have access to the exact settings for it. Added the following to the code:

Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart cqwp = (Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart)wp1;

                                    Console.WriteLine("................." + cqwp.GroupBy);

Now I can access the GroupBy property for my webpart, which is not available otherwise.