Get spelling error count using SharePoint SpellCheck

28/12/2015 08:14

//reads 'inputString' and returns spelling error count

//script is using SharePoint Spell Service (SpellCheck.asmx) in vti_bin
function getSpellCheckErrorsCount(inputString) {
  
 var errorCount;
 var lcid = SP.Res.lcid; //e. g. En-Us is 1033
 var serviceUrl = _spPageContextInfo.webAbsoluteUrl + "/_vti_bin/SpellCheck.asmx";
 var soapRequest = "<?xml version='1.0' encoding='utf-8'?><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>    <SpellCheck xmlns='https://schemas.microsoft.com/sharepoint/publishing/spelling/'>      <chunksToSpell>        <string>"+ inputString +"</string></chunksToSpell>      <declaredLanguage>"+ lcid +"</declaredLanguage>      <useLad>true</useLad>    </SpellCheck>  </soap:Body></soap:Envelope>";

 $.ajax({
  type: "POST",
  url: serviceUrl,
  beforeSend: function (xhr) {
   xhr.setRequestHeader("SOAPAction", "https://schemas.microsoft.com/sharepoint/publishing/spelling/SpellCheck");
  },
  contentType: "text/xml",
  dataType: "xml",
  data: soapRequest,
  success: SuccessOccur,
  error: ErrorOccur,
  async: false //to return value from ajax
 });

 function SuccessOccur(data, status, req) {
  if (status == "success") {
   console.log(soapRequest); //request
   console.log(req.responseText); //response

   //todo - read response xml, find node 'flaggedWords' count
   var i = 0;
   $(req.responseXML).find('flaggedWords').each(function () {    
    $(this).find('word').each(function () {
     var id = $(this).text();
     console.log(id);
     i++;
    });
   });
   console.log("Error count: " + i);
   errorCount = i;
  }
 }

 function ErrorOccur(data, status, req) {
  alert(req.responseText + " " + status);
 }
 return errorCount;
}