GetUserObj Rerurn all fiels from /_layouts/userdisp.aspx

Jul 29, 2010 at 4:24 PM
Edited Jul 30, 2010 at 7:41 AM

Hello! I've made a function based on $().SPServices.SPGetCurrentUser, witch return user with all fields from /_layouts/userdisp.aspx

	$.fn.SPServices.SPGetUserObj = function(options) {

		var opt = $.extend({}, {
			fieldName: "Name",			// Specifies which field to return from the userdisp.aspx page. Put "*" (with quotes) to get all fields
			userID: "", 				// Spesifies ID of the user. By default is empty i.e. the current user will be returned 
			debug: false				// If true, show error messages; if false, run silent
		}, options);

		var user = {length:0};			//Number of fields returned from userdisp.aspx
		var userIDStr = (opt.userID) ? "&id=" + opt.userID : ""; 
		var getFieldValue = function(obj){
			var fieldValue = "";
			// Each fieldtype contains a different data type, as indicated by the id
			switch($(obj).attr("id")) {
				case "SPFieldText":
					fieldValue = $(obj).text();
					break;
				case "SPFieldNote":
					fieldValue =  $(obj).find("div").html();
					break;
				case "SPFieldURL":
					fieldValue =  $(obj).find("img").attr("src");
					break;
				// Just in case
				default:
					fieldValue =  $(obj).text();
					break;
			}
			return fieldValue ? fieldValue.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '') : fieldValue;
		};
		
		var getFieldInternalName = function(obj){
			return String($(obj).html().match(/FieldInternalName.*/i)).split('=')[1].replace(/\"/ig, "");
		};
		
		$.ajax({
 			// Need this to be synchronous so we're assured of a valid value
 			async: false,
 			// Force parameter forces redirection to a page that displays the information as stored in the UserInfo table rather than My Site.
 			// Adding the extra Qjuery String parameter with the current date/time forces the server to view this as a new request.
 			url: $().SPServices.SPGetCurrentSite() + "/_layouts/userdisp.aspx?Force=True" + userIDStr +"&"+ new Date().getTime(),
			complete: function (xData, Status) {
				$(xData.responseText).find("table.ms-formtable td[id^='SPField']").each(function() {
					if (opt.fieldName == '*'){
						user[getFieldInternalName(this)] = getFieldValue(this);
						user.length ++;
					}
					else {
						var thisTextValue = RegExp("FieldInternalName=\"" + opt.fieldName + "\"", "gi");
						if(thisTextValue.test($(this).html())) {
							user[opt.fieldName] = getFieldValue(this);
							user.length ++;
						}
					}
				});
			}
		});
		return user;
	};


Coordinator
Jul 30, 2010 at 2:25 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.