SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
    
Did you find the solution you were looking for? – bgmCoder Dec 5 '15 at 3:21
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
    
I ran into a problem with ViewState Validation with spservices. Here is my post concerning that: spservices.codeplex.com/discussions/648386 – bgmCoder Dec 5 '15 at 3:08

Here is another way to fill in the username box.

First fetch your current user.

//there are a number of ways to fetch current user; here is one
var currentUser = $().SPServices.SPGetCurrentUser({ fieldName: "Name", debug: false })

Now fill the box.

Credit for the trick of filling in the value and resolving the user actually goes here. This actually works.

//setting the value isn't too hard
$("div[title='People Picker']").text( currentUser );
$('a[title="Check Names"]').click();  // <-- clicks the "Check Name" button programmatically to resolve your user

Caveat

However, all of these jquery-fill-the-box (this one above, as well as using spservices methods get me caught up in a System.Web.UI.ViewStateException: Invalid viewstate error. Read my thread here for details. I believe the error has to do with using a custom NewForm.aspx or custom EditForm.aspx.

share|improve this answer
function fnassign(){
   try{
       var len3=$("div [title='Assign To']").find(".sp-peoplepicker-resolveList").find("span").length;
       if(len3!=3){
          alert("Please enter the AssignTo value");
          return false;
       }
   }
   catch(e)
   {
      alert(e);
   }
}

Click here more details

share|improve this answer

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
    
Awwwwww! That solution costs $199! – bgmCoder Dec 5 '15 at 3:18
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

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.