Quantcast
Channel: Developing Apps for SharePoint 2013 forum
Viewing all articles
Browse latest Browse all 8089

Getting data from a host_web list...losing my mind

$
0
0

Last week I started building my first SharePoint App (Office 365, E3). I am using the new Napa development environment.  Progress has been slow but steady, but I appear to have hit a wall. I am hoping someone can help. At this point its fairly simple, its a web form, on document ready it gets the context of the host_web, grabs a list that exists in that host_web and then...Well, what I want it to do is grab the items in that list, and use that data to fill in a <select></select> element on the page.  This is not working. It seems to indicate that I am getting the list (after resetting the permissions of the app), that I am getting the items, but I can't get it to do anything.  I am probably making it more complicated than it needs to be.  From what I've found, I have to use a CAMLQuery to get the data from the list.

My code is below. Line 56(inside onOfficeItemsGetSuccess) is running, it populates the first item of the select. The while loop however never runs. There are 9 "rows"/items in the list.  For this page, all I really need is that first column.

'use strict';

//Used to get the app context and determine who is accessing the app
var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();

//Used to get the host context to view/update lists on the parent site (parent to the app)
var hostweburl;  //The URL of the host_web
var appweburl; //The URL of the application
var hostcontext; //Used to store the current context on the host web
var host; //Used to store reference to the host_web
var list; //Used to store the list being accessed
var listItemCollection; //Used to store list items

//This is the Query to get the office list information, ordered by office name
var OfficeQuery = "<View><RowLimit>20</RowLimit></View>";
//var OfficeQuery = "<View><ViewFields><FieldRef Name='Office' /><FieldRef Name='Manager' /><FieldRef Name='Address' /><OrderBy><FieldRef Name='Office' /></OrderBy></ViewFields></View>";

// This code runs when the DOM is ready and creates a context object which is 
// needed to use the SharePoint object model
$(document).ready(function () {
	
	//Moved these here to ensure that they don't determint their context until after the page loads.
	hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
	appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
	hostcontext = new SP.ClientContext(appweburl);
	host = new SP.AppContextSite(hostcontext, hostweburl);
	//Grab the context and load it
	var web = host.get_web();
	hostcontext.load(web);
	
	//Lets get all the Offices List from the host web
	list = web.get_lists().getByTitle("TeamBuilder-OfficeManagerRelationship");
	//var list = web.get_lists().getByTitle("WackaMole");
	hostcontext.load(list);
	hostcontext.executeQueryAsync(onOfficeListSuccess, onOfficeListFailure);

	getUserName();
});

//On Office List retrival success
function onOfficeListSuccess(){
	//alert("Office list loaded!");
	var camlQuery = new SP.CamlQuery();
	camlQuery.set_viewXml(OfficeQuery);
	listItemCollection = list.getItems(camlQuery);
	hostcontext.load(listItemCollection);
	hostcontext.executeQueryAsync(onOfficeItemsGetSuccess, onOfficeItemsGetFailure);
}

//On Office list items retrival success
function onOfficeItemsGetSuccess(){
	//alert("Office List Item Success!");
	var OfficesEnumerator = listItemCollection.getEnumerator();
	$('#OfficeSelect').append('<option value="0">Choose Prospective Office</option>');
	while(OfficesEnumerator.moveNext()){
		var CurrentOffice = OfficesEnumerator.get_current();
		$('#OfficeSelect').append('<option value =' + CurrentOffice.get_item('Manager') + ">" + CurrentOffice.get_item('Manager') + "</option>");
		//<option value="default">Choose Prospective Office</option>
	}
}
//On Office list items retrival failure
function onOfficeItemsGetFailure(sender, args){
	alert("Failed to get Office List Items " + args.get_message());
}


//On Office List retrival failure
function onOfficeListFailure(sender, args){
	alert("Office list failed to load " + args.get_message());
}

//Sets up the Event for clicking submit
$('#submit').click(onClickSubmit());
	
//Used to pull data from the url - needed to get host context(see above)
function getQueryStringParameter(param) {
    var params = document.URL.split("?")[1].split("&");
    //var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == param) {
            return singleParam[1];
        }
    }
}
	/* ======================= Default Page Functions =========================== */
	// This function prepares, loads, and then executes a SharePoint query to get 
	// the current users information
	function getUserName() {
		context.load(user);
		context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
	}
	// This function is executed if getUserName fails
	function onGetUserNameFail(sender, args) {
		alert('There seems to be an error with your browser session. Please relogin to Office 365.' + args.get_message());
	}

	// This populates the Sponsoring Agent Section of the Default Page
	function onGetUserNameSuccess() {
		$('#SponsoringAgent').html('<table class="prospectiveform"><tr><td>User: </td><td>' + user.get_title() + '</td><td>Email: </td><td>'+ user.get_email() + '</td></tr></table>');
	}
	//function is called when someone clicks the submit button on the prospective agent form
	function onClickSubmit(){
		validateProspectForm();
		//alert('Write the code for Submitting New Agents!!!');
	}
	//Prospect Form Validation
	function validateProspectForm(){

		}
	/* ==================== Manager Page Related ======================= */


Viewing all articles
Browse latest Browse all 8089

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>