Sample code for a Sandboxed Solution for Office 365 using search.asmx with jQuery

By | 2011-05-06

Wanted to create a sample that:

  • Runs on SharePoint 2010
  • Uses the Search.asmx webservice
  • Can run inside the sandbox
  • Uses jQuery
  • Can run on Office 365

What to create? A webpart that shows all site collections you have access to!

Here is the output of the webpart actually running on Office 365:

o365-code-search

The webpart itself is quite simple:

/// <summary>
/// Render the required javascript links and html
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer) {
	base.Render(writer);
	StringBuilder output = new StringBuilder();

	output.Append("<script type="text/javascript" src="/Style%20Library/WebParts/jquery-1.5.2.min.js"></script>rn");
	output.Append("<script type="text/javascript" src="/Style%20Library/WebParts/myteamsites.js"></script>rn");

	// you shoud be able to edit the All Sites scope setting
	output.Append("<div class="myteamsites" scope="All Sites" delay="500">rn</div>rn");
	writer.Write(output.ToString());
}

Using the search.asmx with jquery is not that hard:

query = "SELECT Title, Description, Path, Size, Rank, Write,WebId FROM SCOPE() WHERE ( ( "IsDocument" = 0 ) and ("Size" > 0 ) )";

    var queryXML = "<QueryPacket xmlns='urn:Microsoft.Search.Query' Revision='1000'>";
    queryXML += "<Query domain='QDomain'>";
    queryXML += "<SupportedFormats><Format>urn:Microsoft.Search.Response.Document.Document</Format></SupportedFormats>";
    queryXML += "<Context>";
    queryXML += "<QueryText language='en-US' type='MSSQLFT'><![CDATA[ " + query + " ]]></QueryText>";
    queryXML += "</Context>";
    queryXML += "<SortByProperties>";
    queryXML += "<SortByProperty name='" + orderbyfield + "' />";
    queryXML += "</SortByProperties>";
    queryXML += "<Range><StartAt>" + startat + "</StartAt><Count>" + pagesize + "</Count></Range>";
    queryXML += "<EnableStemming>false</EnableStemming>";
    queryXML += "<TrimDuplicates>true</TrimDuplicates>";
    queryXML += "<IgnoreAllNoiseQuery>true</IgnoreAllNoiseQuery>";
    queryXML += "<ImplicitAndBehavior>true</ImplicitAndBehavior>";
    queryXML += "<IncludeRelevanceResults>true</IncludeRelevanceResults>";
    queryXML += "<IncludeSpecialTermResults>true</IncludeSpecialTermResults>";
    queryXML += "<IncludeHighConfidenceResults>true</IncludeHighConfidenceResults>";
    queryXML += "</Query></QueryPacket>";

    var soapEnv =
            "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> 
              <soap:Body> 
                <Query xmlns='urn:Microsoft.Search'> 
                  <queryXml>" + escapeHTML(queryXML) + "</queryXml> 
                </Query> 
              </soap:Body> 
            </soap:Envelope>";

$.ajax({ 
        url: "/_vti_bin/search.asmx", 
        type: "POST", 
        dataType: "xml", 
        data: soapEnv, 
        complete: parseresultsfunction, 
        contentType: "text/xml; charset="utf-8"" 
    });

There is a gotcha: not all managed properties avaiable in a OnPremise default SharePoint 2010 installation are available in office 365. So I had to do some extra filtering in the javascript code (managed property webid should have a value).

The code is available here: 0365-sample-v14-jquery-search.WebParts

The Sandboxed Solution here: 0365-sample-v14-jquery-search.WebParts.wsp.zip

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.