Help with GetListItems and SPComplexToSimpleDropdown

Jul 3, 2012 at 10:51 PM

Hey,

I know this can be done but I can't seem to piece it together. I would like to customize a copy of my NewForm so that when a value is selected from a particular dropdown list that value would be looked up in a related list and a couple of additional values would be returned and used as the default values for a couple of fields. So here's a quick outline:

1. Main form (maintenance request) has dropdown called Tools and the values for this field come from a different list called Tool_List located in the same site.

2. The Tool_List has Title, Owner, and Manager fields. The Manager and Owner fields are people fields.

Ideally, when a user selects a value from the Tool dropdown (onchange event) the GetListItem code would execute and return the associated Owner and Manager values. The Owner and Manager values would then be used in the maintenance request form to default it's Assigned To and Manager field values.

I've looked at a lot of examples on this site and I think I'm close but I'm not sure how to execute the code on the Tool dropdown's onchange event. Also, to further complicate things my Tool dropdown exceeds 20 items so I think I should be using the SPComplexToComplex service to make things easier but I can seem to get it to work. Or I can't tell if it's working so maybe it's working but my onchange isn't. I'm not getting any errors but none of my test alerts are firing either.

I'm very new to writing scripting code and this is my 2nd attempt at using the jquery and SPServices library so I apologize if this is a stupid question. I did look through the documentation so hopefully I didn't miss something completely obvious.

I have been able to get the SPDisplayRelatedInfo to work on another form so I know my references to both the jquery and SPServices libraries are working.

Thanks for the help - much appreciated!

