1

我试图了解通过代码扩展 cqwp 的机制。

可能很难相信,但我找不到一篇文章来创建通过查询 web 部件从内容继承的 web 部件

我需要做的是在 Web 部件属性中键入列表名称。然后所有的分组、排序和查询都将通过代码实现,即在扩展的 Web 部件中。

我已经阅读了 waldek 的帖子,但它们在用作备忘单方面有点先进。

Msdn 的示例显示了 itemstyle 的自定义以及在 webpart 属性工具栏上设置 queryoverride。我需要通过代码设置它。

注意:如果这不是自定义 cqwp 的方式,请告诉我。我的目的是将wp放在masterpage中并设置listname并等待结果显示(:

我尝试通过代码分别通过 OnInit 和 ModifyXsltArgument 方法设置 listguid 和 queryoverride。没有返回,当我导出 wp 时,listguid 和 queryoverride 似乎没有设置。

我确定我在做一些基本的错误,所以我很感激你的帮助。提前致谢..

4

2 回答 2

3

要继承 ContentQueryWebPart,只需执行以下操作:

using System;
using System.ComponentModel;
using Microsoft.SharePoint.Publishing.WebControls;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;

namespace YOURNAMESPACE
{
    [ToolboxItemAttribute(false)]
    public class CustomContentQueryWebPart : ContentByQueryWebPart
    {
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                //Reemplazamos [UserContext:<field>] por su valor
                string val, field;
                UserProfile userProfile = getCurrentUserProfile();

                val = this.FilterValue1;
                if (val.StartsWith("[UserContext:") && val.EndsWith("]"))
                {
                    field = val.Substring(13, val.Length - 14);
                    this.FilterValue1 = userProfile[field].Value.ToString();
                }

                val = this.FilterValue2;
                if (val.StartsWith("[UserContext:") && val.EndsWith("]"))
                {
                    field = val.Substring(13, val.Length - 14);
                    this.FilterValue2 = userProfile[field].Value.ToString();
                }

                val = this.FilterValue3;
                if (val.StartsWith("[UserContext:") && val.EndsWith("]"))
                {
                    field = val.Substring(13, val.Length - 14);
                    this.FilterValue3 = userProfile[field].Value.ToString();
                }
            }
            catch (Exception ex) { }
            finally
            {
                base.OnLoad(e);
            }
        }

        private UserProfile getCurrentUserProfile()
        {
            SPUser user = SPContext.Current.Web.CurrentUser;
            //Create a new UserProfileManager
            UserProfileManager pManager = new UserProfileManager();
            //Get the User Profile for the current user
            return pManager.GetUserProfile(user.LoginName);
        }
    }
}

在此示例中,我只是添加了一个过滤器以从 UserProfile 中获取一个字段,就像原始 web 部件对查询字符串所做的那样。你到底需要什么?

于 2011-09-06T10:38:26.943 回答
0

然后所有的分组、排序和查询都将通过代码实现,即在扩展的 Web 部件中。

为什么要编写 CQWP 中已有的所有功能。也许我错过了重点。关于扩展 Web 部件,您只需将其子类化即可。

请参阅此处的示例:

http://www.andrewconnell.com/blog/archive/2008/02/18/Subclassing-the-Content-Query-Web-Part-Adding-Dynamic-Filtering.aspx

http://ecqwp.codeplex.com/

让我知道您的确切目标是什么,我可以帮助您完成它。

于 2011-09-06T10:26:52.860 回答