using System;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.XPath;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Audience;
using Microsoft.Office.Server.Search.Query;
using Microsoft.Office.Server.Search.WebControls;
using Microsoft.Office.Server.WebControls;
using Microsoft.SharePoint;
namespace KBSearchWebPart
{
[ToolboxData("<{0}:CoreResultsWebPart runat=server>{0}:CoreResultsWebPart>")]
public class KBSearch : CoreResultsWebPart
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
string strQueryText = string.Empty;
if (Page.Request["k"] != null)
{
strQueryText = Page.Request["k"].ToString();
}
if (strQueryText != string.Empty && strQueryText != null)
{
keywordQueryExecute(strQueryText);
this.Xsl = @"
…
-
Bytes
KB
MB
-
[
]
";
// this.Xsl = @"
//
//
//
//
//
//
//
// ";
}
else
{
XmlDataSource source = new XmlDataSource();
source.Data = "";
this.DataSource = source;
}
this.DataBind();
}
//this is required due to a bug in using XML as the datasource for a dataformwebpart
protected override XPathNavigator GetXPathNavigator(string viewPath) {
XmlDocument doc = new XmlDocument();
doc.LoadXml(((XmlDataSource)this.DataSource).Data.ToString());
return doc.CreateNavigator();
}
private void keywordQueryExecute(string strQueryText)
{
KeywordQuery kRequest = new KeywordQuery(ServerContext.Current);
kRequest.QueryText = strQueryText;
//only want to search on this list, don't want it to be modified on web part page
kRequest.HiddenConstraints = "scope:" + "\"IT Knowledge Base Search\"";
kRequest.ResultTypes = ResultType.RelevantResults;
ResultTableCollection resultTbls = kRequest.Execute();
if ((int)ResultType.RelevantResults != 0)
{
ResultTable tblResult = resultTbls[ResultType.RelevantResults];
DataTable relResultsTbl = new DataTable();
relResultsTbl.TableName = "Result";
DataSet ds = new DataSet("All_Results");
ds.Tables.Add(relResultsTbl);
ds.Load(tblResult, LoadOption.OverwriteChanges, relResultsTbl);
displayResults(ds);
}
}
private void displayResults(DataSet grdDs)
{
DataSet dsFiltered = grdDs.Clone();
foreach (DataRow relevantResult in grdDs.Tables["Result"].Rows)
{
//The Magic happens here. pulls the list ID from the URL using Regex
//and then pulls up the list item, checks if current user is in
//the audience, and returns results if so
String itemURL = relevantResult["Path"].ToString();
Regex r = new Regex(@"(id=)\S*", RegexOptions.IgnoreCase);
Match m = r.Match(itemURL);
if (m.Success)
{
Regex r2 = new Regex(@"(id=)", RegexOptions.IgnoreCase);
Int32 itemID = Convert.ToInt32(r2.Replace(m.Groups[0].Value.ToString(), ""));
using (SPWeb owebsite = SPContext.Current.Site.AllWebs["informationtechnology"])
{
//GUID of my KB list
Guid listid = new Guid("07198824-2397-40c6-b6d1-31ff016bb1c3");
SPList olist = owebsite.Lists.GetList(listid, false);
SPListItem olistitem = olist.GetItemById(itemID);
AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
String strAudienceList = (string)olistitem["Target_x0020_Audiences"];
if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, strAudienceList, false) || strAudienceList == string.Empty)
{
DataRow dr = dsFiltered.Tables["Result"].NewRow();
dr.ItemArray = relevantResult.ItemArray;
dsFiltered.Tables["Result"].Rows.Add(dr);
}
}
m.NextMatch();
}
}
StringWriter writer = new StringWriter();
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.OmitXmlDeclaration = true;
xmlSettings.Indent = true;
xmlSettings.IndentChars = "\t";
xmlSettings.NewLineHandling = NewLineHandling.Entitize;
xmlSettings.NewLineChars = Environment.NewLine;
XmlWriter xmlResultWriter = XmlWriter.Create(writer, xmlSettings);
dsFiltered.WriteXml(xmlResultWriter, XmlWriteMode.IgnoreSchema);
XmlDataSource dataSource = new XmlDataSource();
string strData = writer.ToString();
//cleanup of data
strData = Regex.Replace(strData,"<","<");
strData = Regex.Replace(strData,">", ">");
strData = Regex.Replace(strData, "amp;", "");
dataSource.Data = strData;
dataSource.DataBind();
xmlResultWriter.Close();
this.DataSource = dataSource;
}
}
}
|