programmatically setting the CAML-query on the QueryOverride-member of an custom WebPart that inherits from ContentByQueryWebPart

StackOverflow https://stackoverflow.com/questions/5457223

Question

The title pretty much sums up, what I'm looking for. I created a custom WebPart in Visual Studio 2010, for a SharePoint 2010 Solution, that derives from the ContentByQueryWebPart. The purpose of the WebPart is to show the users an overview of their own tasks, taken from multiple task-lists. No problems so far.

But, I have overridden the OnInit-Method in order to get the some data from the url-query (using Page.Request) to do some custom filtering, grouping and sorting. The problem is, that the custom CQWP won't take the CAML string I created.

I tried some approaches already, but none of them seemed to work. I even stripped it down to the minimum. Maybe the spot where I set the QueryOverride is wrong. (OnInit / OnLoad / before or after the base.On****(e); ?) For now, It looks like this:

protected override void OnInit(EventArgs e)
{
    string query = "<Query><Where><Eq><FieldRef Name=\"AssignedTo\" /><Value Type=\"User\"><UserID /></Value></Eq></Where></Query>";
    this.QueryOverride = query;
    base.OnInit(e);
}

I hope that someone has an idea. Any help appreciated. If there's still something unclear, please don't hesitate to ask.

Markus Schwalbe

Note: English is not my first language, so please excuse me for every mistake I've made in this post. :)

Was it helpful?

Solution

I found a solution to do this. seems like it didn't wanted the UserID-tag as value.

It works like a charm when you use the Name-attribute of the current (or any other) SPUser-object and change the type of the value to "Text": (Also, you can leave the Query-tag away and the " can be replaced with ', which looks kinda cleaner)

this.QueryOverride = "<Where><Eq><FieldRef Name='AssignedTo' /><Value Type='Text'>" + SPContext.Current.Web.CurrentUser.Name + "</Value></Eq>";

I hope this helps anyone, who has the same problem. sincerly,

Markus Schwalbe

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top