SPGetCurrentUser populating text fields

May 2, 2011 at 3:31 PM

Hello, and thanks a lot for sharing your knowledge, it is much appreciated esopecially by those like me trying to learn the different ways of doing things in SP other then the GUI.

My conundrum I believer is a simple one and apologize if it's been brought up before. I have researched the forum but some of the responses are a little different. Anyways, the code below seems pretty self explanatory for obtaining the desired fields (via their internal name).

<script language="javascript" type="text/javascript">
$(document).ready(function() {

var userDepartment = $().SPServices.SPGetCurrentUser({
fieldName: "Department"
});

$("input[Title='Department']").val(userDepartment);

var userManager = $().SPServices.SPGetCurrentUser({
fieldName: "Manager",
debug: true

});

However, to populate those defined variables in a respective text box,, is is as simple as now just adding before debug:true:

textbox1 = userDepartment
textbox2 = userManager

?

Thanks!!

 

 

 

Coordinator
May 2, 2011 at 3:37 PM

carlw:

Not quite, but you're not far off. Probably something more like this:

<script language="javascript" type="text/javascript">
$(document).ready(function() {
  var userDepartment = $().SPServices.SPGetCurrentUser({
    fieldName: "Department"
  });
  var userManager = $().SPServices.SPGetCurrentUser({
    fieldName: "Manager", 
  });
  $("input[Title='Department']").val(userDepartment);
  $("input[Title='Manager']").val(userManager);
});
</script>
Note that Manager is not one of the available values with SPGetCurrentUser (see the docs) unless you've done something to make it available.

M.

May 2, 2011 at 3:52 PM

Dohh!! Thanks.

Man you’re a busy guy. It would’ve been nice if your jquery services functions’ capabilities were built-in to SP like choosing a web part. There are some really slick little functions in there!! Good job.

Carl

From: sympmarc [email removed]
Sent: Monday, May 02, 2011 10:37 AM
To: Williams,Carl [Ontario]
Subject: Re: SPGetCurrentUser populating text fields [SPServices:256000]

From: sympmarc

carlw:

Not quite, but you're not far off. Probably something more like this:

<script language="javascript" type="text/javascript">
$(document).ready(function() {
  var userDepartment = $().SPServices.SPGetCurrentUser({
    fieldName: "Department"
  });
  var userManager = $().SPServices.SPGetCurrentUser({
    fieldName: "Manager", 
  });
  $("input[Title='Department']").val(userDepartment);
  $("input[Title='Manager']").val(userManager);
});
</script>

Note that Manager is not one of the available values with SPGetCurrentUser (see the docs) unless you've done something to make it available.

M.

May 2, 2011 at 3:54 PM

Sorry, M. I didn’t see your last statement. Do you mean custom profile properties aren’t pulled in?

CW

From: sympmarc [email removed]
Sent: Monday, May 02, 2011 10:37 AM
To: Williams,Carl [Ontario]
Subject: Re: SPGetCurrentUser populating text fields [SPServices:256000]

From: sympmarc

carlw:

Not quite, but you're not far off. Probably something more like this:

<script language="javascript" type="text/javascript">
$(document).ready(function() {
  var userDepartment = $().SPServices.SPGetCurrentUser({
    fieldName: "Department"
  });
  var userManager = $().SPServices.SPGetCurrentUser({
    fieldName: "Manager", 
  });
  $("input[Title='Department']").val(userDepartment);
  $("input[Title='Manager']").val(userManager);
});
</script>

Note that Manager is not one of the available values with SPGetCurrentUser (see the docs) unless you've done something to make it available.

M.

Coordinator
May 2, 2011 at 4:01 PM

No, custom profile properties aren't pulled in with SPGetCurrentUser. It's actually a bit of a kludge, and scrapes the values from the /_layouts/userdisp.aspx?Force=True page, as mentioned in the docs.

However, you can grab values from the user profile using the UserProfileService.

SPGetCurrentUser is great (IMO) because it works with WSS 3.0 and up.

M.

May 2, 2011 at 4:09 PM

Yes, I was reading about the UserProfileService on your CodePlex page last night.

Cw

From: sympmarc [email removed]
Sent: Monday, May 02, 2011 11:02 AM
To: Williams,Carl [Ontario]
Subject: Re: SPGetCurrentUser populating text fields [SPServices:256000]

From: sympmarc

No, custom profile properties aren't pulled in with SPGetCurrentUser. It's actually a bit of a kludge, and scrapes the values from the /_layouts/userdisp.aspx?Force=True page, as mentioned in the docs.

However, you can grab values from the user profile using the UserProfileService.

SPGetCurrentUser is great (IMO) because it works with WSS 3.0 and up.

M.

May 2, 2011 at 6:20 PM

As per your repose to use GetUserProfileByName, I got this from one of your blogs http://sympmarc.com/2010/10/29/capturing-the-current-users-manager-in-moss-using-spservices-and-getuserprofilebyname/.

How about this to get custom property profies?

$(document).ready(function() {

var userRegion;

$().SPServices({

operation: "GetUserProfileByName",

async: false,

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

completefunc: function (xData, Status) {

$(xData.responseXML).find("PropertyData").find("Name[text=Region]").each(function() {userRegion = ($(this).parent().find("Values").text());

});

}

{

var userDistrict;

$().SPServices({

operation: "GetUserProfileByName",

async: false,

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

completefunc: function (xData, Status) {

$(xData.responseXML).find("PropertyData").find("Name[text=District]").each(function() {userDistrict = ($(this).parent().find("Values").text());

});

}

$("input[Title='textbox1']").val(userRegion);

$("input[Title='textbox2']").val(userManager);

});

From: sympmarc [email removed]
Sent: Monday, May 02, 2011 10:37 AM
To: Williams,Carl [Ontario]
Subject: Re: SPGetCurrentUser populating text fields [SPServices:256000]

From: sympmarc

carlw:

Not quite, but you're not far off. Probably something more like this:

<script language="javascript" type="text/javascript">
$(document).ready(function() {
  var userDepartment = $().SPServices.SPGetCurrentUser({
    fieldName: "Department"
  });
  var userManager = $().SPServices.SPGetCurrentUser({
    fieldName: "Manager", 
  });
  $("input[Title='Department']").val(userDepartment);
  $("input[Title='Manager']").val(userManager);
});
</script>

Note that Manager is not one of the available values with SPGetCurrentUser (see the docs) unless you've done something to make it available.

M.

Coordinator
May 2, 2011 at 6:53 PM

That looks good, but there's no need to make two calls to the Web Service:

 $(document).ready(function() {
  var managerName;
  var lastName;
  $().SPServices({
    operation: "GetUserProfileByName",
    async: false,
    AccountName: $().SPServices.SPGetCurrentUser(),
    completefunc: function (xData, Status) {
      managerName = $(xData.responseXML).find("PropertyData > Name[text='Manager']").parent().find("Values").text();
      lastName = $(xData.responseXML).find("PropertyData > Name[text='LastName']").parent().find("Values").text();
    }
  });
  alert(managerName);
  alert(lastName);
}); // End $(document).ready

M.

May 2, 2011 at 6:59 PM

Thanks for the nurturing J You’re right of course. Cheers. I’ll update the post when all goes well.

CW

From: sympmarc [email removed]
Sent: Monday, May 02, 2011 1:53 PM
To: Williams,Carl [Ontario]
Subject: Re: SPGetCurrentUser populating text fields [SPServices:256000]

From: sympmarc

That looks good, but there's no need to make two calls to the Web Service:

 $(document).ready(function() {
  var managerName;
  var lastName;
  $().SPServices({
    operation: "GetUserProfileByName",
    async: false,
    AccountName: $().SPServices.SPGetCurrentUser(),
    completefunc: function (xData, Status) {
      managerName = $(xData.responseXML).find("PropertyData > Name[text='Manager']").parent().find("Values").text();
      lastName = $(xData.responseXML).find("PropertyData > Name[text='LastName']").parent().find("Values").text();
    }
  });
  alert(managerName);
  alert(lastName);
}); // End $(document).ready

M.

May 3, 2011 at 8:52 PM

I can't use this function: $().SPServices.SPGetCurrentUser

I link this files in the pageheader: 

<script src="../Js/jquery-1.4.2.min.js" type="text/javascript">

</script><script src="../Js/jquery.SPServices-0.5.6.min.js" type="text/javascript"></script>

and use this script:

<script type="text/javascript">   var user = $().SPServices.SPGetCurrentUser({fieldName: "Name",debug: false});alert(user);</script>

I get this error: Acces is denied

 

I tried to change the version of the jquery and SPServices to

<script src="../Js/jquery-1.6.min.js" type="text/javascript"></script>

<script src="../Js/JScriptLib.js" type="text/javascript"></script><script src="../Js/jquery.SPServices-0.6.1.min.js" type="text/javascript"></script>

 

Now, the <script type="text/javascript">   var user = $().SPServices.SPGetCurrentUser({fieldName: "ID",debug: false});alert(user);</script>

works, but with the Title, Name and Account parameters the function returns an empty string.

Can anybody help me solve this problem?

Thanks in advance!

Coordinator
May 3, 2011 at 9:23 PM

spawn85:

If you read the docs for the SPGetCurrentUser function, what it does is "scrape' the values from _layouts/userdisp.aspx. Try to go directly to that page and see if you can reach it. It's the same page you get if you go to "My Settings" in the Welcome ECB or append /_layouts/userdisp.aspx?Force=True to the current site.

M.

May 3, 2011 at 11:15 PM
sympmarc wrote:

spawn85:

If you read the docs for the SPGetCurrentUser function, what it does is "scrape' the values from _layouts/userdisp.aspx. Try to go directly to that page and see if you can reach it. It's the same page you get if you go to "My Settings" in the Welcome ECB or append /_layouts/userdisp.aspx?Force=True to the current site.

M.

Dear sympmarc, thanks for your quick reply. I read the docs and I tried to browse the userdisp.aspx. I can reach it. The fields I'm trying to return are visible, the page shows my Account, and Name. However, the SPGetCurrentUser can't reach them. I use Sharepoint 2010 Foundation.



Coordinator
May 4, 2011 at 2:11 AM

Hmm. OK, that was the easy thing to check.

I can't think of anything else obvious. You'll probably need to do some debugging to see what's going on. If you take a look at the network traffic with Firebug or Fiddler, you ought to be able to see more detail on what is causing the access issue.

M.

May 4, 2011 at 9:20 AM

As I do not have SP2010 I can not fully check, but in WSS 3 or SP2007, our users will have the same issue unless they are given read rights to the User Information List. What I do, is go to this list and go to the permissions for the list and ensure that "Authenticated Users" have read access. I also give all users the "Enumerate Permissions" permission. I am not saying it is the best solution, but for whatever reason, it works for us. Users can then do this and it works fine. Your mileage may vary!

Coordinator
May 4, 2011 at 1:40 PM

SPGetCurrentUser only looks at the *current* user's information, and by default everyone should have access to that. But your points as valid, of course, Dan.

One other thought: Might you be trying to read this information across Site Collections or Web Applications in your environment? (There are so many crazy variations out there that I can't even think of them all, much less debug them.)

M.

May 4, 2011 at 6:20 PM
spevilgenius wrote:

As I do not have SP2010 I can not fully check, but in WSS 3 or SP2007, our users will have the same issue unless they are given read rights to the User Information List. What I do, is go to this list and go to the permissions for the list and ensure that "Authenticated Users" have read access. I also give all users the "Enumerate Permissions" permission. I am not saying it is the best solution, but for whatever reason, it works for us. Users can then do this and it works fine. Your mileage may vary!

If I try to browse my page with this javascript code, fiddler show an "HTTP/1.1 401 Unauthorized" response. Where can i grant permissons for the User information list? I tried to grant permissions on this link:

http://localhost:6666/_catalogs/users/detail.aspx

But i can't see the ribbon with the "Grant  permissions" button. ( I am site collection administrator)

May 4, 2011 at 6:40 PM
Edited May 4, 2011 at 6:41 PM

Hi Marc, just an updfate. As per your instructions, this pulled in two custom profile properties and populated the two fields on the form:

<script type="text/javascript" src="http....../central/JQuery/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="http..../central/JQuery/jquery.SPServices-0.6.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  var userRegion;
  var userDistrict;
  $().SPServices({
    operation: "GetUserProfileByName",
    async: false,
    AccountName: $().SPServices.SPGetCurrentUser(),
    completefunc: function (xData, Status) {
      userRegion = $(xData.responseXML).find("PropertyData > Name[text='Region']").parent().find("Values").text();
      userDistrict = $(xData.responseXML).find("PropertyData > Name[text='District']").parent().find("Values").text();
    }
  });
  alert(userRegion);
  alert(userDistrict);
  $("input[Title='Region']").val(userRegion);
  $("input[Title='District']").val(userDistrict);
}); // End $(document).ready
</javascript>

Once again thanks for the assistance, much much appreciated!!

Carl

Coordinator
May 4, 2011 at 6:58 PM

spawn85:

This http://localhost:6666 is a clue. You need to be accessing the page via the right DNS name. If not, you'll potentially be crossing domains, which won't work in script.

M.

Coordinator
May 4, 2011 at 6:59 PM
Edited May 4, 2011 at 6:59 PM

Carl:

Awesome. Glad it worked for you!

M.

May 4, 2011 at 7:00 PM

Me too!! It doesn’t like people picker though very much. Searching around for egs.

CW

From: sympmarc [email removed]
Sent: Wednesday, May 04, 2011 1:59 PM
To: Williams,Carl [Ontario]
Subject: Re: SPGetCurrentUser populating text fields [SPServices:256000]

From: sympmarc

Carl:

Awesome. Gald it worked for you!

M.

Coordinator
May 4, 2011 at 7:22 PM

Carl:

Undoubtedly solvable. What are you trying to do with the People Picker?

M.

May 4, 2011 at 7:28 PM

Hi Marc. The code I showed previously is pulling a user’s Region and District from custom profile properties. Those work great, but as I figured, pulling the person’s Manager (also a profile property) and populating a Manager people picker would likely be a task in and of itself because of the field type I assume.

I’ve seen a few examples on forums but still searching.

From: sympmarc [email removed]
Sent: Wednesday, May 04, 2011 2:22 PM
To: Williams,Carl [Ontario]
Subject: Re: SPGetCurrentUser populating text fields [SPServices:256000]

From: sympmarc

Carl:

Undoubtedly solvable. What are you trying to do with the People Picker?

M.

Coordinator
May 4, 2011 at 7:33 PM

Carl:

I have a blog post for that:
http://sympmarc.com/2010/03/16/set-a-people-pickers-value-on-a-form-revisited-with-jquery/

M.

May 4, 2011 at 7:36 PM

Thanks for that, I just read that last week too. I’ll try and tear out of it what I need to populate my Manager field instead of:

$("input[Title='Manager']").val(userRegion);

I’ll let you know.

From: sympmarc [email removed]
Sent: Wednesday, May 04, 2011 2:33 PM
To: Williams,Carl [Ontario]
Subject: Re: SPGetCurrentUser populating text fields [SPServices:256000]

From: sympmarc

May 30, 2011 at 4:10 PM

Hi all, this script below works great to populate given form fileds with data from the user's profile properties.

  1. The blue text field as a (text field) is populated fine with the domain\usename fine, however, the version in red (people picker field) is not populated with the variable userManager. Any ideas?
  2. Also, at the end of the script, should I uncomment // End $(document).ready?

<script type="text/javascript" src=....../central/JQuery/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src=...../central/JQuery/jquery.SPServices-0.6.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  var userRegion;
  var userDistrict;
  var userManager
  $().SPServices({
    operation: "GetUserProfileByName",
    async: false,
    AccountName: $().SPServices.SPGetCurrentUser(),
    completefunc: function (xData, Status) {
      userRegion = $(xData.responseXML).find("PropertyData > Name[text='Region']").parent().find("Values").text();
      userDistrict = $(xData.responseXML).find("PropertyData > Name[text='District']").parent().find("Values").text();
      userManager = $(xData.responseXML).find("PropertyData > Name[text='ManagerSupervisor']").parent().find("Values").text();
    }
  });
  alert(userRegion);
  alert(userDistrict);
  alert(userManager);
  $("input[Title='Region']").val(userRegion);
  $("[Title='Region']").attr("disabled", "disabled");
  $("input[Title='District']").val(userDistrict);
  $("[Title='District']").attr("disabled", "disabled");
  $("input[Title='ManagerText']").val(userManager);    //regular text field
  $("input[Title='Manager']").val(userManager);          //Person field
  $("input[Title='ManagerText']").val(userManager);
  $("[Title='ManagerText']").attr("disabled", "disabled");
}); // End $(document).ready
</script>

Cheers

Carl

Coordinator
May 31, 2011 at 3:55 AM

See this thread for an answer:
http://spservices.codeplex.com/discussions/259576

M.