<script type="text/javascript" language="javascript" src="/LMACLossResRD/SiteAssets/jquery-1.7.2.min.js"></script>
	<script type="text/javascript" language="javascript" src="/LMACLossResRD/SiteAssets/jquery.SPServices-0.7.1a.min.js"></script>
	<script type="text/javascript" language="javascript">
		$(document).ready(function() {
			$().SPServices.SPComplexToSimpleDropdown({	
				columnName: "Tool2",
				//completefunc: null,
				debug: true
				});
		};
		
		$("select[title=SPComplexToSimpleDropdown_Tool2]").change(function() { 				
			var queryStringValues = $().SPServices.SPGetQueryString();
			var ToolName = queryStringValues["SPComplexToSimpleDropdown_Tool2"];
			alert("Tool Name = " + ToolName);
				$().SPServices({
						operation: "GetListItems",
						async: false,
						listName: "Tool_List",
						CAMLQuery: "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + ToolName + "</Value></Eq></Where></Query>",
						completefunc: function (xData, Status) {
								$(xData.responseXML).SPFilterNode("z:row").each(function () {
									
									alert("1");
									
									var OwnerName = $(this).attr("Owner");
									var ManagerName = $(this).attr("Manager");
									
									alert("owner = " + OwnerName + " manager = " + ManagerName);
									
									setFieldValue('AssignedTo', OwnerName);
									setFieldValue('TaskGroup', ManagerName);
									
									alert("after set fields");
								});
							}								
				});
			});
	</script>

Coordinator
Jul 4, 2012 at 4:13 AM

ribrem:

This kind of thing is always tough to debug from afar, of course, because I can't see the underlying data structures. You should get familiar with the debugging tools in Firebug and the IE Developer Tools. Because the dropdown has 20+ values, it will only render as a complex dropdown in IE, so you have to use the Developer tools to some degree.

My guess is that where you're getting stuck is setting the People Pickers. I have a new function in v0.7.2 (currently an alpha) that can help: SPFindPeoplePicker. Each type of SharePoint control has it's own unique markup and in quite a few cases, script to drieve it as well. People Pickers are one of the more complex controls. I don't see your setFieldValue function, but it's probably not setting the People Pickers right.

Hope this helps...

M.

Jul 5, 2012 at 3:53 PM

Thanks for the quick response. Apparently, I’m further from getting this to work than I thought. I never even wrote the setFieldValue function! I just thought it was a function that was part of the jquery library. I need more help than I realized.

So does it make sense what I’m trying to do? Also, the SPFindPeoplePicker looks like it would be really helpful. Below are my thoughts on how to do this. Looking for some feedback to see if this makes sense.

1. Use the SPComplexToSimpleDropdown to convert my dropdown to simple dropdown. Is this necessary and the best approach? The dropdown field is called Tool2 and it’s a lookup column from my Tool_list (Title field).

2. When the user selects a value from the Tool2 dropdown use the GetListItems to look up the Owner and Manager values from the Tool_List. Do I need to use the SPFindPeoplePicker for this? The Tool_List columns are Title, Owner, and Manager. The Title is a text field, and the owner & manager fields are people fields.

3. Write a setFieldValue function and use the SPFindPeoplePicker to find the Assigned To and Manager fields on my NewForm then set the values for these fields to the Owner and Manager values I retrieved using the GetListItems.

I did see a similar discussion to what I’m trying to do but they were using the SPDisplayRelatedInfo (http://spservices.codeplex.com/discussions/216134). In that post you suggested that they use the GetListItem instead so that’s what I’m trying to do.

Tool_List (list that has the data being looked up)

Fields (static names):

1. Title (text)

2. Owner (Person or Group)

3. Manager (Person or Group)

Maintenance Work (list that has the form I’m trying to customize)

Fields (display names):

1. Title (text)

2. Tool Lookup (lookup) - Static Name = Tool2

3. Tool Name (lookup) – Static Name = Tool%5Fx0020%5FLookup%5Fx003a%5FTitle

4. Request Type (choice)

5. Description (text)

6. Status (choice)

7. Importance (choice)

8. Effort/Complexity (choice)

9. Mgt. Review Required (Yes/No)

10. Target Date (Date and Time)

11. Assigned To (Person or Group) – Static Name = AssignedTo

12. Manager (Person or Group) – Static Name = TaskGroup

13. Reviewer (Person or Group) – Static Name = Reviewer

14. Completed Date (Date and Time)

I’m going to spend some time getting familiar with the debug tools you recommended.

Thanks for the help!

Coordinator
Jul 10, 2012 at 3:20 AM

ribrem:

You've asked a bunch of questions above which indicate that you don't quite get the idea yet. (Sorry if that sounds harsh - I don't mean it to.)

A few general answers:

1) You don't have to convert to a simple dropdown if you don't want to, but selects are easier to work with than complex dropdowns.

2) Yes, and no you don't need the People Picker function to *get* the values.

3) This is where you'd use the SPFindPeoplePicker function - to set the People Pickers with the values you've retreived in 2).

M.

Jul 10, 2012 at 9:38 PM

It's all good. This is my first time really writing code in SharePoint outside of some very minor tweaks. I realize I'm stumbling in the dark right now!

I have made some progress so I think I'm getting closer. I've got the following working but I'm having trouble with getting the selected value from the dropdown after changing it from a Complex to a Simple type.

Working
$().SPServices.SPFilterDropdown({
  	relationshipList: "Tool_List",
  	relationshipListColumn: "Title",
  	columnName: "Tool Lookup",
  	CAMLQuery: "<Neq><FieldRef Name='Status' /><Value Type='Text'>Retired</Value></Neq>",
  	//completefunc: null,
  	debug: true
});
			
$().SPServices.SPComplexToSimpleDropdown({	
	columnName: "Tool Lookup",  
	//completefunc: null,
	debug: true
});
			
