如何通过代码访问SharePoint 2010中的内容查询Web部件的分组属性?

时间:2012-06-01 10:18:20

标签: c# sharepoint-2010 cqwp

我正在使用SPLimitedWebPartManager访问内容查询Web部件的webpart属性,以便以编程方式修改CQWP的“Grouped by”属性...不幸的是我不知道如何访问该特定属性,以及我似乎能够修改标题,描述等...但不能修改分组/排序属性...有关如何执行此操作的任何线索?

以下是代码:

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

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,找到了怎么做。 我需要将此webpart实际转换为CQWP,以便能够访问它的确切设置。在代码中添加了以下内容:

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

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

现在我可以访问我的webpart的GroupBy属性,否则无法使用。