2

I am trying to make a field in a list autopopulate with the signed in user. I tried the Lookup field and it gives me a choice but it doesn't change based on which user is logged in. Any ideas how I get that to work? For instance:

Initiated by: (should autopopulate currently logged in user)

Thanks.

0

In SharePoint 2013 you can do this by adding a JavaScript to the New form.

<script type="text/javascript">

var loginName = "";
var userid = _spPageContextInfo.userId;

GetCurrentUser();

function GetCurrentUser() {

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";    
var requestHeaders = { "accept" : "application/json;odata=verbose" };

$.ajax({
  url : requestUri,
  contentType : "application/json;odata=verbose",
  headers : requestHeaders,
  success : onSuccess,
  error : onError
  });
}

function onSuccess(data, request) {
    var loginName = data.d.Title;
    //set the value to the field "LogedInUser" in the new form
    $('input[title="LogedInUser"]').val(loginName);    
}

function onError(error) {
  alert(error);
}

</script>

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.