Friday, April 1, 2011

Custom web part for search list items and display in custom view

SharePoint 2010 web parts…..

Custom web part for Search list items and display in custom view
 
In here I’ll show you how to view list items, that are compatible with user input data. I’m using custom web part to implement this project.
As the example I’ll use “Task” list and retrieve data according to column “Status”.
Eg: If user input “Completed”, then he will see the list items with status “completed”.

Step 1: Create new visual web part project in Visual Studio 2010.



Step 2: Add new textbox and button to project.


Step 3: Insert below code into button click event.
                     ListViewByQuery mycustomview;
           
            SPWeb thisWeb = null;
           
            thisWeb = SPContext.Current.Web;
           
            mycustomview = new ListViewByQuery();
            mycustomview.List = thisWeb.Lists["Tasks"];
            SPQuery query = new SPQuery(mycustomview.List.DefaultView);
                     
            query.Query = "<Where><Eq><FieldRef Name='Status'/><Value           Type='Text'>"+TextBox1.Text+"</Value></Eq></Where>";
            query.ViewFields = "<FieldRef Name=\"Title\"/><FieldRef Name=\"Status\"/>";
            mycustomview.Query = query;          
            this.Controls.Add(mycustomview);
               
 Step 4: Deploy the solution.

Step 5: Add your new custom web part to SharePoint new page.

















Retrieve tasks with status "Completed"
Output will be like this.















No comments:

Post a Comment