вторник, 27 мая 2008 г.

ListViewWebPart my own filter.

Standard filter allows us using only equal statements.
If You need to extend your abilities, You can make your own webpart - ListViewWebPartModificator.
This webpart can modify representation of list in ListViewWebPart by changing it's schema on the fly.
I've made it recently, because I needed to show information depended on parameters or user.
You can write properties to url?property=value.
Read it in webpart class. Like a this.Context.Request["DocumentID"]
And change the schema Query tag.
In that way, you can modify anyone tag of view schema.
Code:
_DocumentID - you property from url.
_viewGuid - webpart property guid of the ListViewWebPart to modify.
SPWebPartManager spwp = (SPWebPartManager)this.WebPartManager;
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in spwp.WebParts)
{
try
{
ListViewWebPart lv = (ListViewWebPart)wp;
if (lv.ViewGuid == _viewGuid)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.InnerXml = lv.ListViewXml;
XmlNode node;
node = xmlDoc.DocumentElement;
node["Query"].InnerXml = "<where>" + "<eq><fieldref name="ParentId"><value type="Text">" + _DocumentID + "</value></fieldref>" + "</eq>";
lv.ListViewXml = xmlDoc.InnerXml;
}
}
}

I initiate my cod in method: "protected override void OnInit(EventArgs e){}"
And all works.
If you want to make an interface for filtering you can add controls in your webpart, and create query in the SPQuery way.
And it also works.

Комментариев нет: