这里总结了关于使用ECMAscript对象模型来操作Goup与User的常用情况,内容如下:
1、取得当前Sharepoint网站所有的Groups
2、获取当前登录用户的Title与所属Group
3、获取指定Group下的所有Users
4、获取所有的Groups的所有Users的特定信息
5、 向指定Group中添加指定User
6、 判断当前登录用户是否有EditPermission权限
7、判断当前登录用户是否在某特定的Group中
ASCX File Content:
1 <div id="divGroupCollection" title="1、取得当前Sharepoint网站所有的Groups"></div><br /> 2 <div id="divCurrLgUserAndGrp" title="2、获取当前登录用户的Title与所属Group"></div><br /> 3 <div id="divAllUsersInGroup" title="3、获取指定Group下的所有Users"></div><br /> 4 <div id="divAllUsersAllGroups" title="4、获取所有的Groups的所有Users的特定信息"></div><br /> 5 <div id="divAddUserToGroup" title="5、向指定Group中添加指定User"></div><br /> 6 <div id="divIsHasEditPermission" title="6、判断当前登录用户是否有EditPermission权限"></div><br /> 7 <div id="divIsInThisGroup" title="7、判断当前登录用户是否在某特定的Group中"></div><br /> 8 <script src="/_layouts/15/Learning_ECMAScript/Learning_WP_Permission.js"></script>
JS File Content(Learning_WP_Permission.js):
1 //1、取得当前Sharepoint网站所有的Groups 2 //Get All The Site Groups in SharePoint Using ECMAScript 3 var groupCollection; 4 function getAllGroups() { 5 var clientContext = new SP.ClientContext(); 6 this.groupCollection = clientContext.get_web().get_siteGroups(); 7 clientContext.load(groupCollection); 8 clientContext.executeQueryAsync(Function.createDelegate(this, this.getAllGroupsSucceeded), 9 Function.createDelegate(this, this.getAllGroupsFailed)); 10 11 } 12 13 function getAllGroupsSucceeded() { 14 var groupsHTML = ‘<strong>---------------Site Groups-------------</strong><br/>‘; 15 var groupsEnumerator = this.groupCollection.getEnumerator(); 16 var group; 17 while (groupsEnumerator.moveNext()) { 18 group = groupsEnumerator.get_current(); 19 //SP2013 20 groupsHTML += ‘Title: <a href="/_layouts/15/people.aspx?MembershipGroupId=‘ + group.get_id() + ‘" target="_blank">‘ + group.get_title() + 21 ‘</a> (ID: ‘ + group.get_id() + ‘) (Owner Title: ‘ + group.get_ownerTitle() + ‘)<br/>‘; 22 } 23 document.getElementById(‘divGroupCollection‘).innerHTML = groupsHTML; 24 } 25 26 function getAllGroupsFailed(sender, args) { 27 alert(‘Request failed. ‘ + args.get_message() + ‘\r\n‘ + args.get_stackTrace()); 28 } 29 30 //2、获取当前登录用户的Title与所属Group 31 //Retrieve current logged in user and added to group 32 var user; 33 var visitorsGroup; 34 function retrieveCurrLgUserAndGrp() { 35 var clientContext = new SP.ClientContext(); 36 var groupCollection = clientContext.get_web().get_siteGroups(); 37 // Get the visitors group, assuming its ID is 5. 38 visitorsGroup = groupCollection.getById(5); 39 user = clientContext.get_web().get_currentUser(); 40 var userCollection = visitorsGroup.get_users(); 41 userCollection.addUser(user); 42 43 clientContext.load(user); 44 clientContext.load(visitorsGroup); 45 clientContext.executeQueryAsync(Function.createDelegate(this, this.retrieveCurrLgUserAndGrpSucceeded), 46 Function.createDelegate(this, this.retrieveCurrLgUserAndGrpFailed)); 47 48 } 49 50 function retrieveCurrLgUserAndGrpSucceeded() { 51 var currentUserAndGroupHTML = ‘<strong>---------------Current User and Added to Group-------------</strong><br/>‘; 52 currentUserAndGroupHTML += user.get_title() + ‘ added to group (‘ + visitorsGroup.get_title() + ‘)‘; 53 document.getElementById(‘divCurrLgUserAndGrp‘).innerHTML = currentUserAndGroupHTML; 54 } 55 56 function retrieveCurrLgUserAndGrpFailed(sender, args) { 57 alert(‘Request failed. ‘ + args.get_message() + ‘\r\n‘ + args.get_stackTrace()); 58 } 59 60 //3、获取指定Group下的所有Users 61 //Retrieve all users in specified group 62 var collUser; 63 function retrieveAllUsersInGroup(groupID) { 64 var clientContext = new SP.ClientContext(); 65 //var clientContext = new SP.ClientContext(‘/‘); 66 var collGroup = clientContext.get_web().get_siteGroups(); 67 var oGroup = collGroup.getById(groupID); //Specify which group you want to retrieve 68 // var oGroup = collGroup.getByTitle(‘DevpTest Visitor‘); 69 // var oGroup = collGroup.getByName(‘DevpTest Visitor‘); //SP2013 70 this.collUser = oGroup.get_users(); 71 clientContext.load(collUser); 72 //clientContext.load(collUser, ‘Include(Title, LoginName, Email)‘); //Limit loading data 73 clientContext.executeQueryAsync(Function.createDelegate(this, this.retrieveAllUsersInGroupSucceeded), 74 Function.createDelegate(this, this.retrieveAllUsersInGroupFailed)); 75 } 76 77 function retrieveAllUsersInGroupSucceeded() { 78 var allUsersHTML = ‘<strong>---------------All Users In Specified Group, e.g. the ID of group is 5-------------</strong><br/>‘; 79 var userEnumerator = collUser.getEnumerator(); 80 var oUser; 81 while (userEnumerator.moveNext()) { 82 oUser = userEnumerator.get_current(); 83 allUsersHTML += ‘User Title: ‘ + oUser.get_title() + 84 ‘, ID: ‘ + oUser.get_id() + 85 ‘, Email: ‘ + oUser.get_email() + 86 ‘, Login Name: ‘ + oUser.get_loginName() + ‘<br/>‘; 87 } 88 document.getElementById(‘divAllUsersInGroup‘).innerHTML = allUsersHTML; 89 } 90 91 function retrieveAllUsersInGroupFailed(sender, args) { 92 alert(‘Request failed. ‘ + args.get_message() + ‘\r\n‘ + args.get_stackTrace()); 93 } 94 95 //4、获取所有的Groups的所有Users的特定信息 96 //Retrieve all groups all users specific information 97 var groupCollection2; 98 function retrieveAllUsersAllGroupsSpecificProperties() { 99 //var clientContext = new SP.ClientContext(‘/‘); 100 var clientContext = new SP.ClientContext(); 101 this.groupCollection2 = clientContext.get_web().get_siteGroups(); 102 clientContext.load(groupCollection2, ‘Include(Title,Id,Users.Include(Title,LoginName))‘); 103 104 clientContext.executeQueryAsync(Function.createDelegate(this, this.retrieveAllUsersAllGroupsSpecificPropertiesSucceeded), 105 Function.createDelegate(this, this.retrieveAllUsersAllGroupsSpecificPropertiesFailed)); 106 } 107 108 function retrieveAllUsersAllGroupsSpecificPropertiesSucceeded() { 109 var allUsersAllGroupsHTML = ‘<strong>---------------All Users in All Groups-------------</strong><br/>‘; 110 var groupEnumerator = groupCollection2.getEnumerator(); 111 var oGroup; 112 while (groupEnumerator.moveNext()) { 113 oGroup = groupEnumerator.get_current(); 114 allUsersAllGroupsHTML += ‘Group ID: ‘ + oGroup.get_id() + 115 ‘, Group Title: ‘ + oGroup.get_title() + ‘<br/>‘; 116 117 var collUser = oGroup.get_users(); 118 var userEnumerator = collUser.getEnumerator(); 119 var oUser; 120 while (userEnumerator.moveNext()) { 121 oUser = userEnumerator.get_current(); 122 allUsersAllGroupsHTML += ‘--User Title: ‘ + oUser.get_title() + 123 ‘, Login Name: ‘ + oUser.get_loginName() + ‘<br/>‘; 124 } 125 } 126 document.getElementById(‘divAllUsersAllGroups‘).innerHTML = allUsersAllGroupsHTML; 127 } 128 129 function retrieveAllUsersAllGroupsSpecificPropertiesFailed(sender, args) { 130 alert(‘Request failed. ‘ + args.get_message() + ‘\r\n‘ + args.get_stackTrace()); 131 } 132 133 //5、向指定Group中添加指定User 134 //Add user to group 135 var newUser; 136 function addUserToGroup(groupID) { 137 var clientContext = new SP.ClientContext(); 138 var collGroup = clientContext.get_web().get_siteGroups(); 139 var oGroup = collGroup.getById(groupID); 140 var userCreationInfo = new SP.UserCreationInformation(); 141 userCreationInfo.set_email(‘[email protected]‘); 142 userCreationInfo.set_loginName(‘gammonsp130\\spadmin‘); 143 userCreationInfo.set_title(‘spadmin‘); 144 newUser = oGroup.get_users().add(userCreationInfo); //add user into group 145 clientContext.load(newUser); 146 clientContext.executeQueryAsync(Function.createDelegate(this, this.addUserToGroupSucceeded), 147 Function.createDelegate(this, this.addUserToGroupFailed)); 148 } 149 150 function addUserToGroupSucceeded() { 151 var addUserToGroupHTML = ‘<strong>---------------Add User to Group, e.g. the ID of group is 5-------------</strong><br/>‘; 152 addUserToGroupHTML += newUser.get_title() + " added to group which ID is 5."; 153 document.getElementById(‘divAddUserToGroup‘).innerHTML = addUserToGroupHTML; 154 } 155 function addUserToGroupFailed(sender, args) { 156 alert(‘Request failed. ‘ + args.get_message() + ‘\r\n‘ + args.get_stackTrace()); 157 } 158 159 //6、判断当前登录用户是否有EditPermission权限 160 //Check current user has Edit Permission 161 var currentWeb, currentUser; 162 function isHasEditPermission() { 163 var context = new SP.ClientContext.get_current(); 164 currentWeb = context.get_web(); 165 currentUser = currentWeb.get_currentUser(); 166 context.load(currentUser); 167 context.load(currentWeb, ‘EffectiveBasePermissions‘); 168 context.executeQueryAsync(Function.createDelegate(this, this.isHasEditPermissionSucceeded), 169 Function.createDelegate(this, this.isHasEditPermissionFailed)); 170 } 171 function isHasEditPermissionSucceeded() { 172 var isHasEditPermissionHTML = ‘<strong>---------------Check the user has Edit Permission or not-------------</strong><br/>‘; 173 isHasEditPermissionHTML += ‘Has (user: ‘ + currentUser.get_loginName() + ‘) Edit Permission on current website? ‘ + 174 (currentWeb.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems) ? ‘Yes‘ : ‘No‘); 175 document.getElementById(‘divIsHasEditPermission‘).innerHTML = isHasEditPermissionHTML; 176 } 177 function isHasEditPermissionFailed(sender, args) { 178 alert(‘Request failed. ‘ + args.get_message() + ‘\r\n‘ + args.get_stackTrace()); 179 } 180 181 //7、判断当前登录用户是否在某特定的Group中 182 // The below checks if the user exists in the specified group 183 var allUsersInGroup, currentUser2; 184 function isCurrentUserInGroup(groupID) { 185 var context = SP.ClientContext.get_current(); 186 //I go to parent site if I‘m in a subsite! 187 var siteColl = context.get_site(); 188 var web = siteColl.get_rootWeb(); 189 var groupCollection = web.get_siteGroups(); 190 191 var oGroup = groupCollection.getById(groupID); 192 this.allUsersInGroup = oGroup.get_users(); 193 this.currentUser2 = web.get_currentUser(); 194 context.load(oGroup); 195 context.load(this.allUsersInGroup); 196 context.load(this.currentUser2); 197 context.executeQueryAsync(Function.createDelegate(this, this.isCurrentUserInGroupSucceeded), 198 Function.createDelegate(this, this.isCurrentUserInGroupFailed)); 199 } 200 201 function isCurrentUserInGroupSucceeded() { 202 var isInThisGroup = 0; 203 if (this.allUsersInGroup.get_count() > 0) { 204 var userEnumerator = this.allUsersInGroup.getEnumerator(); 205 while (userEnumerator.moveNext()) { 206 var user = userEnumerator.get_current(); 207 if (user.get_loginName() == this.currentUser2.get_loginName()) { 208 isInThisGroup = 1; 209 break; 210 } 211 } 212 } 213 var isInThisGroupHTML = ‘<strong>---------------Check the user exists in the specified group or not-------------</strong><br/>‘; 214 isInThisGroupHTML += ‘Is (user: ‘ + this.currentUser2.get_loginName() + ‘) exists in the specified group which ID is 5? ‘ + 215 (isInThisGroup ? ‘Yes‘ : ‘No‘); 216 document.getElementById(‘divIsInThisGroup‘).innerHTML = isInThisGroupHTML; 217 } 218 219 function isCurrentUserInGroupFailed(sender, args) { 220 alert(‘Request failed. ‘ + args.get_message() + ‘\r\n‘ + args.get_stackTrace()); 221 } 222 223 224 //初始化 225 //Init 226 _spBodyOnLoadFunctionNames.push(‘documentReady‘); 227 function documentReady() { 228 SP.SOD.executeFunc(‘sp.js‘, ‘SP.ClientContext‘, function () { 229 getAllGroups(); 230 retrieveCurrLgUserAndGrp(); 231 retrieveAllUsersInGroup(5); 232 retrieveAllUsersAllGroupsSpecificProperties(); 233 addUserToGroup(5); 234 isHasEditPermission(); 235 isCurrentUserInGroup(5); 236 }); 237 }
效果:
时间: 2024-10-15 15:37:42