Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

This is for SP2010. Got a list. One of the fields is a "Person or Group" type, with the results limited to members of an SP group.

I've created a copy of NewForm.aspx in SharePoint Designer, and am trying to figure out how to set that field, which displays using the People Picker, so that on page load, the current user is loaded as the default value.

Fwiw, I've added SPServices to my NewForm.aspx, so it's available as a possible solution to the problem. I'm just not familiar with working with that People Picker control, so I'm hoping somebody has a fairly painless answer.

share|improve this question

4 Answers 4

up vote 2 down vote accepted

If you want to use SPServices, I added a new function called SPFindPeoplePicker to v0.7.2 (released on 24 Sep 2012) which will let you set the People Picker value easily.

share|improve this answer
SPUser user = web.CurrentUser;
PickerEntity entity = new PickerEntity();
entity.Key = user.LoginName;
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
entityArrayList.Add(entity);
PeoplePickerControlInstance.UpdateEntities(entityArrayList);

That should populate the people picker instance with the current user.

share|improve this answer
    
Is this Javascript? –  Batman Aug 16 '13 at 0:04
    
No, it's C#. eirikb's answer is Javascript. –  RJ Cuthbertson Aug 16 '13 at 3:31

Another option is to use JavaScript to inject the current user, for example by using jQuery like this:

var ctx = new SP.ClientContext();
var user = ctx.get_web().get_currentUser();
ctx.load(user);
ctx.executeQueryAsync(function() {
  $('.ms-formbody').filter(function() {
    return $(this).html().match(/spfielduser/i);
  }).each(function() {
    $(this).find('div:first').html(user.get_title());
  });
});
share|improve this answer
    
I'm trying this, got it inside a $(document).ready block, but am getting a JS err about SP.ClientContext() not existing. Googling "SP.ClientContext" leads me to believe it's a built-in SharePoint js func that should be available. Is it, or do I need to include a script src to it? –  Alan M Sep 25 '12 at 19:01
    
Yes it should be there, it's part of the [msdn.microsoft.com/en-us/library/ee557057.aspx](JavaScript SP namespace). If you are using Chrome then adding jQuery document ready can sometimes break the loading of body, in that case try manually running _spBodyOnLoadWrapper() in your console. –  eirikb Sep 25 '12 at 19:33
    
I'm using IE7 (yes, I know, I know. Corporate environment, out of my control). I'm going to see what's up with SP.ClientContext not working, but I can see from that docs and your example that it should be the tool for the job. –  Alan M Sep 25 '12 at 19:37

View this add-on(SharePoint Default Value Add-On), which inject a "default value" section into "Create Column" dialog.

Set "Current User" (User who is adding item) as default value.

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.