快捷搜索:

【Sharepoint教程—ECMAScript对象模型】8、组与用户

这里总结一下关于应用ECMAscript工具模型来操作Goup与User的常用环境,由于内容较多,以是拆分为两个部分,这部分主要内容如下:

1、取适合前Sharepoint网站所有的Group

2、获取当前登任命户的Title与所属Group

3、获取指定Group下的所有Users

4、获取指定Group下的所有Users的特定信息

5、获取所有的Group的所有Users的特定信息

分手描述如下:

1、取适合前Sharepoint网站所有的Group

//Get All The Site Groups in SharePoint Using ECMAScript

var groupCollection;function getAllSiteGroups() {

var clientContext = new SP.ClientContext();this.groupCollection = clientContext.get_web().get_siteGroups();

clientContext.load(groupCollection);clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededgetAllSiteGroups),

Function.createDelegate(this, this.onQueryFailedgetAllSiteGroups));

}

function onQuerySucceededgetAllSiteGroups() {var groupName = 'Site Groups: n';

var groupsEnumerator = this.groupCollection.getEnumerator();while (groupsEnumerator.moveNext()) {

var group = groupsEnumerator.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 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_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, this.onQueryFailedretrieveCurrLgUserAndGrp));

}

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_stackTrace());}

3、获取指定Group下的所有Users

var siteUrlretrieveAllUsersInGroup = '/';

function retrieveAllUsersInGroup(groupID) {// debugger;

var clientContext = new SP.ClientContext.get_current();//var clientContext = new SP.ClientContext(siteUrlretrieveAllUsersInGroup);

var collGroup = clientContext.get_web().get_siteGroups();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.onQuerySucceededretrieveAllUsersInGroup),

Function.createDelegate(this, this.onQueryFailedretrieveAllUsersInGroup));}

function onQuerySucceededretrieveAllUsersInGroup() {

// debugger;var userInfo = '';

var userEnumerator = collUser.getEnumerator();

while (userEnumerator.moveNext()) {var oUser = userEnumerator.get_current();

userInfo = userInfo + '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下的所有Users的特定信息

//Get User Specific Informations

var siteUrlSpecificUserProperties = '/';

function retrieveSpecificUserProperties(groupID) {//var clientContext = new SP.ClientContext(siteUrlSpecificUserProperties);

var clientContext = new SP.ClientContext.get_current();var collGroup = clientContext.get_web().get_siteGroups();

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.onQueryFailedretrieveSpecificUserProperties));}

function onQuerySucceededretrieveSpecificUserProperties() {

var userInfo = '';

var userEnumerator = collUser.getEnumerator();

while (userEnumerator.moveNext()) {var oUser = userEnumerator.get_current();

userInfo += 'nUser: ' + oUser.get_title() +'nEmail: ' + oUser.get_email() +

'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 informations

var siteUrlAllUsersAllGroupsSpecificProperties = '/';function retrieveAllUsersAllGroupsSpecificProperties() {

//var clientContext = new SP.ClientContext(siteUrlAllUsersAllGroupsSpecificProperties);var clientContext = new SP.ClientContext.get_current();

this.collGroup = clientContext.get_web().get_siteGroups();clientContext.load(collGroup, 'Include(Title,Id,Users.Include(Title,LoginName))');

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededretrieveAllUsersAllGroupsSpecificProperties),

Function.createDelegate(this, this.onQueryFailedretrieveAllUsersAllGroupsSpecificProperties));

}

function onQuerySucceededretrieveAllUsersAllGroupsSpecificProperties() {

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.moveNext()) {

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 failed. ' + args.get_message() + 'n' + args.get_stackTrace());

}

您可能还会对下面的文章感兴趣: