Total Pageviews

11 Jan 2013

Testing SharePoint Server Search with KeywordQuery


Code:
   class Program
    {
        static void Main(string[] args)
        {

            SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();

            SearchServiceApplicationProxy searchProxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("Enterprise Search Service");

            KeywordQuery keywordQuery = new KeywordQuery(searchProxy);

            keywordQuery.QueryText = "Region:b2a48ce5-31e0-40f7-873b-f4fbceacc45f";

            keywordQuery.SelectProperties.Add("Title");
            keywordQuery.SelectProperties.Add("Region");
            keywordQuery.SelectProperties.Add("Регион");
            keywordQuery.SelectProperties.Add("ItemSection");

            //The creator of this fault did not specify a Reason:
           // keywordQuery.SelectProperties.Add("owsMetadataFacetInfo");
            
           // keywordQuery.SelectProperties.Add("owstaxIdRegion");

            keywordQuery.ResultsProvider = SearchProvider.Default;

            keywordQuery.ResultTypes = ResultType.RelevantResults;

            keywordQuery.RowLimit = 50;

            ResultTableCollection resultsTableCollection = keywordQuery.Execute();

            ResultTable searchResultsTable = resultsTableCollection[ResultType.RelevantResults];

            DataTable resultsDataTable = new DataTable();

            resultsDataTable.TableName = "Results";

            resultsDataTable.Load(searchResultsTable, LoadOption.OverwriteChanges);

            Console.WriteLine(resultsDataTable.Rows.Count);

            for (int j = 0; j < resultsDataTable.Rows.Count; j++)
            {

                DataRow row = resultsDataTable.Rows[j];
                foreach (DataColumn column in resultsDataTable.Columns)
                {
                    Console.Write("Item: ");
                    Console.Write(column.ColumnName);
                    Console.Write(" ");
                    Console.WriteLine(row[column]);
                }
            } 
        }
    }


Code:
     using(SPSite site = new SPSite("http://portal.com"))
            {
                SearchServiceApplicationProxy ssa = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(site));

                KeywordQuery kq = new KeywordQuery(ssa);
                kq.ResultsProvider = SearchProvider.Default;
                kq.QueryText = string.Format(CultureInfo.InvariantCulture, "scope:\"{0}\"; title:\"Ivanov Ivan\"", "people");
                kq.ResultTypes = ResultType.RelevantResults;
                kq.StartRow = 0;
                kq.EnablePhonetic = true;
                kq.EnableStemming = true;
                kq.RowLimit = 1000;
                kq.KeywordInclusion = KeywordInclusion.AllKeywords;

                ResultTableCollection res = kq.Execute();

                bool exists = res.Exists(ResultType.RelevantResults);

                if (exists)
                {
                    ResultTable searchResult = res[ResultType.RelevantResults];
                    Console.WriteLine(searchResult.TotalRows);
                }

                
            }
            Console.ReadLine();


How to configure Incoming Email Enabled Libraries in MOSS2007 RTM using Exchange 2007 in an Active Directory Domain

Check this out :)

Adding a warm-up step to your SharePoint project deploy action

I've recently found a very useful blog post on how to improve SharePoint Deployment experience in Visual Studio.

Check this out!

Simple Custom Error Page


  1. In web.config set: 
    •  CallStack="false" 
    •  customErrors mode="On"
  2. Run Powershell command "Set-SPCustomLayoutsPage -Identity Error -RelativePath /_layouts/CustomError/error.aspx -WebApplication http://Srv
  3. Add a new ASPX page to /_layouts/CustomError/error.aspx: 
You can download source code from here. My custom error page looks like that:


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="Microsoft.SharePoint.Administration"  %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Error</title>
</head>
<script language="C#" runat="server">

    public void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("Error occured:");

        Exception[] errors = Context.AllErrors;

        if (errors == null)
        return;

        for (int i = 0; i < errors.Length; i++)
        {
            Exception err = errors[i];
            LogError(err.Message);

            sb.Append("<br>");
            sb.Append(err.Message);
            sb.Append("<br><br>Stack:<br>");
            sb.Append(err.StackTrace);

            if (err.InnerException != null && err.InnerException.Message != err.Message)
                sb.Append("<br><br>" + err.InnerException.Message);
        }

        lblError.Text = sb.ToString();        
    }

    private static void LogError(string message)
    {
        uint customId = 7777;
        SPDiagnosticsService.Local.WriteTrace(customId,
            new SPDiagnosticsCategory("CustomError Message", TraceSeverity.Unexpected, EventSeverity.Error),
            TraceSeverity.Unexpected, message);
    }

</script>

<body>
<asp:Label ID="lblError" runat="server" />
</body>
</html>