JS不会自动填充SharePoint用户

时间:2018-04-25 16:45:07

标签: javascript sharepoint

我已经收到了一段似乎没有按照我的意愿行事的代码片段。在过去,我已经能够使用它根据SharePoint中的用户名自动填充名称。没有明显的错误,脚本中的其他所有内容都运行正常,看起来这样做,它只是没有做到的。

function getWebUserData() {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        currentUser = web.get_currentUser();
        currentUser.retrieve();
        context.load(web);
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), 
             Function.createDelegate(this, this.onFailureMethod));
}

function onSuccessMethod(sender, args) {
    var userObject = web.get_currentUser();

    $("input[Title='Requester']").val(userObject.get_title());
    $("input[Title='Requester']").attr('disabled','disabled');   
}

1 个答案:

答案 0 :(得分:0)

下面的代码适用于我的本地SharePoint 2013. referenced thread

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            function GetCurrentUser() {
                var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser";
                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 userName = data.d.LoginName;
                //parse the value.
                userName = userName.toString().split("i:0#.w|")[1];
                SetUserFieldValue("Requester", userName);
            }
            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.val(userName);
                var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
                _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
            }
            GetCurrentUser();
        });
    </script>