Scripts

CAML Query


<script type="text/javascript">
window.jQuery || document.write('<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.js">\x3C/script>')</script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.js">
</script>
<script type="text/javascript">
    jQuery(document).ready(function(){

          //Lookup Field on the list where dynamic field population needs to happen

         jQuery("select[title=Country]").change(function(){

               //Onchange of the lookup values set the rest of the related fields pulling values from the list used for lookup

              setFields();

          });

    });

     function setFields(){

    var selectedMatch = jQuery("select[title=Country] option:selected").text();

    //Use Client Object Model to query the Country list for an item with

      //the value above in its title.

     var context = new SP.ClientContext.get_current();

     var listTitle = "Child";

    list = context.get_web().get_lists().getByTitle(listTitle);

    

     //Build a CAML query to return the required fields

    var query = '<view><query><where><eq><fieldref Name=\'Title\' />'

                 +'<value Type=\'Text\'>'+selectedMatch+ '</Value></Eq></Where></Query>'

                +'<viewfields><fieldref Name=\'Capital\'/><fieldref Name=\'Language\'/><fieldref Name=\'Population\'/></ViewFields></View>';

    

    var camlQuery = new SP.CamlQuery();

    

     camlQuery.set_viewXml(query);




    //all the items returned by the query.

     var allItems = list.getItems(camlQuery);

    

    context.load(allItems);

    

         context.executeQueryAsync(

            function(sender, args){

                 //This code is run on success.

                var enumerator = allItems.getEnumerator();

                 while (enumerator.moveNext()){

                    var listItem = enumerator.get_current();    

                     //Walk through the collection of items  

                    //Set the value of the text box to the value returned by the query.                             

                    jQuery("input[title$='Capital']").val(listItem.get_item('Capital'));

                    jQuery("input[title$='Capital']").change();
    
                    jQuery("input[title$='Language']").val(listItem.get_item('Language'));

                    jQuery("input[title$='Language']").change();    

                                        jQuery("input[title$='Population']").val(listItem.get_item('Population'));

                    jQuery("input[title$='Population']").change();    

    

                }

             },

             function(sender, args){ alert("error: " + args.get_message()); }

    

        );

    

     } 
 </script>


SP WebServices


<script type="text/javascript">window.jQuery || document.write('<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.0.js">\x3C/script>')</script>

<script type="text/javascript">

No comments:

Post a Comment