Assistance Needed with SPServices GetListItems Function

May 22, 2012 at 11:29 AM

Hi

If anyone could help me then you might be able to save my head from further bashing against my desk.

My requirement is fairly simple - I am writing a piece of Javascript which is attempting to look up a value from one list to use in another list. The obvious choice is to use GetListItems in the awesome SPServices library. I have never used this function before however and I seem to be missing something fundamental.

The list that I am looking up from is "Heatmap Skill Codes" and I am attempting to return the value of the "Skill Title" field based on the "Skill Code" field that matches the alpCode parameter. Then I am setting the alpTitle variable to the "Skill Title" field content. Then I want to insert it into a field on the SharePoint form (though even putting it into an alert box would be nice as can be seen at the bottom of my script.

Essentially my script looks like this ....

var queryStringValues = $().SPServices.SPGetQueryString();
var alpCode = queryStringValues["ALP"];
alert("NewValue = " + alpCode);

$(document).ready(function() {
	var alpTitle = "";

	var startDateVar = new Date();
	var endDateVar = new Date();
	endDateVar.setDate(startDateVar.getDate() + 42);
	var startDate = startDateVar.getDate() + "/" + (startDateVar.getMonth() + 1) + "/" + 

startDateVar.getFullYear();
	var endDate = endDateVar.getDate() + "/" + (endDateVar.getMonth() + 1) + "/" + 

endDateVar.getFullYear();
	$("input[title='Proposed Start Date']").val(startDate);
	$("input[title='Proposed End Date']").val(endDate);

        var query = "<Query><Where><Eq><FieldRef Name='Skill Code'/><Value Type='Text'>" + 

alpCode + "</Value></Eq></Where></Query>";

	$().SPServices({
		operation: "GetListItems",
		async: false,
		listName: "Heatmap Skill Codes",
		CAMLQuery: query,
		completefunc: function (xData, Status) {
			$(xData.responseXML).find("[nodeName='z:row']").each(function () {
				alpTitle = $(this).attr("Skill Title");
				alert("in function " + alpTitle);
				$("input[title='Title']").val(alpTitle);
			});
		},
	})
	alert("outside function " + alpTitle);
});

Am I missing something really stupid ?  Probably.

Coordinator
May 22, 2012 at 12:23 PM
Edited May 22, 2012 at 2:07 PM

You haven't said exactly where things aren't working. However, if you are using jQuery 1.7+, the .find("[nodeName='z:row']") syntax is no longer valid. you should use the SPFilterNode function instead.

M.

May 22, 2012 at 12:47 PM

Thank you so much for your response. You are quite right - the fact that I forgot to put the actual problem in that post is an indication of how much of a headache I have I guess - lol

I am using jQuery-1.7.2.min.js and SPServices0.7.1a.min.js

I have amended the code so that the completefunc part is as follows ....

		completefunc: function (xData, Status) {
			$(xData.responseXML).SPFilterNode("z:row").each(function () {
				var alpTitle = $(this).attr("BT Skill Title");
				setFieldValue('Title', alpTitle);
			});

I was hoping that the value from my list item would make its way into the alpTitle variable and then the 'Title' field on my newform.aspx would be set to that value by the call to setFieldValue. However, the 'Title' field on the newform remains blank and the 'alert("Outside function " + alpTitle);' shows that alpTitle is still blank.

May 22, 2012 at 1:16 PM

OK - feeling a bit stupid now but I still have a problem.

I noticed the spurious comma after the curly brackets after the completefunc call. I have removed this and added some debug alerts but I am not seeing the debug alerts "1" and "2" so I reckon I have a fundamental flaw in my syntax for this code.

Here's the new code ...

var queryStringValues = $().SPServices.SPGetQueryString();
var alpCode = queryStringValues["ALP"];
alert("NewValue = " + alpCode);

$(document).ready(function() {
	var alpTitle = "";
	var startDateVar = new Date();
	var endDateVar = new Date();
	endDateVar.setDate(startDateVar.getDate() + 42);
//Set date items in NewForm
	var startDate = startDateVar.getDate() + "/" + (startDateVar.getMonth() + 1) + "/" + 

startDateVar.getFullYear();
	var endDate = endDateVar.getDate() + "/" + (endDateVar.getMonth() + 1) + "/" + 

endDateVar.getFullYear();

        var query = "<Query><Where><Eq><FieldRef Name='BT Skill Code' /><Value Type='Text'>" + 

alpCode + "</Value></Eq></Where></Query>";
alert("about to call spservices");
	$().SPServices({
		operation: "GetListItems",
		async: false,
		listName: "Heatmap Skill Codes",
		CAMLQuery: query,
		completefunc: function (xData, Status) {
			$(xData.responseXML).SPFilterNode("z:row").each(function () {
				alert("1");
				var alpTitle = $(this).attr("BT Skill Title");
				alert("in function " + alpTitle);
				setFieldValue('Title', alpTitle);
				alert("2");
			});
		}
	})
	alert("outside function");
//	alert("outside function " + alpTitle);
});
</script>

I see the initial alert as well as the "about to call spservices" alert and the "outside function" alert. I do not see the alerts within the completefunc call and the Title on my form is still not being set. Any ideas would be much appreciated ?

May 22, 2012 at 1:42 PM

Try changing your SPService call to something like this:

	$().SPServices({
		operation: "GetListItems",
		async: false,
		listName: "Heatmap Skill Codes",
		CAMLQuery: query,
		completefunc: function (xData, Status) {
			alert( xData.responseText );
			alert( Status );
			$(xData.responseXML).SPFilterNode("z:row").each(function () {
				alert("1");
				var alpTitle = $(this).attr("BT Skill Title");
				alert("in function " + alpTitle);
				setFieldValue('Title', alpTitle);
				alert("2");
			});
		}
	});

This will alert the AJAX call's response to you.  This will provide a little bit more info as to what may be happening in the background.  Post the results here, so we can get you fixed up. 

 

Cheers,

Matt

May 22, 2012 at 2:05 PM

Hi Matt

Again - thank you for responding so quickly.

I did as you said and got an error back. I hope I got it all right - had to type it from the message window ....

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://schemas.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://schemas.w3.org/2001/XMLSchema"> <soap:Body> <soap:Fault> <faultcode> soap:Server</faultCoide> <faultstring> Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.</faultstring> <detail> <errorstring xmlns="http://schemas.microsoft.com/sharepoint/soap/"> One or more field types are not installed properly. Go to the list settings page to delete these fields.</errorstring> <errorcode xmlns="http://schemas.microsoft.com/sharepoint/soap/"0x81020014</errorcode></soap:Fault></soap:Body></soap:Envelope>
The next alert box said "error"

Coordinator
May 22, 2012 at 2:10 PM

Ah.

<FieldRef Name='BT Skill Code' />

isn't valid, as you need to provide the InternalName for the column, not the DisplayName. It's likely BT_x0020_Skill_x0020Code, but you should check.

M.

May 22, 2012 at 3:17 PM

You have saved me a large headache there. The function runs fine in itself now. However my SetFieldValue is now not working.

The code is now ...

$().SPServices({
		operation: "GetListItems",
		async: false,
		listName: "Heatmap Skill Codes",
		CAMLQuery: query,
		completefunc: function (xData, Status) {
			alert( xData.responseText );
			alert( Status );
			$(xData.responseXML).SPFilterNode("z:row").each(function () {
				alert("1");
				alpTitle = $(this).attr("BT_x0020_Skill_x0020Title");
				alert("in function " + alpTitle);
				setFieldValue('Title', alpTitle);
				alert("2");
			});
		}
	});

The setFieldValue is supposed to be setting the retrieved variable into the Title field on the NewForm, but iIt stops at the setFieldValue with the following error

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; NGD_build; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; MS-RTC LM 8)
Timestamp: Tue, 22 May 2012 14:12:53 UTC

Message: Object expected
Line: 1079
Char: 5
Code: 0
URI: https://office.bt.com/sites/PCLD/LDO/LDPrivateTeamArea/LDRR/Lists/Approvals/NewForm.aspx?ALP=BTSKILL02795

I am sorry to have taken up so much time with this one. 
May 22, 2012 at 3:28 PM

IGNORE THAT LAST POST !!!!!  I think I figured it out.

Will get back to you in 5 minutes when I have found out if I am right.

May 22, 2012 at 3:56 PM

You guys are genius !  Thank you so much for all of your help. It's working a treat now and I hope you don't mind if I don't tell you what I was doing wrong 2 posts ago - lol

Thanks for all your time.

Ben

Coordinator
May 22, 2012 at 5:12 PM

Glad you got it, Ben.

M.