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:


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) {
	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");

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='' xmlns:xsd='' xmlns:soap=''> 
                <Query xmlns='urn:Microsoft.Search'> 
                  <queryXml>" + escapeHTML(queryXML) + "</queryXml> 

        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:

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.