I'm trying to understand the mechanism to extend the cqwp via code.
It may be hard to believe but I couldn't find a single article to create a web part inherited from content by query web part.
What I need to do, is to type the listname in the web part properties. Then all the groupings, sorting and the query will be implemented via code, that is in the extended web part.
I've read waldek's posts but they are a bit advanced to use as a cheat sheet.
Msdn's samples show the customization of itemstyle and setting queryoverride over the webpart properties toolbar. I need to set it via code.
Note:If that's not the way to customize cqwp, let me know. My purpose is to put the wp in the masterpage and set the listname and wait for the results to show(:
I've tried to set the listguid and queryoverride via code through OnInit and ModifyXsltArgument methods seperately. Nothing returned, and when I export the wp, the listguid and queryoverride seems not set.
Solution:
To inherit the ContentQueryWebPart just do this:
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); } } }
In this example, I just added a filter to get a field from UserProfile like the original webpart does with querystring. What do you need exactly?