Sharepoint 2010 通过代码扩展内容查询Web部件

Sharepoint 2010 通过代码扩展内容查询Web部件,sharepoint-2010,web-parts,extend,cqwp,Sharepoint 2010,Web Parts,Extend,Cqwp,我试图理解通过代码扩展cqwp的机制 可能很难相信,但我找不到一篇文章来创建继承自的web部件 我需要做的是在web部件属性中键入listname。然后,所有分组、排序和查询都将通过代码实现,即在扩展web部件中 我读过waldek的帖子,但它们作为备忘使用有点高级 Msdn的示例显示了itemstyle的自定义以及在webpart属性工具栏上设置queryoverride。我需要通过代码进行设置 注意:如果这不是定制cqwp的方法,请告诉我。我的目的是将wp放入母版页,设置listname并等

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

可能很难相信,但我找不到一篇文章来创建继承自的web部件

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

我读过waldek的帖子,但它们作为备忘使用有点高级

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

注意:如果这不是定制cqwp的方法,请告诉我。我的目的是将wp放入母版页,设置listname并等待结果显示(:

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

我肯定我做错了什么,所以我很感激你的帮助。 提前谢谢

然后,所有分组、排序和查询都将通过 代码,该代码位于扩展web部件中

为什么您要对CQWP中已经可用的所有功能进行编码。也许我没有抓住要点。关于扩展web部件,您只需对其进行子分类即可

请参见此处的示例:


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

要继承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);
        }
    }
}
使用系统;
使用系统组件模型;
使用Microsoft.SharePoint.Publishing.WebControl;
使用Microsoft.SharePoint;
使用Microsoft.Office.Server.UserProfiles;
名称空间YOURNAMESPACE
{
[ToolboxItemAttribute(false)]
公共类CustomContentQueryWebPart:ContentByQueryWebPart
{
受保护的覆盖无效加载(事件参数e)
{
尝试
{
//Reemplazamos[UserContext:]por su valor
字符串val,字段;
UserProfile UserProfile=getCurrentUserProfile();
val=this.FilterValue1;
if(val.StartsWith(“[UserContext:”)&val.EndsWith(“]))
{
字段=值子字符串(13,值长度-14);
this.FilterValue1=userProfile[field].Value.ToString();
}
val=this.FilterValue2;
if(val.StartsWith(“[UserContext:”)&val.EndsWith(“]))
{
字段=值子字符串(13,值长度-14);
this.FilterValue2=userProfile[field].Value.ToString();
}
val=this.FilterValue3;
if(val.StartsWith(“[UserContext:”)&val.EndsWith(“]))
{
字段=值子字符串(13,值长度-14);
this.FilterValue3=userProfile[field].Value.ToString();
}
}
捕获(例外情况除外){}
最后
{
基础荷载(e);
}
}
私有用户配置文件getCurrentUserProfile()
{
SPUser user=SPContext.Current.Web.CurrentUser;
//创建新的UserProfileManager
UserProfileManager pManager=新建UserProfileManager();
//获取当前用户的用户配置文件
返回pManager.GetUserProfile(user.LoginName);
}
}
}

在本例中,我刚刚添加了一个过滤器,以从UserProfile中获取字段,就像原始Web部件对querystring所做的那样。您到底需要什么?

这里提供了一篇详细的文章。