Modification on SPServices.SPGetCurrentUser

Mar 1, 2011 at 1:45 PM

Mark,
first of all, I thank you for this great plugin. Help me in my daily work by proposing new solutions and promoting the use of collaborative portals through "sharepoint".
Well, the purpose of this discussion is to send a small fix that I've made to version-0.5.8.js jquery.spservice, this need was born because the home page of a solution is needed to show the current user with the corresponding image.

When using the SPService some users had problems because they had no image assigned.

ERROR: Variable undefined

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

  var opt = $.extend({}, {
   fieldName: "Name",    // Specifies which field to return from the userdisp.aspx page
   debug: true     // If true, show error messages;if false, run silent
  }, options);

  var thisField = "";
  var thisTextValue = RegExp("FieldInternalName=\"" + opt.fieldName + "\"", "gi");
  $.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&" + new Date().getTime(),
   complete: function (xData, Status) {
    $(xData.responseText).find("table.ms-formtable td[id^='SPField']").each(function() {
     if(thisTextValue.test($(this).html())) {
      // Each fieldtype contains a different data type, as indicated by the id
      switch($(this).attr("id")) {
       case "SPFieldText":
        thisField = $(this).text();
         break;
       case "SPFieldNote":
        thisField = $(this).find("div").html();
         break;
       case "SPFieldURL":
        thisField = $(this).find("img").attr("src");
         break;
        // Just in case
        default:
        thisField = $(this).text();
         break;      
      }
      // Stop looking;we're done
      return false;
     }
    });
   }
  });
  var returnedvalue ="";
  if (thisField == null){
    returnedvalue ="no data";
   }else{
    returnedvalue = thisField.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
   }
 return returnedvalue;
 };

SOLUTION:
I ADD A "IF STATMENT" TO VALIDATE THE RETURNED VALUE.

 

Coordinator
Mar 1, 2011 at 10:23 PM

isahack:

I think that this is something that you should handle outside the function. While "no data" may be a good value for you, others may want to see the null and test for it externally. (Remember other languages, for instance.) So if you call the function, test the result which is returned:

var thisUser = $().SPServices.SPGetCurrentUser();
var userLoginName = thisUser == null ? "no data" : thisUser;

M.