Not Working		
 $("select[title='SPComplexToSimpleDropdown_Tool2']".change(function() {
	//var queryStringValues = $().SPServices.SPGetQueryString();
	//var ToolName = queryStringValues["SPComplexToSimpleDropdown_Tool2"];
	var ToolName =  $(this).attr("value");
	alert("Tool Name = " + ToolName);
});

Aug 8, 2012 at 4:29 PM

Hey - I've made a ton of progress on this but I need a little help on one part. I can't seem to populate the Manager field (people picker field type) using the SPFindPeoplePicker. Not sure what I'm missing. Everything up to that point in the code is working great.

<script type="text/javascript" language="javascript" src="/LMACLossResRD/Site Assets/jquery-1.7.2.min.js"></script>
<script type="text/javascript" language="javascript" src="/LMACLossResRD/Site Assets/jquery.SPServices-0.7.1a.min.js"></script>
<script type="text/javascript" language="javascript">
	$(document).ready(function() {
		// July 2012. Filter Tool Lookup dropdown so only Active Tools are displayed.
		$().SPServices.SPFilterDropdown({
			relationshipList: "Tool_List",
			relationshipListColumn: "Title",
			columnName: "Tool Lookup",
			CAMLQuery: "<Neq><FieldRef Name='Status' /><Value Type='Text'>Retired</Value></Neq>",
			debug: false, // Set to true in dev. 
			completefunc: null			
		});
		
		// July 2012. Convert the Tool Lookup field from a Complex type (more than 20 values) to a Simple type.
			// Simple types are easier to work with. 
		$().SPServices.SPComplexToSimpleDropdown({	
			columnName: "Tool Lookup",
			debug: false, // Set to true in dev. 
			completefunc: null								
			});
		
		// August 2012. Added as the default option. 
		$("#SPComplexToSimpleDropdown_Tool2").prepend("<option value='0' selected='selected'> Select Tool </option>").val('0');

		// August 2012. Added. Get the manager of the selected tool and use it as the default value for the manager field. 
		$("#SPComplexToSimpleDropdown_Tool2").change(function() { 
			var SelToolId = $("#SPComplexToSimpleDropdown_Tool2").val()
			if (SelToolId != 0)
				{alert ("The selected value is " + SelToolId);
					$().SPServices({
							operation: "GetListItems",
							async: false,
							listName: "Tool_List", 
							CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Counter'>" + SelToolId + "</Value></Eq></Where></Query>",
							completefunc: function (xData, Status) {
									$(xData.responseXML).SPFilterNode("z:row").each(function() {
									alert("Inside SPServices function");
									var splitManager = $(this).attr("ows_Manager").split(";#");
									
									Manager = splitManager[1];
									
									alert("Manager is " + Manager);
									
									if (Manager != '')
										{alert("Inside Manager If")
										// Not Working
											$().SPFindPeoplePicker({
													peoplePickerDisplayName: "Manager",
													valueToSet: Manager,
													checkNames: true
												});										
										}
								});
							}
						});
				} 
		});
		
	 });
</script>
Coordinator
Aug 8, 2012 at 5:53 PM

Easy answer: SPFindPeoplePicker is a new function that is only in the v0.7.2 alpha; it's not in v0.7.1a.

M.

Aug 8, 2012 at 6:27 PM

Got it. So do you have an idea of when v0.7.2 will be released? I'm not sure I want to use the alpha version but may need to depending on when the released version will be available.

Thanks!

Aug 8, 2012 at 7:28 PM
Edited Aug 8, 2012 at 7:34 PM

When I need to set a people picker field in a form I use this jquery: 

var peoplepicker = $("tr:contains('Manager'):last").find("div[title='People Picker']");
peoplepicker.html(manager);

This should find the first people picker whose display name is 'Manager', and set it to the value of the string-variable, manager. In reality, this jquery searches for the tr element that has 'Manager' in it, and normally this tr element has two td elements. Inside the first td element it has an h3 element, then lastly a nobr whose innerHTML has the display name , unless you manually changed that nobr HTML in a custom form (whew!). I believe the string-variable, manager, should just contain the person's name in this format LastName, FirstName MiddleInitial(if applicable).

Coordinator
Aug 8, 2012 at 8:26 PM

I keep hoping to get v0.7.2 out there, but I've been too busy. It's tough when it's just for fun. ;+)

M.

Aug 8, 2012 at 8:44 PM

Thanks RidesTheShortBus! I tried your solution and it worked great.

Mark thanks for all the help and this code library. Incredibly helpful and much appreciated!

I'm looking forward to the next release and will put the SPFindPeoplePicker to good use when it's available. :-)