SharePoint CAML Query - retrieving field names

I am trying to retrieve the columns to my list which I think works but I'm having a hard time concatenating it.  Look at line 57.  How can  I get that to work.  Thanks!
	//IIFE
	$(function() {
			retrieveListItems();
	});

 * Get the value of a querystring
 * @param  {String} field The field to get the value of
 * @param  {String} url   The URL to get the value from (optional)
 * @return {String}       The field value
 */
var getQueryString = function ( field, url ) {
    var href = url ? url : window.location.href;
    var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
    var string = reg.exec(href);
    return string ? string[1] : null;
};

	//Retrieves all list items- Will need to modify to take in query string data to pass to CAML.
	function retrieveListItems() {
		var clientContext = new SP.ClientContext();
		var oList = clientContext.get_web().get_lists().getByTitle('ParkFundingForm');
		//alert(getQueryString('Title'));
		var camlQuery = new SP.CamlQuery();
			camlQuery.set_viewXml("<View><Query>"+
					   "<Where>"+
					      "<Eq>"+
					         "<FieldRef Name='Title' />"+
					         "<Value Type='Text'>"+getQueryString('Title')+"</Value>"+
					      "</Eq>"+
					   "</Where>"+
					"</Query>"+
					"<ViewFields>"+
					   "<FieldRef Name='Title' />"+
					   "<FieldRef Name='ProjectName' />"+
					   "<FieldRef Name='PriorityNumber' />"+
					   "<FieldRef Name='ID' />"+
					   "<FieldRef Name='ParkID' />"+
					   "<FieldRef Name='ProjectNumber' />"+
					"</ViewFields></View>");
	  	
		this.collListItem = oList.getItems(camlQuery);
		
		clientContext.load(collListItem);
		
	    clientContext.executeQueryAsync(
	        Function.createDelegate(this, this.onQuerySucceeded),
	        Function.createDelegate(this, this.onQueryFailed)
	        );
	}

var listName="ParkFundingForm";
var viewName = "parkView";

getFieldNames(listName, viewName);
	/*New to make this dynamic-had to get it working first.*/
	var htmlTbl = "<div><button type='Submit'>Prioritize</button></div><table id='prioritize' class='display' cellspacing='0' width='100%'>";
	htmlTbl += "<thead>"+getFieldNames(listName, viewName)+"</thead>";
	htmlTbl += "<tbody>";

	/*Array will contain two values, item id and new priority value. */
	var items=[];

	function onQuerySucceeded(sender, args) {
		var listItemInfo = '';
		var listItemEnumerator = collListItem.getEnumerator();
		
		while (listItemEnumerator.moveNext()) {
			var oListItem = listItemEnumerator.get_current();
			
			htmlTbl += "<tr><td>"+oListItem.get_item('ID')+"</td><td data-order="+oListItem.get_item('PriorityNumber')+"> "+
			"<input type='text' name = 'rowID-"+oListItem.get_item('ID')+"' id='rowID-"+oListItem.get_item('ID')+"' data-curVal="+oListItem.get_item('PriorityNumber')+" value="+oListItem.get_item('PriorityNumber')+"></td>"+
			"<td>"+oListItem.get_item('Title')+"</td>"+
			"<td>"+oListItem.get_item('ProjectName')+"</td>"+
			"<td>"+oListItem.get_item('ProjectNumber')+"</td></tr>";

			
			items.push([oListItem.get_item('ID'),oListItem.get_item('PriorityNumber')]);
		}
		
			htmlTbl += "</tbody></table>";
		
			$("#divListItems").html(htmlTbl);
			
			/*Apply DataTable style */
			var table = $('#prioritize').DataTable();
			
		$('table#prioritize').on('change','input[type=text]',function(e){
				
			//Value being replaced
			var curVal =  $(this).attr('data-curVal');
			
			//New Value
			var newVal = $(this).val();
		    		    
		   //Unique id of List Item. Format: 'rowID-xx' 
		   var itemID =  $(this).attr('id');
		   var id=getId(itemID);   
		   
		   /*Update and Prioritize*/
		   prioritize(newVal, id, curVal);
	    
		});		
	
	
	}
	
	/*Grab the id from the id name. Format: 'rowID-xx'*/
	function getId(iID) {
		var myID = iID.split("-");	
		var ID = myID[1];
		
		return ID;
	}

	function prioritize(uValue, uid, prevVal) {
	        update(uValue, uid);
	        
	  for (x = 0; x < items.length; x++) {
	    for (i = 0; i < 1; i++) {
	      if (items[x][0] === parseInt(uid)) {       	
	        if (uValue > prevVal) {
	       // alert(uValue+">"+prevVal);
	          for (j = parseInt(prevVal) - 1; j < uValue; j++) {
	            if (items[j][0] != uid)
	              items[j][1] = parseInt(items[j][1]) - 1;
	              alert(items[j][1]+"="+parseInt(items[j][1]) - 1);
	          }
	        } else {
	          for (j = parseInt(uValue ) - 1; j < parseInt(prevVal); j++) {
	            if (items[j][0] != uid)
	              items[j][1] = parseInt(items[j][1]) + 1;
	              alert(items[j][1]+"="+parseInt(items[j][1]) + 1);
	          }
	        }
	
	      }
	    }
	  }
	}

	/*Updates item with new priority value*/
	function update(pVal, itID){
		var rowID = parseInt(itID);
		var ptVal = parseInt(pVal);
		var ctx = new SP.ClientContext.get_current();
		var customList = ctx.get_web().get_lists().getByTitle('ParkFundingForm');
		var listItem = customList.getItemById(rowID);
		
		/*Set the value and update*/
		listItem.set_item('PriorityNumber', ptVal);
		listItem.update();
	 
		ctx.executeQueryAsync(
	    	function(){ 
			    		/*Need to change this to show on the page*/
			    		alert('Item updated'); 
			}, 
			
			function(sender, args){ alert('Error: ' + args.get_message()); });
			}
		
			function onQueryFailed(sender, args) {
				alert('Request failed. '+args.get_message() + '\n' + args.get_stackTrace());
	}
	
function getFieldNames(listName,viewName){

   var context = new SP.ClientContext.get_current();
   var web = context.get_web();
   var list = web.get_lists().getByTitle(listName);
   var view = list.get_views().getByTitle(viewName);
   var listFields = view.get_viewFields();
   context.load(listFields);
   context.executeQueryAsync(printFieldNames,onError);


   function printFieldNames() {
      var e = listFields.getEnumerator();
      while (e.moveNext()) {
         var fieldName = e.get_current();
         var buildColumns = "<th>"+fieldName+"</th>";
      }
   }

   function onError(sender,args)
   {
      console.log(args.get_message());
   }

}

Open in new window

LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Jamie McAllister MVPConnect With a Mentor SharePoint ConsultantCommented:
For further clarity; So I'm seeing the query being run async, fine. But in the eventual method to handle the result we call printFieldNames.

In there is this line in a while loop;

var buildColumns = "<th>"+fieldName+"</th>";

This is getting redeclared and overwritten each loop instead of the concat happening. I'm guessing this is where the concat is failing, if successful you could use this in Line 57.... except beware of timing issues as the method is called async. I think the work that line 57 does should be moved into the processing of teh async call later on to avoid issues...

All the above assuming I'm understanding your intent.
0
 
Jamie McAllister MVPSharePoint ConsultantCommented:
I think this might be going a bit wrong on line 57. You call GetFieldNames on line 54, fine, but then again on 57?

I think you should make your variable 'buildColumns' global and put that in the concat statement in line 57.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.