SharePoint 2013 - Client OM

你离开我真会死。 提交于 2019-11-26 11:32:57

1. 向 executeQueryAsync 中传递参数可以使用以下两种方式,也可以参考这篇文章

var mySuccessCallBack = Function.createCallback(onQuerySucceeded, oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, mySuccessCallBack),Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded(sender, args, item){
    alert("Added"+ item.get_id());
}
              
function onQueryFailed(sender, args){
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

或者:

clientContext.executeQueryAsync(Function.createDelegate(this, function(){onQuerySucceeded(oListItem);}), Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded(item){
    alert("Added"+ item.get_id());
}
function onQueryFailed(sender, args){
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

2. 下面代码演示了SharePoint 2013 中如何使用 client-side people picker control 并获取用户信息,参考链接

引用内置JS文件:(注意,在content edit webpart 中最好不要使用 <SharePoint:ScriptLink name="clienttemplates.js" runat="server" LoadAfterUI="true" Localizable="false" /> 来引用内置文件,因为<SharePoint:ScriptLink>需要包含在<asp:Content>标签内,在Content Edit Webpart内使用时,可能不起作用)

<script type="text/javascript" src="/_layouts/15/clienttemplates.js"></script>
<script type="text/javascript" src="/_layouts/15/clientforms.js"></script>
<script type="text/javascript" src="/_layouts/15/autofill.js"></script>
<script type="text/javascript" src="/_layouts/15/clientpeoplepicker.js"></script>

JavaScript:

<script type="text/javascript" src="/angularJS/SiteAssets/Scripts/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
        // Run your custom code when the DOM is ready.
        $(document).ready(function () {
            // Specify the unique ID of the DOM element where the
            // picker will render.
            initializePeoplePicker('peoplePickerDiv');
        });
        
        // Render and initialize the client-side People Picker.
        function initializePeoplePicker(peoplePickerElementId) {
            // Create a schema to store picker properties, and set the properties.
            var schema = {};
            schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
            schema['SearchPrincipalSource'] = 15;
            schema['ResolvePrincipalSource'] = 15;
            schema['AllowMultipleValues'] = true;
            schema['MaximumEntitySuggestions'] = 50;
            schema['Width'] = '280px';
        
            // Render and initialize the picker. 
            // Pass the ID of the DOM element that contains the picker, an array of initial
            // PickerEntity objects to set the picker value, and a schema that defines
            // picker properties.
            this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);
        }
        
        // Query the picker for user information.
        function getUserInfo() {
            // Get the people picker object from the page.
            var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
        
            // Get information about all users.
            var users = peoplePicker.GetAllUserInfo();
            var userInfo = '';
            for (var i = 0; i < users.length; i++) {
                var user = users[i];
                for (var userProperty in user) { 
                    userInfo += userProperty + ':  ' + user[userProperty] + '<br>';
                }
            }
            $('#resolvedUsers').html(userInfo);
        
            // Get user keys.
            var keys = peoplePicker.GetAllUserKeys();
            $('#userKeys').html(keys);
        
            // Get the first user's ID by using the login name.
            getUserId(users[0].Key);
        }
        
        // Get the user ID.
        function getUserId(loginName) {
            var context = new SP.ClientContext.get_current();
            this.user = context.get_web().ensureUser(loginName);
            context.load(this.user);
            context.executeQueryAsync(
                 Function.createDelegate(null, ensureUserSuccess), 
                 Function.createDelegate(null, onFail)
            );
        }
        
        function ensureUserSuccess() {
            $('#userId').html(this.user.get_id());
        }
        
        function onFail(sender, args) {
            alert('Query failed. Error: ' + args.get_message());
        }
    </script>

HTML:

<div id="peoplePickerDiv"></div>
    <div>
        <br/>
        <input type="button" value="Get User Info" οnclick="getUserInfo()"></input>
        <br/>
        <h1>User info:</h1>
        <p id="resolvedUsers"></p>
        <h1>User keys:</h1>
        <p id="userKeys"></p> 
        <h1>User ID:</h1>
        <p id="userId"></p>
    </div>

3.

转载于:https://www.cnblogs.com/qijiage/p/4259792.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!