0

I have SharePoint newform.aspx, I want to make the default value for a people picker to be the current user and disable the people picker. so I want to make look like a textbox but at the same time to make it submitted as person inside my list so Lync status appears on the right side of the column, is there a way to do that?

2 Answers 2

0

You can get the current user using SPServices like described in the following documentation:

http://spservices.codeplex.com/wikipage?title=%24%28%29.SPServices.SPGetCurrentUser

You should specifiy the name property, so you do something like this:

var user = $().SPServices.SPGetCurrentUser({
    fieldName: "Name",
    debug: false
});

After getting the user, you can use all jQuery stuff to reach what you want, so you could write a code like this:

$("div[title='People Picker']").text(user); //Set the text for the control
$("div[Title='People Picker']").attr("contentEditable", false); //will make it disabled.
$("a[Title='Check Names']").hide(); //Will hide the check button
$("a[Title='Browse']").hide(); //Will hide the browse control that looks like a book

Note that you should specifiy more specific selectors in case you have more than one people picker and you want to modify one.

2

If suitable in your case, reference some javascript on the newForm (for example by making a custom newform.aspx).

You could get the current logged in user by combining information from _spPageContextInfo with a REST query. E.g.

var userid = _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
$.ajax({
  url : requestUri,
  contentType : "application/json;odata=verbose",
  headers : requestHeaders,
  success : onSuccess,
  error : onError});

Using the property LoginName returned by the REST query the people picker can be set using the following code (assuming jQuery is available):

var loginName = "i:0#.w|contoso\\johannes"; // Assume this is the returned value from the rest query
var form = jQuery("table[class='ms-formtable']"); // get the form element
var userField = form.find("input[id$='ClientPeoplePicker_EditorInput']").get(0) // find the people picker element, assuming you dont have multiple people pickers on the form 
var peoplepicker = SPClientPeoplePicker.PickerObjectFromSubElement(userField) // Use SPClientPeoplePicker to get the actual picker object
peoplepicker.AddUserKeys(loginName); // finally set the loginName as the people picker value, this also triggers a validation.

Unfortunately I don´t have a code example for making the people picker disabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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