Populating a person field

May 31, 2011 at 12:10 AM
Edited May 31, 2011 at 12:10 AM

Hi all, sorry to repeat this from deep within another post, but just in case it got missed, I thought I'd carry on by creating a new post. Also, perhaps this may help Arron's previosus request.

Anyway, 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 2:57 AM

Carl:

The reason whay the red code isn't working is that the People Picker is a complex control built up of several HTML elements. It's not just an input element.

This blog post of mine shows you how to set the value of a People Pivker with jQuery:
http://sympmarc.com/2010/03/16/set-a-people-pickers-value-on-a-form-revisited-with-jquery/

M.

Coordinator
May 31, 2011 at 2:59 AM

p.s. The comment after the closing braces is simply there ato help follow the code. -> }); // End $(document).ready

May 31, 2011 at 11:27 AM

I see tx, good to know. The reason I asked was I recall years ago having to destroy objects after using them so as not to have them build up wasting memory etc.

Carl

From: sympmarc [email removed]
Sent: Monday, May 30, 2011 10:59 PM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

p.s. The comment after the closing braces is simply there ato help follow the code. -> }); // End $(document).ready

May 31, 2011 at 11:32 AM

Ahh, that figures. Good point, that would make sense.

I should be able to easily update the person field with a workflow using the simple text box as the source.

Glad there’s guys out there like you to help steer those of us who’s formal education was not programming. Me personally, chemistry and pharmacy.

Tx, and once again. Damned good work!!!

From: sympmarc [email removed]
Sent: Monday, May 30, 2011 10:58 PM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

Carl:

The reason whay the red code isn't working is that the People Picker is a complex control built up of several HTML elements. It's not just an input element.

This blog post of mine shows you how to set the value of a People Pivker with jQuery:
http://sympmarc.com/2010/03/16/set-a-people-pickers-value-on-a-form-revisited-with-jquery/

M.

May 31, 2011 at 12:25 PM
Edited May 31, 2011 at 12:27 PM

Yikes. I'm not sure if it's me or what, but it seems the text boxes (see code above for 'Region','District', 'Manager', 'Manager Text')are not updated by the variables if these text boxes are either:

  1. visible on the form but disabled by the jquery script after the variable populates them (see code above) or,
  2. the text boxes are "hidden" via content type.

I don't understand how they would loose their values because of the field being disabled (to prevent editing), or hidden when you can see them in balck and white on the form. If I leave them visible and editable, the values do show up on the pertinent list ok.

Great, you can't have your cake and eat it too :(

Carl

Coordinator
May 31, 2011 at 12:40 PM

Carl:

If you have columns disabled which you would like to save upon commit, you need to reenable them in the PreSaveAction prior to the commit.

If you are using a Content Type which doesn't have those columns, then they aren't hidden; they aren't in the page.

M.

May 31, 2011 at 12:43 PM

Hi M. Two good points. The content types do have those fields though and I’m positive they were updating correctly when they were hidden before..

CW

From: sympmarc [email removed]
Sent: Tuesday, May 31, 2011 8:41 AM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

Carl:

If you have columns disabled which you would like to save upon commit, you need to reenable them in the PreSaveAction prior to the commit.

If you are using a Content Type which doesn't have those columns, then they aren't hidden; they aren't in the page.

M.

May 31, 2011 at 12:48 PM

I’m just reviewing your post on this subject: http://sympmarc.com/2010/10/08/using-jquery-to-prefill-and-disable-required-column-in-a-sharepoint-form/

Carl

From: sympmarc [email removed]
Sent: Tuesday, May 31, 2011 8:41 AM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

Carl:

If you have columns disabled which you would like to save upon commit, you need to reenable them in the PreSaveAction prior to the commit.

If you are using a Content Type which doesn't have those columns, then they aren't hidden; they aren't in the page.

M.

Coordinator
May 31, 2011 at 1:00 PM

Perfect. I knew I had a post on that somewhere.

M.

May 31, 2011 at 2:13 PM

I have the PreSaveAction function after my main script, but still I get You must specify a value for this required field.:

<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();

}

});

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

$("input[Title='District']").val(userDistrict);

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

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

$("input[Title='Region']").attr("disabled", "disabled");

$("input[Title='District']").attr("disabled", "disabled");

$("input[Title='ManagerText']").attr("disabled", "disabled");

}); // End $(document).ready

</script>

<script language="javascript" type="text/javascript" >

function PreSaveAction() {

$("[Title='Region']").attr("disabled", "");

$("[Title='District']").attr("disabled", "");

$("[Title='ManagerText']").attr("disabled", "");

};

return true;

}</script>

From: sympmarc [email removed]
Sent: Tuesday, May 31, 2011 8:41 AM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

Carl:

If you have columns disabled which you would like to save upon commit, you need to reenable them in the PreSaveAction prior to the commit.

If you are using a Content Type which doesn't have those columns, then they aren't hidden; they aren't in the page.

M.

Coordinator
May 31, 2011 at 2:22 PM

