Using ajax to query XML in SharePoint Doc Lib, for use in a form’s input autocomplete

Long title, but I wanted to get across what I was trying to do. If I had and XML file in a document library (you could mail enable the doc lib, and send XML from a query in SQL server using the “FOR XML” statement!), could I use jquery to add Autocomplete values to an input field in a newform.aspx? Ended up being not that difficult.

  1. Once the ajax call sucessfully retrievs the xml file, the parseXml routine is called.
  2. Seems there is an issue that IE will not think the file is XML, but rather as txt, so there some quick code to load the file as XML
  3. Then I just look in the file and grab the values I want, and append them to an array
  4. Then just set the array to be used as the autocomplete source
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<script type="text/javascript">
$(document).ready(function() {
$.ajax({   
    type: "GET",
    url: "http://URL/To/xmlfile.xml",
    dataType: ($.browser.msie) ? "text" : "xml",
    success: parseXml
});
});
function parseXml(data)
{
    var xml;
     if (typeof data == "string") {
       xml = new ActiveXObject("Microsoft.XMLDOM");
       xml.async = false;
       xml.loadXML(data);
     } else {
       xml = data;
     };
    var results = [];
    $(xml).find("ID").each(function(){ 
        var ClientName = $.trim($(this).find('Name').text());
        var ClientCode = $.trim($(this).find('ZipCode').text());
        results[results.length] = ClientName + "(" + ClientCode + ")"
    });
 
    $('input[title=Title]').autocomplete({
    source: results,
    delay:10,
    minLength: 3
    });
};
</script>

,

Comments are closed.

Powered by WordPress. Designed by WooThemes