Enjoy Web Dev Life !!!
jQuery, ASP.NET, C#, MVC Tutorials

Loading...

Default view metadata(columns) in sharepoint custom search result page

Generally, it is required to search in a document library and display default view columns in search result. I read different articles for this. I like this article and updated for MOSS 2007.
There are following steps in the code:

1. Retrieve default view columns name for a document library and add columns in Datagrid.
2. Get listitems as search result (For this, use GetListItemsFromFTSQuery method from here)
3. Bind listitems to datagrid
DataTable returnResults;   
protected void Page_Load(object sender, EventArgs e)
{if (Page.IsPostBack)
{
AddDefaultColumnsInGrid();
}
}
void AddDefaultColumnsInGrid()
{

using (SPSite siteCollection = new SPSite(txtDocLib.Text))
{
SPWeb CRsite = siteCollection.OpenWeb();
SPList ContRep = CRsite.GetListFromUrl(txtDocLib.Text);

//get field objects from Content Repository - needed to obtain display name of fields
SPFieldCollection ListFields = ContRep.Fields;

//get field names used in search results view (these are "internal" names, not display names)
SPView SRView = ContRep.Views["All Documents"];
SPViewFieldCollection SRViewFields = SRView.ViewFields;
System.Collections.Specialized.StringCollection InternalViewFldNames = SRViewFields.ToStringCollection();

//Add Columns to datagrid ************************************************ Start
BoundColumn C = null;
string str = "";
for (int i = 0; i < InternalViewFldNames.Count; i++)
{
C = new BoundColumn();
if (i > 0) //no header for doc icon
{
C.HeaderText = ListFields.GetFieldByInternalName(InternalViewFldNames[i]).Title; //this is the DISPLAY name
str += "Column: " + C.HeaderText + "|";
C.SortExpression = InternalViewFldNames[i];
str += "Sort by: " + InternalViewFldNames[i] + "<br>";
//change "name" column to be "file name"
if (C.HeaderText == "Name")
C.HeaderText = "File Name";
if (C.HeaderText == "Title")
C.SortExpression = "SortByTitle";
}

C.DataField = InternalViewFldNames[i];
C.ItemStyle.VerticalAlign = VerticalAlign.Top;
DataGrid1.Columns.Add(C);               
}
//add hidden column for sorting by title (NOT URL of title!)
C = new BoundColumn();
C.HeaderText = "SortByTitle";
C.SortExpression = "SortByTitle";
C.DataField = "SortByTitle";
C.Visible = false;
DataGrid1.Columns.Add(C);
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
BindData(GetListItemsFromFTSQuery(txtDocLib.Text, txtSearch.Text),"");
}
void BindData(List<SPListItem> items, string sortOrder)
{
DataTable ResultsTable = new DataTable();
DataRow ResultsRow;
string strField;
using (SPSite siteCollection = new SPSite(txtDocLib.Text))
{
SPWeb CRsite = siteCollection.OpenWeb();
SPList ContRep = CRsite.GetListFromUrl(txtDocLib.Text);

//get field objects from Content Repository - needed to obtain display name of fields
SPFieldCollection ListFields = ContRep.Fields;

//get field names used in search results view (these are "internal" names, not display names)
SPView SRView = ContRep.Views["All Documents"];
SPViewFieldCollection SRViewFields = SRView.ViewFields;
System.Collections.Specialized.StringCollection InternalViewFldNames = SRViewFields.ToStringCollection();
//process each view field, building results table columns 
for (int j = 0; j < InternalViewFldNames.Count; j++)
{
ResultsTable.Columns.Add(new DataColumn(InternalViewFldNames[j], typeof(string)));           
}
//add the hidden column
ResultsTable.Columns.Add(new DataColumn("SortByTitle", typeof(string)));

foreach (SPListItem lstItem in items)
{
ResultsRow = ResultsTable.NewRow();
for (int j = 0; j < InternalViewFldNames.Count; j++)
{
try
{
ResultsRow[InternalViewFldNames[j]] = lstItem[ListFields.GetFieldByInternalName(InternalViewFldNames[j]).Title].ToString();
}
catch
{ 
}
strField = ResultsRow[InternalViewFldNames[j]].ToString();
if (ListFields.GetFieldByInternalName(InternalViewFldNames[j]).TypeAsString == "WMTags")
{ SPFieldMultiColumnValue mulVal = new SPFieldMultiColumnValue(strField);
ResultsRow[InternalViewFldNames[j]] = mulVal[0].ToString(); 
}

//some fields may have "x;#" at the beginning - strip it off as well as any others
else if (strField.IndexOf("#") >= 0)
{
strField = strField.Substring(strField.IndexOf("#") + 1); //get's the first occurrence
strField = strField.Replace(";#", ";"); //get rid of any other #'s, leaving the ";"
ResultsRow[InternalViewFldNames[j]] = strField;
}
}
//customize "special rows": 
try
{
//make icon row display an image
ResultsRow["DocIcon"] = "<a href='" + lstItem.Url + "'><img src='/_layouts/images/" + lstItem.File.IconUrl + "' border=0></a>";


//make doc name link to doc property page
ResultsRow["Title"] = "<a href='" + lstItem.Url + "'>" + lstItem["Title"] + "</a>";
ResultsRow["SortByTitle"] = lstItem["Title"].ToString();
}
catch { }
ResultsTable.Rows.Add(ResultsRow);    

}
}
//put results into a dataview
DataView ResultsView = new DataView(ResultsTable);
//sort, if needed
if (sortOrder != "")
{
ResultsView.Sort = sortOrder;
}
//bind to grid
DataGrid1.DataSource = ResultsView;
DataGrid1.DataBind();       

}

protected void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
{
BindData(GetListItemsFromFTSQuery(txtDocLib.Text, txtSearch.Text), e.SortExpression);
}

Hope, It helps.

1 comment:

  1. Hi,

    Thanks to update this code for moss 2007.

    I get always an 'unknown error' when loading the searchresultscr page with your code. Can you give me more info howto implement this code?

    Thanks in advance

    ReplyDelete