Tuesday, May 12, 2009

SharePoint's ListViewWebPart --- Part 2

In continuation with previous post SharePoint's ListViewWebPart.

In this post I discussed how we can apply complex filter in SharePoint's ListView WebPart.

In one of appliation I was using SharePoint's ListView webpart in an aspx page to display the records from SPList. In that page I am also providing a search funtionality. Based on the applied search I am changing the records displayed in ListView webpart.

So the challenge was to apply the complex search filter on ListView webpart programmatically and dynamically. ListView webpart exposes FilterString property but in that we can apply simple filter and that only to one column (as per my knowledge).

So to apply complex filter, first we have to get the SPView from which we are populating data in ListViewWebPart. Then change the Query property of SPView as per our requirement.

In following method I implemented the above mentioned logic (codes with comments are self-explainatory).




/// <summary>
/// Search functionality OR Apply complex filter in SharePoint's ListView WebPart
/// </summary>
/// <param name="eventArgument"></param>
void searchRecord(string flag)
{
try
{
SPSite oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["LISTNAME"];
//oListViewWP is ListView Webpart's ID

oWebsite.AllowUnsafeUpdates = true;

//Clear Search filter OR revert back SPView modification.
if (flag == "false")
{
//Listview webpart related settings
oListViewWP.ListName = oList.ID.ToString("B").ToUpper();

//Set SPView for Listview WP
SPView view = oList.Views["SPView_NAME"];
view.Query = string.Empty;
view.Update();

oListViewWP.ViewGuid = view.ID.ToString("B").ToUpper();
oListViewWP.GetDesignTimeHtml();
}
else
{
//Build the SPQuery
StringBuilder strbPreQuery
= new StringBuilder("<Where><Contains>");

StringBuilder strbPostQuery
= new StringBuilder("</Value></Contains></Where>");

string strQueryKeyword = string.Empty;

switch (drpSearchKeywordType.SelectedValue)
{
case "Contact Number":
strQueryKeyword
= "<FieldRef Name='Contact_x0020_Number'/>
<Value Type='Text'>";
break;

case "Email ID":
strQueryKeyword
= "<FieldRef Name='Email_x0020_ID'/>
<Value Type='Text'>";
break;
}

//Build SPQuery
SPQuery oQuery = new SPQuery();
oQuery.Query
= strbPreQuery.ToString() + strQueryKeyword +
txtSearchKeyword.Text + strbPostQuery.ToString();
SPListItemCollection itemCol = oWebsite.Lists[spListName].GetItems(oQuery);

if (itemCol.Count > 0)
{
//Listview webpart related settings
oListViewWP.ListName = oList.ID.ToString("B").ToUpper();

//Modifying SPView as per required SPQuery.
SPView view = oList.Views["SPView_NAME"];
view.Query = oQuery.Query;
view.Update();

//Modifying ListView WebPart as changed SPView
oListViewWP.ViewGuid = view.ID.ToString("B").ToUpper();
oListViewWP.GetDesignTimeHtml();
}
}

oWebsite.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
}
}


7 comments:

Anonymous said...

Thank you so much. It works very well.

Anonymous said...

Sorry but I'm a beginner in sharepoint but if let's say I want to have multiple text boxes to input the search criteria (can be one or a combination of field values) and then clicking a search button to get the results in the listview, how can I use that code that you provided to implement it? Thanks.

Avinash said...

Hi,

If your are using more than once textboxes for search criteria, then accordingly you have to write complicate SPQuery with AND / OR operations. In this you have to dynamically build query based on the text entered in various textboxes.

Another approach---
Use SharePoint search engine. Register all required columns as metadata column and map it in Central Admin. Then use SP search webpart to search and display records. This will be faster than SPQuery approach, but here you have to play with SP Search functionality.

Anonymous said...

Thanks for that! I've actually got it to work just using an article on applying multiple filters...

but now the problem is blank criteria.

for e.g. if textbox1 is blank and textbox2 contains value, normally it will only search by applying the criteria in textbox2 only but for instance right now it's evaluating both textbox1 and textbox2 and since textbox1 is blank, it won't return any results which is incorrect

any idea on how to make sharepoint ignore a criteria if it's blank?

Anonymous said...

Hey Avinash ...
Nice post...
One problem I faced that while using
_wpListView.ListName = spreflist.ID.ToString("B").ToUpper();

//Modifying SPView as per required SPQuery.
view.Query = spquery1.Query;
view.Update();
//Modifying ListView WebPart as changed SPView
_wpListView.ViewGuid = view.ID.ToString("B").ToUpper();

It is not showing all columns..
Could you please advice me about this?

Sanjeev

Avinash said...

Hi Sanjeev,

You can follow any one of these approaches to display desired columns:

1) {Code change}---
You can set the ViewFields of SPView [SPView.ViewFields]

2) {Change through Browser} ---
Set SPView, so that it displays your required columns and then refer that SPView's GUID in following code -
_wpListView.ViewGuid

~ Avinash

Anonymous said...

Hi Avinash,

I have adding default list or library as my web part.and i created another web part also in this page..In that web part i used 3 text box to search.and i create a view by code and assign it to that list..so it will affect to all the user that is my filter data also affect others. any solution for this???

Google