2

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.

  • Did you find the solution you were looking for? – bgmCoder Dec 5 '15 at 3:21
2

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.

3

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());
  });
});
  • 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
1

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

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

  • 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
0
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

0

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.

0

I know it's too late but for some that might have the same problem in the feature. I've searched a lot of article but to no avail till I saw bgmCoder answer.

search your picker ID using debugger and replace the one below:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
    // Wait until SP.JS has loaded before calling getWebUserData 
    ExecuteOrDelayUntilScriptLoaded(SetCurrentUsernameToPeoplePicker, "sp.js");
});

function SetCurrentUsernameToPeoplePicker()
{
var ctx = new SP.ClientContext.get_current();
this.website = ctx.get_web();
this.currentUser = website.get_currentUser();
ctx.load(currentUser);
ctx.executeQueryAsync(Function.createDelegate(this, this.onSucceess), 
Function.createDelegate(this, this.onFail));
}

function onSucceess(sender, args)
 {
var loginName = currentUser.get_loginName();

//Replace # with your PeoplePickerID
$("#ctl00_ctl43_g_8956d607_d934_4703_94aa_e208896f3dfa_ff51_ctl00_ctl00_UserField_upLevelDiv").text(loginName);

$('a[title="Check Names"]').click();


 }

function onFail(sender, args)
{
alert('request failed ' + args.get_message() + '\n'+ args.get_stackTrace());
}
</script>

TNX bgmCoder for giving the idea. you saved my day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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