SharePoint 使用ECMAscript对象模型来操作Goup与User

这里总结了关于使用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             ‘,&nbsp;&nbsp;ID: ‘ + oUser.get_id() +
 85             ‘,&nbsp;&nbsp;Email: ‘ + oUser.get_email() +
 86             ‘,&nbsp;&nbsp;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                 ‘,&nbsp;&nbsp;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                 ‘,&nbsp;&nbsp;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

SharePoint 使用ECMAscript对象模型来操作Goup与User的相关文章

SharePoint 使用ECMAscript对象模型来读取帖子列表

本随笔讲述如何用JavaScript来读取SharePoint 2013 中blog相关的帖子列表. ASCX File Content: <div id="divGetItemsFromPosts" title="读取贴子列表"></div><br /> <div id="divGetItemsFromComments" title="读取某个帖子的所有回贴"></div

每日学习心得:SharePoint 2013 自定义列表项添加Callout菜单项、文档关注、SharePoint服务端对象模型查询

前言: 前一段时间一直都比较忙,没有什么时间进行总结,刚好节前项目上线,同时趁着放假可以好好的对之前遇到的一些问题进行总结.主要内容有使用SharePoint服务端对象模型进行查询.为SharePoint 自定义列表项添加callout菜单.希望能够给大家带来一些帮助. 1.  在aspx页引用可视化Web部件 有时候会需要在页面中引用项目中创建的可视化Web部件,具体步骤有以下这几步: 1) 在aspx页面顶部注册该可视化Web部件 示例如下: <%@ Register Tagprefix=&qu

每天的学习经验:SharePoint 2013 定义自己添加的产品清单。Callout菜单项、文档关注、SharePoint服务机端对象模型查询

前言: 前一段时间一直都比較忙.没有什么时间进行总结,刚好节前项目上线.同一时候趁着放假能够好好的对之前遇到的一些问题进行总结. 主要内容有使用SharePoint服务端对象模型进行查询.为SharePoint 自己定义列表项加入callout菜单.希望能够给大家带来一些帮助. 1.  在aspx页引用可视化Web部件 有时候会须要在页面中引用项目中创建的可视化Web部件,详细步骤有下面这几步: 1) 在aspx页面顶部注冊该可视化Web部件 示比例如以下: <%@ Register Tagpr

SharePoint 2013 开发——开发自定义操作APP

?博客地址:http://blog.csdn.net/FoxDave 自定义操作即我们所说的Ribbon和ECB(Edit Control Block),在SharePoint 2013之前,我们可以通过在解决方案中添加XML元素来实现创建自定义Ribbon和ECB,到了2013时代,利用APP也可以做类似的事情了,接下来我们看看如何利用APP来创建列表条目的自定义操作. 除了一些细节上的配置项,创建SharePoint APP项目跟之前提到的基本一样.列表条目信息通过查询字符串传递到外部的托管

SharePoint PowerShell命令系列 (3) 操作WSP解决方案包的相关命令

Add-SPSolution 例子 Add-SPSolution -LiteralPath c:\solution.wsp 注意该命令只是向SharePoint服务器场添加解决方案包, 并不会做其他操作. Install-SPSolution 例子 Install-SPSolution -Identity solution.wsp -GACDeployment 参数 Force: 强制部署新的解决方案包. AllWebApplications: 向服务器场中所有Web应用程序部署新的解决方案包.

SharePoint客户端js对象模型

'use strict'; var context = SP.ClientContext.get_current(); var user = context.get_web().get_currentUser(); var web; (function () { // This code runs when the DOM is ready and creates a context object which is // needed to use the SharePoint object m

Sharepoint练习——客户端对象模型

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.SharePoint.Client; using System.IO; #region 上传文档 public static void FileAdd() { List Item1 = Web.GetList("sites/tangka

使用客户端对象模型回写SharePoint列表

使用客户端对象模型回写SharePoint列表 客户端对象模型是一个有效的方式回写SharePoint列表. 1. 管理员身份打开VS,新建WPF应用程序SPWriteListApp,确保选择.NET Framework 3.5. 2. 添加以下类型控件. 界面如下: 对应代码: 3. 开始编程序之前,必须添加引用 Microsoft.SharePoint.Client.dll 和the Microsoft.SharePoint.Client.Runtime.dll.然后添加 using Cli

使用client对象模型回写SharePoint列表

使用client对象模型回写SharePoint列表 client对象模型是一个有效的方式回写SharePoint列表. 1. 管理员身份打开VS,新建WPF应用程序SPWriteListApp,确保选择.NET Framework 3.5. 2. 加入下面类型控件. 界面例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3Jhenlnb2xm/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/diss