Loading Terms store using JSOM error SCRIPT5022: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

I have javascript code to load terms from a termstore using purely JSOM. Here is the full code snippet in test page called test.aspx:

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Inherits="System.Web.UI.Page" MasterPageFile="../_catalogs/masterpage/virgin.master" Title="JSCOM Example Demo" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<style type="text/css">
	.to-be-removed{
		text-decoration:blink;
		background:orange;
		color:#000;
	}
	.removed {
		text-decoration:line-through;
		background:yellow;
		color:red;
	}	
	.checkbox-label{ padding:5px; }
}
</style>
	<script src="../_catalogs/masterpage/js/jquery.js" type="text/javascript"></script>
	<script src="/_layouts/15/SP.Runtime.js" type="text/javascript"></script>
<script src="/_layouts/15/SP.js" type="text/javascript"></script>
<script src="/_layouts/15/SP.Taxonomy.js" type="text/javascript"></script>


	<script type="text/javascript">
		
		
		$(document).ready(function () {
			
				execOperation();
				
				$(document).on("click","#load_btn",function(){
					execOperation();
				});
				$(document).on("click","#del_btn",function(){
					//Current Context
					var context = SP.ClientContext.get_current();
					//Current Taxonomy Session
					var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
					//Term Stores
					var termStores = taxSession.get_termStores();
					//Name of the Term Store from which to get the Terms.
					var termStore = termStores.getByName("Managed Metadata Service Proxy"); 
					//GUID of Term Set from which to get the Terms.
					var termSet = termStore.getTermSet("8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f");

					var checked = $('input[type="checkbox"]:checked');
					checked.each(function() {
						var elVal = $(this);
						var termid = elVal.val();
						cleanElements(elVal, false)						
						var term = termSet.getTerm(termid);						
						term.deleteObject();																		
				    });	
				    
				    
					//send the updates
					context.executeQueryAsync(function () {
						checked.each(function() {
							cleanElements($(this), true)
						});
						/*
						var len = deleted.length;
						for(var i= 0; i<len;i++){
							var elVal = deleted[i];
							cleanElements(elVal, true)
						};	
						*/
					}, function (error){						
						alert("failed");						
					});				
					
				});
		}); //END document.ready section
			
			
		
		function execOperation(){
			$("#outputTerms").html("<h2>Loading... Please wait!</h2>");
			//Current Context
			var context = SP.ClientContext.get_current();
			//Current Taxonomy Session
			var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
			//Term Stores
			var termStores = taxSession.get_termStores();
			//Name of the Term Store from which to get the Terms.
			var termStore = termStores.getByName("Managed Metadata Service Proxy"); 
			//GUID of Term Set from which to get the Terms.
			var termSet = termStore.getTermSet("8ed8c9ea-7052-4c1d-a4d7-b9c10bffea6f");
			var terms = termSet.getAllTerms();
			context.load(terms);
			context.executeQueryAsync(function(){
				var termEnumerator = terms.getEnumerator();
				var termList = "";
				while(termEnumerator.moveNext()){
					var currentTerm = termEnumerator.get_current();
					termList += "<input id='" + currentTerm.get_id() + "' name='myterms' value='" + currentTerm.get_id() + "' type='checkbox'><span class='checkbox-label' id='" + currentTerm.get_id() + "-text'>" + currentTerm.get_name() + "</span><br/>";
				}
				$("#outputTerms").html(termList);
			},function(sender,args){
				console.log(args.get_message());
			});
		} //end execOperation()
		function cleanElements(elVal, removed){
			var termid = elVal.val();
			var elText = $("#" + termid + "-text");
			var termname = elText.text();				
			if(removed){
				console.log(">> Deleted..." + termname + " ('" + termid + "') successfully!");
				elText.removeClass("to-be-removed");
				elText.addClass("removed");
				$("#" + termid).remove();
			}else{
				console.log("Deleting..." + termid + " - " + termname);
				elText.addClass("to-be-removed");
			} 
		} //end func		
	</script>
	
<h2>Terms</h2>
	<div id="outputTerms"></div>
	<input type="button" value="Delete" id="del_btn"/> 	<input type="button" value="Reload" id="load_btn"/>

</asp:Content>

I also have 3 environments DEV, UAT and PRODUCTION. Now this code works perfectly on DEV, but on UAT and PRODUCTION it does not work, giving this error:

SCRIPT5022: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. 


The error occurs in function execOperation() on the line:

var termEnumerator = terms.getEnumerator();

I have also dones some investigation of what is being send to the server between DEV and UAT environment and I noticed that on DEV where it's working, the XML Request has the following:

<ChildItemQuery SelectAllProperties="true"><Properties /></ChildItemQuery>

which is not in the UAT or PRODUCTION requests where it's NOT working. I am a bit confused because it's exactly the same code with a user that has full control on the site etc. Any pointers will be greatly appreciated.

July 17th, 2015 11:19am

Hi,

According to your description, my understanding is that you want to load term store values using JavaScript Client Object Model.

For this error, I suggest you can debug using Internet Explorer Developer Tool to see in which line throws the error. Also, in JavaScript Client Object Model, you need to load a collection before using it like below.

var termStores = taxSession.get_termStores();
context.load(termstores);

Here is a detailed code demo for you reference:

Accessing Terms in Term Store using JSOM in Sharepoint 2013

Thanks

Best Regards

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2015 3:34am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics