Home RSS 2.0 ATOM 1.0  CDF  
 
CodeSegment - Carlos Segura Sanz (blog)
 

This is other sample of how I am using Ajax in my SharePoint environment (see Using Ajax to query Exchange Server Part 1 and Part 2) , now I'm using it to improve my searches in SPS.



First I have added a new page to make the searches, this new page runs under SPS context and it is stored under Layouts directory. This is very basic search page that look at the "DAV:displayname" field to locate files that contains a keyword in the name. This page use a query string to get the keyword and the results are written in HTML. (You can use other queries here).
 

csegAjaxSearch.aspx

<%@ Page language="C#" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, 
Microsoft.SharePoint,Version=11.0.0.0, Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" 
Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePointPortal" Namespace="Microsoft.SharePoint.Portal" 
Assembly="Microsoft.SharePoint.Portal, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %> 
<%@ Import Namespace="Microsoft.SharePoint.Portal" %>
<%@ Import Namespace="Microsoft.SharePoint.Portal.Topology" %>
<%@ Import Namespace="Microsoft.SharePoint.Portal.Search" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text" %>
 
<%
    TopologyManager topology = new TopologyManager();
    PortalSite portal = topology.PortalSites[new Uri("<USE YOUR SERVER HERE>")];
    PortalContext context = PortalApplication.GetContext(portal);
    QueryProvider qp = new QueryProvider(context.SearchApplicationName);
 
    string keyword = Request["k"];
    
    if(keyword!=null && keyword.Trim()!="")
    {
        string queryTemplate = "SELECT \"DAV:href\", " +
                               "\"DAV:displayname\", " +
                               "\"urn:schemas.microsoft.com:fulltextqueryinfo:description\", " +
                               "\"urn:schemas-microsoft-com:office:office#ows_SiteName\" " +
                               "FROM ( TABLE Portal_Content..Scope() " +
                               "UNION ALL TABLE Non_Portal_Content..Scope() ) " +
                               "WHERE CONTAINS(\"DAV:displayname\", '\"*" + 
                               keyword.Replace("'", "''").Replace("\"", "\"\"") + "*\"') ";
        Response.Write(queryTemplate);
        
        DataSet ds = qp.Execute(queryTemplate);
 
        StringBuilder sb = new StringBuilder();
    
        if(ds != null)
        {
           sb.Append("<UL>");
           foreach(DataRow dr in ds.Tables[0].Rows)
           {
              sb.AppendFormat("<LI><A href='{0}'>{1}</A><BR/>{2}<BR/>{3}<BR/></LI>",
                 SPEncode.HtmlEncode(dr["DAV:href"].ToString()),
                 SPEncode.HtmlEncode(dr["DAV:displayname"].ToString()),
                 SPEncode.HtmlEncode(dr["urn:schemas.microsoft.com:fulltextqueryinfo:description"].ToString()),
                 SPEncode.HtmlEncode(dr["urn:schemas-microsoft-com:office:office#ows_SiteName"].ToString()));
           }
           sb.Append("</UL>");
        }
        else
        {
           sb.Append("<P>No results</P>");
        }
      
        Response.Write(sb.ToString());
    }
%>   

In a new area of my SPS server I have added a Html Form webpart, that use ajax to return the results. Here is the code.


<script language="javascript">
var xmlHttpReq;
 
function initHttpRequest()
{
    try 
    {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(ex)
    {
        xmlHttpReq = null;
    }    
}
 
function sendQuery(keywords)
{
    var serverUrl = "http://YOUR SERVER/_layouts/csegAjaxSearch.aspx?k=" + keywords;
    
    initHttpRequest();
   
    if (xmlHttpReq != null)
    {
        xmlHttpReq.onreadystatechange = checkState;
        xmlHttpReq.open("GET", serverUrl, true);
        xmlHttpReq.send(null);
    }    
}
 
function checkState()
{
    // Load completed
    if (xmlHttpReq.readyState == 4)  
    {
        // Status OK
        if (xmlHttpReq.status == 200)
        {
            document.getElementById("Results").innerHTML = xmlHttpReq.responseText;
        }
        // Error
        else
        {
           document.getElementById("Results").innerHTML = "<b>There is an error in the service</b>";
        }
    }
}
</script>
 
<form name="form">
  <input name="keyword" onKeyUp="sendQuery(this.value)" style="WIDTH:500px" autocomplete="off">
  <div align="left" id="Results" style="width:100%"></div>
</form>
Tuesday, May 02, 2006 10:02:37 AM (Hora de verano romance, UTC+02:00)
Great Idea. Will you include ajax into your search webpart?
Echo
Tuesday, May 02, 2006 7:51:51 PM (Hora de verano romance, UTC+02:00)
Very nice!!! Very creative way to test search results ;)
Tuesday, May 02, 2006 9:45:46 PM (Hora de verano romance, UTC+02:00)
Beautiful... now if I could adapt it to do specifically only a people finder and return display name, phone number and photo dynamically ... (the functionality of the SPS People Finder here http://mindsharpblogs.com/matthew/archive/2005/07/20/625.aspx) with the AJAX implementation you mention above.

Superb work!

Rob
Wednesday, May 03, 2006 5:55:22 PM (Hora de verano romance, UTC+02:00)
I'm getting errors when I try it out.

Object expected and another one is complaining about a missing accolade "}"
Robert Graauw
Wednesday, May 03, 2006 8:11:49 PM (Hora de verano romance, UTC+02:00)
Robert, is a problem when cut and paste the html code in the form html editor, try to remove the javascript comments.
Carlos.
Wednesday, May 03, 2006 10:34:54 PM (Hora de verano romance, UTC+02:00)
Excelente Post, Carlos, muy buen contenido en tu Blog.

Saludos

Luis.
Thursday, May 04, 2006 11:13:47 AM (Hora de verano romance, UTC+02:00)
thnx for the tip.
It works great now.
Robert Graauw
Thursday, May 11, 2006 4:57:48 AM (Hora de verano romance, UTC+02:00)
Would it be possible to build something similar to search in a WSS list?
Gilson
Comments are closed.

Copyright © 2008 Carlos Segura. All rights reserved.