﻿$(document).ready(function() 
{       
    var soapEnv = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> <soapenv:Body><GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>CommonTerms</listName><viewFields><ViewFields><FieldRef Name='Title' /></ViewFields></ViewFields></GetListItems></soapenv:Body></soapenv:Envelope>";       
    //Make a call to the List WebService using the SOAP envelope described above.     
    //The URL is fixed to a Specific Site Root.  Feel free to change this     
    //to your own fixed root or do some jscript voodoo to figure out where       
    //Of course in 2010 you can do this with the Client Object Model, or hit     
    //the list rest Service and return json, so enabling jsonp cross site calls.     
    $.ajax({         
        url: "/sites/kavli/_vti_bin/lists.asmx",         
        type: "POST",         
        dataType: "xml",        
        data: soapEnv,         
        contentType: "text/xml; charset=\"utf-8\"",         
        success:  function( xmlResponse )         
        {             
            //xmlResponse contains an IXMLDOMDocument, you can see the raw xml soap response, if you query             
            //xmlRepsonse.xml.  But as this is a list and we want tis entries we use jQuery selector on it             
            //      $( "z\\:row", xmlResponse ) will return a jQuery array of IXMLDOMElements matching z:row tags             
            //   (the query "z\\:row" means "z:row" remember that ":" means things in jQuery so it needs to be escaped)             
            //             
            //             
            var domElementArray=$( "z\\:row", xmlResponse );               
            
            //The map function is a technique for processing one array and returning another             
            //jQuery array, in this case we are returning an array of jscript objects that map to attributes             
            //of the XML entries.             
            //see http://api.jquery.com/jQuery.map/             
            var dataMap = domElementArray.map(function()                 
            {                     
                return {                         
                    value: $(this).attr('ows_Title') ,                         
                    id: $(this).attr('ows_Title')                     
                };                  
            });               
            
            //In this case the autocomplete function cannot handle jQuery arrays it want POJ arrays in a specific format.             
            //By using the get() function we return the internals of the jQuery array the core objects             
            //see http://api.jquery.com/get/             
            var data = dataMap.get();               
            
            // Find the Sharepoint Portal Search Box (this is a poor selector, but it is not properly named by sharepoint, 
            // well it is but INamingContainer getrs in the way)             
            $("input.ms-sbplain").autocomplete(             
            {                 
                source: data             
            }); //autocomplete         
            }       
    });//.ajax   
});//docReady 
