2

My scenario:

I have a list and I want to create a new item, the form used to create a new item opens up and I have a field name Administrator there and by default, I want the username of the person creating that item to appear.

Note I do not want to rename the created By field

Also, note we are using a community site, I don't find the Add default value to a column in library settings.

2 Answers 2

1

You could insert script editor/content editor webpart to your list form, and init the field value by javascript.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var userid = _spPageContextInfo.userId;
        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;
            SetUserFieldValue("people", loginName);
        }
        function onError(error) {
            //alert(error);
        }
        function SetUserFieldValue(fieldName, userName) {
            var _PeoplePicker = $("div[title='" + fieldName + "']");
            var _PeoplePickerTopId = _PeoplePicker.attr('id');
            var _PeoplePickerEditor = $("input[title='" + fieldName + "']");
            _PeoplePickerEditer.val(userName);
            var _PeoplePickerObject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
            _PeoplePickerObject.AddUnresolvedUserFromEditor(true);
        }
        GetCurrentUser();
    });
</script>
2
  • exactly the code am been using,note i replace fieldName correctly,but i dont see the name appearing in the required field also,note i dont get any error. Oct 3, 2017 at 5:50
  • ok it works but note the variable _PeoplePickerEditer should be _PeoplePickerEditor..now in the field the whole username doesnt show up only 5 letters!!! Oct 3, 2017 at 6:41
1

This is the code that worked for me without any bug.

<script>  
    $(document).ready(function() {  

        var userid = _spPageContextInfo.userId;  


        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;  

            SetUserFieldValue("your field name", loginName);  

        }  

        function onError(error) {  

            alert(error);  

        }  

        function SetUserFieldValue(fieldName, userName) {  

            var _PeoplePicker = $("div[title='" + fieldName + "']");  

            var _PeoplePickerTopId = _PeoplePicker.attr('id');  

            var _PeoplePickerEditer = $("input[title='" + fieldName + "']");  

            _PeoplePickerEditer .removeAttr('placeholder');

            _PeoplePickerEditer.val(userName);  

            var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];  

            _PeoplePickerOject.AddUnresolvedUserFromEditor(true);  

        }  

        GetCurrentUser();  

    });  
</script> 

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.