One obvious thing is that you are returning true ouside the PreSaveAction. Does the form submit?

M.

May 31, 2011 at 2:27 PM

I am, I’ll put it inside then, I was just going on the example I saw? (see, just a Padawan learner). No the form doesn’t submit, just the typical You must specify a value for this required field.

CW

From: sympmarc [email removed]
Sent: Tuesday, May 31, 2011 10:23 AM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

One obvious thing is that you are returning true ouside the PreSaveAction. Does the form submit?

M.

Coordinator
May 31, 2011 at 2:30 PM

Then it does submit, meaning it goes to the server with a postback. It's just not committing the item.

Which column is giving you the validation error?

M.

May 31, 2011 at 2:33 PM

Region, District, ManagerText

CW

From: sympmarc [email removed]
Sent: Monday, May 30, 2011 10:58 PM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

Carl:

The reason whay the red code isn't working is that the People Picker is a complex control built up of several HTML elements. It's not just an input element.

This blog post of mine shows you how to set the value of a People Pivker with jQuery:
http://sympmarc.com/2010/03/16/set-a-people-pickers-value-on-a-form-revisited-with-jquery/

M.

Coordinator
May 31, 2011 at 2:35 PM

And you're certain that those three columns 1) have values in them, and 2) are being re-enabled?

M.

May 31, 2011 at 2:41 PM

Yes sir, as far as I can see,

1.

2. I assume this is re-enabling:

<script language="javascript" type="text/javascript" >

function PreSaveAction() {

alert('PreSaveArea')

//$("[Title='Region']").attr("disabled", "");

//$("[Title='District']").attr("disabled", "");

//$("[Title='ManagerText']").attr("disabled", "");

return true;

});

}</script>

From: sympmarc [email removed]
Sent: Tuesday, May 31, 2011 10:36 AM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

And you're certain that those three columns 1) have values in them, and 2) are being re-enabled?

M.

May 31, 2011 at 2:42 PM

Well, this anyways:

<script language="javascript" type="text/javascript" >

function PreSaveAction() {

alert('PreSaveArea')

$("[Title='Region']").attr("disabled", "");

$("[Title='District']").attr("disabled", "");

$("[Title='ManagerText']").attr("disabled", "");

return true;

});

}</script>

From: Williams,Carl [Ontario]
Sent: Tuesday, May 31, 2011 10:41 AM
To: 'SPServices@discussions.codeplex.com'
Subject: RE: Populating a person field [SPServices:259576]

Yes sir, as far as I can see,

1.

2. I assume this is re-enabling:

<script language="javascript" type="text/javascript" >

function PreSaveAction() {

alert('PreSaveArea')

//$("[Title='Region']").attr("disabled", "");

//$("[Title='District']").attr("disabled", "");

//$("[Title='ManagerText']").attr("disabled", "");

return true;

});

}</script>

From: sympmarc [email removed]
Sent: Tuesday, May 31, 2011 10:36 AM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

And you're certain that those three columns 1) have values in them, and 2) are being re-enabled?

M.

May 31, 2011 at 2:46 PM

The alert(‘PreSaveArea’) dialog is not poping up meaning the PreSaveAction function is not being called.

From: Williams,Carl [Ontario]
Sent: Tuesday, May 31, 2011 10:42 AM
To: 'SPServices@discussions.codeplex.com'
Subject: RE: Populating a person field [SPServices:259576]

Well, this anyways:

<script language="javascript" type="text/javascript" >

function PreSaveAction() {

alert('PreSaveArea')

$("[Title='Region']").attr("disabled", "");

$("[Title='District']").attr("disabled", "");

$("[Title='ManagerText']").attr("disabled", "");

return true;

});

}</script>

From: Williams,Carl [Ontario]
Sent: Tuesday, May 31, 2011 10:41 AM
To: 'SPServices@discussions.codeplex.com'
Subject: RE: Populating a person field [SPServices:259576]

Yes sir, as far as I can see,

1.

2. I assume this is re-enabling:

<script language="javascript" type="text/javascript" >

function PreSaveAction() {

alert('PreSaveArea')

//$("[Title='Region']").attr("disabled", "");

//$("[Title='District']").attr("disabled", "");

//$("[Title='ManagerText']").attr("disabled", "");

return true;

});

}</script>

From: sympmarc [email removed]
Sent: Tuesday, May 31, 2011 10:36 AM
To: Williams,Carl [Ontario]
Subject: Re: Populating a person field [SPServices:259576]

From: sympmarc

And you're certain that those three columns 1) have values in them, and 2) are being re-enabled?

M.

Coordinator
May 31, 2011 at 2:47 PM
Edited May 31, 2011 at 3:14 PM

No images. Can you try not replying with the thread? Makes it hard to read through!

You'll simply need to do some debugging here. If the input elements aren't re-enabled properly, then you'll continue to get the validation errors.

M.

May 31, 2011 at 2:50 PM

Sorry, will do thanks.

CW