Trying to pull current user manager from UserProfileService.asmx

Oct 28, 2010 at 1:19 AM

I am new to Javascript, but have a slight understanding. I am looking for someone to point me in the right direction.

I currently have a custom list, in MOSS with a few fields for user information. I am using SPServices to auto-populate the user's account name, full name, phone number, email address, job title and department. The last field that I need to auto-populate is not found on the page http://{servername}/_layouts/userdisp.aspx?Force=True but it is in Active Directory and the uuser's My Site, so i would assume I can use the UserProfileService to get this information. My question is, how do I do that?

Coordinator
Oct 28, 2010 at 3:56 PM


Ruth:

Here's an example of calling the  function, which ought to give you access to the propoerties you're looking for. All this will do is show you the available values on the page, so I'm sure you'll have some further questions.

M.

 

<script language="javascript" type="text/javascript" src="../Script Files/jquery-1.4.2.min.js"></script> 
<script language="javascript" type="text/javascript" src="../Script Files/jquery.SPServices-0.5.7.min.js"></script> 
<script language="javascript" type="text/javascript"> 

  $(document).ready(function() { 
    $().SPServices({ 
      operation: "GetUserProfileByName", 
      async: false, 
      AccountName: "andermah", 
      completefunc: function (xData, Status) { 
          var out = $().SPServices.SPDebugXMLHttpResult({ 
             node: xData.responseXML, 
             outputId: "#thisId" 
           }); 
           $("#thisId").html("").append("<b>This is the output from the GetUserProfileByName operation:</b>" + out); 
      } 
    }); 
  }); 
</script> 
<div id="thisId"></div>

 

Oct 28, 2010 at 7:06 PM

Ok, this works, I get the output and I see the manager field available to me. And you guessed right, I do have more questions :)

1. Can/How do I fill the AccountName: in the GetUserProfileByName operation with a variable that I've already pulled from the SPServices.SPGetCurrentUser I'm already using in this script to populate the other fields?

And

2. How do I fill the Manager field with from the output I just got? Obviously don't need all that output, how do I only get that one field and fill a field on my form?

Thank you!

Ruth

Coordinator
Oct 29, 2010 at 3:35 PM
Edited Oct 29, 2010 at 4:56 PM

If you replace the line:

AccountName: "andermah", // This is my account name here

with

AccountName: $().SPServices.SPGetCurrentUser(),

you'll have the current user's account name.

I can work something up for you on the second question. What type of column do you want to put the Manager value into and what is its name?

M.

Oct 29, 2010 at 3:42 PM

Awesome. Manager field is Person or Group column named "Manager"

Coordinator
Oct 29, 2010 at 4:55 PM

Ruth:

I think that this will do it for you. Let me know if it works!

M.

$(document).ready(function() { 
 var managerName;
 $().SPServices({ 
  operation: "GetUserProfileByName", 
  async: false, 
  AccountName: $().SPServices.SPGetCurrentUser(),
  completefunc: function (xData, Status) { 
   $(xData.responseXML).find("PropertyData").find("Name[text=Manager]").each(function() {
    managerName = ($(this).parent().find("Values").text());
   });
  } 
 });
 // There's no easy way to find one of these columns; we'll look for the comment with the columnName
 var searchText = RegExp("FieldName=\"Manager\"", "gi");
 // Loop through all of the ms-formbody table cells
 $("td.ms-formbody").each(function() {
     // Check for the right comment
     if(searchText.test($(this).html())) {
         $(this).find("div[Title='People Picker']").html(managerName);
         return false;
     }
 });
}); // End $(document).ready
 
Oct 29, 2010 at 5:08 PM

You are the JQuery Master, Thank you so much, that worked perfectly!

Coordinator
Oct 29, 2010 at 5:10 PM

Excellent! Solution for you, nice blog post for me. ;+)

M.

Nov 10, 2010 at 1:13 PM

