-
Notifications
You must be signed in to change notification settings - Fork 1
/
SPPeoplePickerPlugin.js
239 lines (226 loc) · 9.56 KB
/
SPPeoplePickerPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
(function ($) {
RegisterScriptFiles('clienttemplates.js');
RegisterScriptFiles('clientforms.js');
RegisterScriptFiles('clientpeoplepicker.js');
RegisterScriptFiles('autofill.js');
AddStyleForPeoplePicker();
// Load JS Files From _layout Folder
function RegisterScriptFiles(filename) {
if (RegisterSod != undefined) {
RegisterSod(filename, '/_layouts/15/' + filename);
SP.SOD.loadMultiple([filename], function () {
console.info('File Loaded', filename);
})
}
else {
var scriptEle = document.createElement('script');
scriptEle.setAttribute("type", "text/javascript")
scriptEle.setAttribute("src", "/_layouts/15/" + filename);
document.getElementsByTagName("head")[0].appendChild(scriptEle)
}
}
// Add Style for People picker
function AddStyleForPeoplePicker() {
var style = '<style type="text/css">' +
'.disablePeoplePicker {' +
'color: rgb(177, 177, 177);' +
'border-color: rgb(225, 225, 225);' +
'background-color: rgb(253, 253, 253);' +
'cursor: not-allowed;' +
'}' +
'.disablePeoplePicker > span >.sp-peoplepicker-delImage {' +
'display: none;' +
'}' +
'</style>';
$('head').append(style);
}
// Render and initialize the client-side People Picker.
function initializePeoplePicker(eleId, AllowMultipleValues, Width, accountType, callback) {
// Create a schema to store picker properties, and set the properties.
var schema = {};
if (accountType === undefined) {
accountType = "User";
} else if (typeof (accountType) === "object") {
accountType = accountType.toString()
}
else if (typeof (accountType) != "string") {
accountType = "User";
}
if (Width === undefined) {
Width = "250px";
}
// schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
schema['PrincipalAccountType'] = accountType;
schema['SearchPrincipalSource'] = 15;
schema['ResolvePrincipalSource'] = 15;
schema['AllowMultipleValues'] = AllowMultipleValues;
schema['MaximumEntitySuggestions'] = 50;
schema['Width'] = Width;
// 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.
window.SPClientPeoplePicker_InitStandaloneControlWrapper(eleId, null, schema);
setTimeout(function(){
callback();
},500);
}
// Get info from the People picker
function GetPeoplePickerValues(eleId) {
var toSpanKey = eleId + "_TopSpan";
var peoplePicker = null;
// Get the people picker object from the page.
//var peoplePicker = window.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
var ClientPickerDict = window.SPClientPeoplePicker.SPClientPeoplePickerDict;
// Get the people picker object from the page.
for (var propertyName in ClientPickerDict) {
if (propertyName == toSpanKey) {
peoplePicker = ClientPickerDict[propertyName];
break;
}
}
if (peoplePicker != null) {
// Get information about all users.
var users = peoplePicker.GetAllUserInfo();
var userInfo = '';
for (var i = 0; i < users.length; i++) {
var user = users[i];
userInfo += user['DisplayText'] + ";#";
}
return userInfo;
}
else
return '';
}
// Get keys from the People picker
function GetPeoplePickerKeys(eleId) {
var toSpanKey = eleId + "_TopSpan";
var peoplePicker = null;
// Get the people picker object from the page.
//var peoplePicker = window.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
var ClientPickerDict = window.SPClientPeoplePicker.SPClientPeoplePickerDict;
// Get the people picker object from the page.
for (var propertyName in ClientPickerDict) {
if (propertyName == toSpanKey) {
peoplePicker = ClientPickerDict[propertyName];
break;
}
}
if (peoplePicker != null) {
// Get information about all users.
var keys = peoplePicker.GetAllUserKeys();
return keys;
}
else
return '';
}
// Set keys to People picker
function SetPeoplePickerKeys(eleId, loginIdOrEmailOrSearchText) {
var toSpanKey = eleId + "_TopSpan";
var peoplePicker = null;
// Get the people picker object from the page.
//var peoplePicker = window.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
var ClientPickerDict = window.SPClientPeoplePicker.SPClientPeoplePickerDict;
// Get the people picker object from the page.
for (var propertyName in ClientPickerDict) {
if (propertyName == toSpanKey) {
peoplePicker = ClientPickerDict[propertyName];
break;
}
}
if (peoplePicker != null) {
peoplePicker.AddUserKeys(loginIdOrEmailOrSearchText, false); //true shows the auto-suggest box, false resolve the user
}
}
// Clear All Users From People picker
function ClearAllUsersFromPeoplePicker(eleId) {
var toSpanKey = eleId + "_TopSpan";
var peoplePicker = null;
// Get the people picker object from the page.
//var peoplePicker = window.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
var ClientPickerDict = window.SPClientPeoplePicker.SPClientPeoplePickerDict;
// Get the people picker object from the page.
for (var propertyName in ClientPickerDict) {
if (propertyName == toSpanKey) {
peoplePicker = ClientPickerDict[propertyName];
break;
}
}
if (peoplePicker != null) {
peoplePicker.IterateEachProcessedUser(function (index, user) {
peoplePicker.DeleteProcessedUser(document.getElementById(user.UserContainerElementId));
});
}
}
// On User Resolved Event For People Picker
function OnUserResolvedEvent(eleId, callback) {
var toSpanKey = eleId + "_TopSpan";
var peoplePicker = null;
// Get the people picker object from the page.
//var peoplePicker = window.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
var ClientPickerDict = window.SPClientPeoplePicker.SPClientPeoplePickerDict;
// Get the people picker object from the page.
for (var propertyName in ClientPickerDict) {
if (propertyName == toSpanKey) {
peoplePicker = ClientPickerDict[propertyName];
break;
}
}
if (peoplePicker != null) {
peoplePicker.OnUserResolvedClientScript = callback;
}
}
// this will use as plugin
// to use this plugin you need to have div and use it as below
/*
$("#idOfDiv").spPeoplePicker(true,"100px");
this will work if js file on top of this file will load and work
*/
$.fn.spPeoplePicker = function (AllowMultipleValues, Width, accountType, callback) {
var eleId = $(this).attr('id');
ExecuteOrDelayUntilScriptLoaded(function () { initializePeoplePicker(eleId, AllowMultipleValues, Width, accountType, callback); }, 'sp.core.js');
};
// Query the picker for user information.
$.fn.getUserInfo = function () {
var eleId = $(this).attr('id');
var spUsersInfo = GetPeoplePickerValues(eleId);
return spUsersInfo.slice(0, -2);
}
// Query the People picker for user keys
$.fn.getUserKeys = function () {
var eleId = $(this).attr('id');
var spUsersInfo = GetPeoplePickerKeys(eleId);
return spUsersInfo;
}
// Set Keys to People picker
$.fn.setUserKeys = function (loginIdOrEmailOrSearchText) {
var eleId = $(this).attr('id');
var spUsersInfo = SetPeoplePickerKeys(eleId, loginIdOrEmailOrSearchText);
return spUsersInfo;
}
// Clear Peoplepicker
$.fn.clearPeoplePicker = function () {
var eleId = $(this).attr('id');
ClearAllUsersFromPeoplePicker(eleId);
}
// Disable People picker
$.fn.disablePeoplePicker = function () {
var eleId = $(this).attr('id');
$('#' + eleId + '_TopSpan_EditorInput').attr('disabled', true);
$('div [id*="' + eleId + '"]').each(function () {
$(this).addClass('disablePeoplePicker');
});
}
// Enable People picker
$.fn.enablePeoplePicker = function () {
var eleId = $(this).attr('id');
$('#' + eleId + '_TopSpan_EditorInput').attr('disabled', false);
$('div [id*="' + eleId + '"]').each(function () {
$(this).removeClass('disablePeoplePicker');
});
}
$.fn.OnUserResolvedEvent = function (callback) {
var eleId = $(this).attr('id');
OnUserResolvedEvent(eleId, callback);
}
})(jQuery);