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

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:

image

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>\r\n");
  output.Append("<script type=\"text/javascript\" src=\"/Style%20Library/WebParts/myteamsites.js\"></script>\r\n");

   // you shoud be able to edit the All Sites scope setting
    output.Append("<div class=\"myteamsites\" scope=\"All Sites\" delay=\"500\">\r\n</div>\r\n");
   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='https://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='https://www.w3.org/2001/XMLSchema' xmlns:soap='https://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: WebParts.zip

The Sandboxed Solution here: WebParts.wsp