One more question. With the field being a person or group field, is there any way I can have it automatically check the name, once its populated and then set the field to read only? Or set it to read only and let the user hit the check name buton? The issue is that the input from the GetUserProfileByName is in the format domain\account where the account names are the person's ID. I would like to display the manager's full name to the user so that the user filling out the form is sure that's the correct manager, without having to lookup the manager's ID. As it is right now, the manager's name isn't verified until the form is submitted. And I need the field to be read only so that they cannot change the name in the field, once populated. Thanks for any help you can give me.

Coordinator
Nov 10, 2010 at 5:07 PM

Do you want to make the field read only to the user entirely? Or do you want them to be able to change it? It seems like you are saying both.

I think what you want is to autopopulate the value (as you're doing), auto-resolve the name, and then make the field read only. Is that right?

M.

Nov 10, 2010 at 5:08 PM

Yes, auto-populate, auto-resolve, then make it read only. Is that possible?

Coordinator
Nov 10, 2010 at 6:56 PM

Try this. It works in my test environment.

M.

$(document).ready(function() {
var managerName;
$().SPServices({
  operation: "GetUserProfileByName",
  async: false,
  AccountName: $().SPServices.SPGetCurrentUser(),
  completefunc: function (xData, Status) {
   $(xData.responseXML).find("PropertyData").find("Name[text=Manager]").each(function() {
    managerName = ($(this).parent().find("Values").text());
   });
  }
});
// There's no easy way to find one of these columns; we'll look for the comment with the columnName
var searchText = RegExp("FieldName=\"Manager\"", "gi");
// Loop through all of the ms-formbody table cells
$("td.ms-formbody").each(function() {
     // Check for the right comment
     if(searchText.test($(this).html())) {
         $(this).find("div[Title='People Picker']").html(managerName);
         $(this).find("a[Title='Check Names']").click();
         $(this).find("div[Title='People Picker']").attr("disabled", "disabled");
         return false;
     }
});
}); // End $(document).ready
Nov 10, 2010 at 8:47 PM

It greys it out, I cannot type into the text box nor remove that name, but I can click the directory button, pick a different name and replace the name that was there originally. Is there anyway to disable the directory button?

Coordinator
Nov 10, 2010 at 9:18 PM

Add this line right before the return false;

         $(this).find("a[Title='Browse']").attr("onclick", "");

That will remove the click event action from the Browse icon.

BTW, another option is to just hide the row in the form which contains the Manager altogether. Since the user won't be able to change anything, is there any benefit to showing it to them? What will they do if they see a problem?

M.

Nov 11, 2010 at 2:20 AM

This works perfectly, thank you very much.

I guess that is in option, didn't really think of that, it was on the existing form I am replacing. However, if they do see a problem, they will notify their correct manager, and also call IT support to get AD updated to the correct name. Can it be populated via JQuery even if the field isn't on the form? Or would that have to happen after the user submits? Either way, awesome solutions, removing or just disabling. Again, you are the greatest!!

Coordinator
Nov 11, 2010 at 2:28 AM
Even if you hide the row by setting display: none; the value will be saved. It all depends on what user experience you are aiming for. It occurred to me after I gave you the last bit above that it might actually be better to hide the Browse icon rather than just removing the click event action. Again, whatever you want to do. Hey, some day you'll hire me. ;-) M.
Mar 16, 2011 at 9:54 AM

Excellent post. Helping me a lot.

Thanks

Feb 11, 2015 at 2:50 PM
I see this is an old post but it's a new problem for me. Unfortunately I'm not a programmer and need a bit more hand-holding. Here's my situation and I think the code above is probably on the right track.

I have a SharePoint form with the same types of common profile fields such as: Name, Job Title, Manager, Department, etc. I need SharePoint to auto-populate those fields. I inserted the code that "sympmarc" provided into a CEWP on the same page as my form. I am also using the code to link to jquery-1.11.2.min.js and jquery.SPServices-0.5.4.min.js. I don't know what most of the code means but I know that I need it. However the fields on my form are still blank when I open a new form.

My questions are: what do I need to do with the code to have it populate the form?

Brian