1、Sharepoint 学习笔记 ECMAScript 对象模型系列- 8、组与用户操作(一) 这里总结一下关于使用 ECMAscript 对象模型来操作 Goup 与 User 的常用情况,因为内容较多,所以拆分为两个部分,这部分主要内容如下:1、取得当前 Sharepoint 网站所有的 Group2、获取当前登录用户的 Title 与所属 Group3、获取指定 Group 下的所有 Users4、获取指定 Group 下的所有 Users 的特定信息5、获取所有的 Group 的所有 Users 的特定信息分别描述如下:1、取得当前 Sharepoint 网站所有的 Group /Get
2、 All The Site Groups in SharePoint Using ECMAScriptvar groupCollection;function getAllSiteGroups() var clientContext = new SP.ClientContext();this.groupCollection = clientContext.get_web().get_siteGroups();clientContext.load(groupCollection);clientContext.executeQueryAsync(Function.createDelegate(th
3、is, this.onQuerySucceededgetAllSiteGroups),Function.createDelegate(this, this.onQueryFailedgetAllSiteGroups);http:/ function onQuerySucceededgetAllSiteGroups() var groupName = Site Groups: n;var groupsEnumerator = this.groupCollection.getEnumerator();while (groupsEnumerator.moveNext() var group = gr
4、oupsEnumerator.get_current();groupName += Title : + group.get_title() + ID : + group.get_id() + n;alert(groupName);function onQueryFailedgetAllSiteGroups(sender, args) alert(Request failed. + args.get_message() + n + args.get_stackTrace();2、获取当前登录用户的 Title 与所属 Group/retrieve curent loggedin user and
5、 the usres GROUP var user;var visitorsGroup;function retrieveCurrLgUserAndGrp() var clientContext = new SP.ClientContext();var groupCollection = clientContext.get_web().get_siteGroups();/ Get the visitors group, assuming its ID is 4.visitorsGroup = groupCollection.getById(4);user = clientContext.get
6、_web().get_currentUser();var userCollection = visitorsGroup.get_users();userCollection.addUser(user);clientContext.load(user);clientContext.load(visitorsGroup);clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededretrieveCurrLgUserAndGrp),Function.createDelegate(this, t
7、his.onQueryFailedretrieveCurrLgUserAndGrp);http:/ function onQuerySucceededretrieveCurrLgUserAndGrp() alert(user.get_title() + “ added to group “ + visitorsGroup.get_title();function onQueryFailedretrieveCurrLgUserAndGrp(sender, args) alert(Request failed. + args.get_message() + n + args.get_stackTr
8、ace(); 3、获取指定 Group 下的所有 Usersvar siteUrlretrieveAllUsersInGroup = /;function retrieveAllUsersInGroup(groupID) / debugger;var clientContext = new SP.ClientContext.get_current();/var clientContext = new SP.ClientContext(siteUrlretrieveAllUsersInGroup);var collGroup = clientContext.get_web().get_siteG
9、roups();var oGroup = collGroup.getById(groupID); /Specify which group you want to retrieve/ var oGroup = collGroup.getByTitle(DevpTest Visitor);this.collUser = oGroup.get_users();clientContext.load(collUser);clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededretrieveA
10、llUsersInGroup),Function.createDelegate(this, this.onQueryFailedretrieveAllUsersInGroup);function onQuerySucceededretrieveAllUsersInGroup() / debugger;var userInfo = ;var userEnumerator = collUser.getEnumerator();while (userEnumerator.moveNext() var oUser = userEnumerator.get_current();userInfo = us
11、erInfo + nUser Title: + oUser.get_title() +nID: + oUser.get_id() +nEmail: + oUser.get_email() +nLogin Name: + oUser.get_loginName();alert(userInfo);function onQueryFailedretrieveAllUsersInGroup(sender, args) alert(Request failed. + args.get_message() + n + args.get_stackTrace();4、获取指定 Group 下的所有 Use
12、rs 的特定信息/Get User Specific Informationsvar siteUrlSpecificUserProperties = /;function retrieveSpecificUserProperties(groupID) /var clientContext = new SP.ClientContext(siteUrlSpecificUserProperties);var clientContext = new SP.ClientContext.get_current();var collGroup = clientContext.get_web().get_si
13、teGroups();var oGroup = collGroup.getById(groupID);this.collUser = oGroup.get_users();clientContext.load(collUser, Include(Title, LoginName, Email);clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededretrieveSpecificUserProperties),Function.createDelegate(this, this.on
14、QueryFailedretrieveSpecificUserProperties);function onQuerySucceededretrieveSpecificUserProperties() var userInfo = ;var userEnumerator = collUser.getEnumerator();while (userEnumerator.moveNext() var oUser = userEnumerator.get_current();userInfo += nUser: + oUser.get_title() +nEmail: + oUser.get_ema
15、il() +nLogin Name: + oUser.get_loginName();alert(userInfo);function onQueryFailedretrieveSpecificUserProperties(sender, args) alert(Request failed. + args.get_message() + n + args.get_stackTrace();5、获取所有的 Group 的所有 Users 的特定信息/Retrieve all groups all users specific informationsvar siteUrlAllUsersAll
16、GroupsSpecificProperties = /;function retrieveAllUsersAllGroupsSpecificProperties() /var clientContext = new SP.ClientContext(siteUrlAllUsersAllGroupsSpecificProperties);var clientContext = new SP.ClientContext.get_current();this.collGroup = clientContext.get_web().get_siteGroups();clientContext.loa
17、d(collGroup, Include(Title,Id,Users.Include(Title,LoginName);clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededretrieveAllUsersAllGroupsSpecificProperties),Function.createDelegate(this, this.onQueryFailedretrieveAllUsersAllGroupsSpecificProperties);function onQuerySu
18、cceededretrieveAllUsersAllGroupsSpecificProperties() var userInfo = ;var groupEnumerator = collGroup.getEnumerator();while (groupEnumerator.moveNext() var oGroup = groupEnumerator.get_current();var collUser = oGroup.get_users();var userEnumerator = collUser.getEnumerator();while (userEnumerator.move
19、Next() var oUser = userEnumerator.get_current();userInfo += nGroup ID: + oGroup.get_id() +nGroup Title: + oGroup.get_title() +nUser: + oUser.get_title() +nLogin Name: + oUser.get_loginName();alert(userInfo);function onQueryFailedretrieveAllUsersAllGroupsSpecificProperties(sender, args) alert(Request
20、 failed. + args.get_message() + n + args.get_stackTrace();Sharepoint 学习笔记 ECMAScript 对象模型系列- 9、组与用户操作(二) 接着上面的继续,这里我们描述的关于 User 与 Group 的操作如下:6、 向指定 Group 中添加指定 User7、 获取指定 Group 的 Owner8、 把当前登录用户添加到指定 Group 中9、 判断当前登录用户是否有 EditPermission 权限10、判断当前登录用户是否在某特定的 Group 中 分别描述如下:6、 向指定 Group 中添加指定 Userva
21、r siteUrl = /;function addUserToSharePointGroup(groupID) /var clientContext = new SP.ClientContext(siteUrl);var clientContext = new SP.ClientContext.get_current();var collGroup = clientContext.get_web().get_siteGroups();var oGroup = collGroup.getById(groupID);var userCreationInfo = new SP.UserCreati
22、onInformation();userCreationInfo.set_email(helpdeskGLSTAR.COM.AU);userCreationInfo.set_loginName(GLSTARhelpdesk);userCreationInfo.set_title(helpdesk);this.oUser = oGroup.get_users().add(userCreationInfo); /add user into group/ var userInfo = nUser: + oUser.get_title() +/ nEmail: + oUser.get_email()
23、+/ nLogin Name: + oUser.get_loginName();/ alert(userInfo);clientContext.load(oUser);clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededaddUserToSharePointGroup),Function.createDelegate(this, this.onQueryFailedaddUserToSharePointGroup);function onQuerySucceededaddUserT
24、oSharePointGroup() alert(this.oUser.get_title() + “ added.“);function onQueryFailedaddUserToSharePointGroup(sender, args) alert(Request failed. + args.get_message() + n + args.get_stackTrace();7、 获取指定 Group 的 Owner/Get The Group Owner Name in SharePoint 2010 Using ECMAScriptvar group;function getGro
25、upOwnerName(groupID) var clientContext = new SP.ClientContext();var groupCollection = clientContext.get_web().get_siteGroups();group = groupCollection.getById(groupID);clientContext.load(group);clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededgetGroupOwnerName),Func
26、tion.createDelegate(this, this.onQueryFailedgetGroupOwnerName);function onQuerySucceededgetGroupOwnerName() alert(“GroupTitle: “ + group.get_title() + “nGroupOwnerTitle : “ +group.get_ownerTitle();function onQueryFailedgetGroupOwnerName(sender, args) alert(Request failed. + args.get_message() + n +
27、args.get_stackTrace();8、 把当前登录用户添加到指定 Group 中/adds the current user to the specific group on the current websitevar currentUser;var visitorsGroup;function addUserToSpecificGroupInCurrWeb(groupID) var clientContext = new SP.ClientContext();var groupCollection = clientContext.get_web().get_siteGroups(
28、);visitorsGroup = groupCollection.getById(groupID);currentUser = clientContext.get_web().get_currentUser();var userCollection = visitorsGroup.get_users();userCollection.addUser(currentUser);clientContext.load(currentUser);clientContext.load(visitorsGroup);clientContext.executeQueryAsync(Function.cre
29、ateDelegate(this, this.onQuerySucceededaddUserToSpecificGroup),Function.createDelegate(this, this.onQueryFailedaddUserToSpecificGroup);function onQuerySucceededaddUserToSpecificGroup() alert(currentUser.get_title() + “ added to group “ + visitorsGroup.get_title();function onQueryFailedaddUserToSpeci
30、ficGroup(sender, args) alert(Request failed. + args.get_message() + n + args.get_stackTrace();9、 判断当前登录用户是否有 EditPermission 权限/Check current user has Edit Permissionvar theCurrentUser;var theWeb;function checkifUserHasEditPermissions() / debugger;var context = new SP.ClientContext.get_current();theW
31、eb = context.get_web();theCurrentUser = theWeb.get_currentUser();context.load(theCurrentUser);context.load(theWeb, EffectiveBasePermissions);context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethodcheckEditPermissions),Function.createDelegate(this, this.onFailureMethodcheckEditPe
32、rmissions);function onSuccessMethodcheckEditPermissions() /debugger;if (theWeb.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems) /User Has Edit Permissionsalert(theCurrentUser.get_loginName()+“Has Edit Permission on current website“);else alert(“No Edit Permission“);function onFail
33、ureMethodcheckEditPermissions() alert(“Failed to check permission“);10、判断当前登录用户是否在某特定的 Group 中 var IsInThisGroupFlag = false;/ The below checks if the user exists in the groupfunction checkIfCurrentUserIsInGroup(groupID) var context = SP.ClientContext.get_current();/I go to parent site if Im in a su
34、bsite!var siteColl = context.get_site();var web = siteColl.get_rootWeb();var groupCollection = web.get_siteGroups();/ Get the Our Groups IDvar _group = groupCollection.getById(groupID); / ID of the Group that we are checkingvar users = _group.get_users(); / Get all Users of the groupcontext.load(_gr
35、oup);context.load(users);this._users = users;this._currentUser = web.get_currentUser(); / Get current usercontext.load(this._currentUser);context.executeQueryAsync(Function.createDelegate(this, this.CheckUserSucceededUserIsInGroup),Function.createDelegate(this, this.CheckUserfailedUserIsInGroup);/Th
36、e below Checks if User is the member of the specified groupfunction CheckUserSucceededUserIsInGroup() /debugger;IsInThisGroupFlag = false;if (this._users.get_count() 0) var _usersEnum = this._users.getEnumerator();while (_usersEnum.moveNext() var user = _usersEnum.get_current();if (user.get_loginNam
37、e() = this._currentUser.get_loginName()/debugger;IsInThisGroupFlag = true;if (IsInThisGroupFlag) alert(user.get_loginName() + “ exist in the checked group “ + IsInThisGroupFlag.toString();else alert(user.get_loginName() + “ not exist in the checkedgroup “);function CheckUserfailedUserIsInGroup() alert(failed);