SPPermissionCollection.AddCollection method

NOTE: This API is now obsolete.

Adds users and their permissions to a list, modifies the permissions of users for a list, modifies the permissions of an existing group for a list, or modifies the permissions of an existing role for a site.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

[ObsoleteAttribute("Use the SPRoleAssignmentCollection class instead")]
public void AddCollection(
	SPUserInfo[] addUsersInfo,
	SPRights[] addUsersPermissions,
	string[] addGroupsNames,
	SPRights[] addGroupsPermissions,
	string[] addRolesNames,
	SPRights[] addRolesPermissions
)

Parameters

addUsersInfo
Type: []

An SPUserInfo array that specifies information about users, including user names, e-mail addresses, display names, and notes about the users.

addUsersPermissions
Type: []

An SPRights array that specifies permissions for a user.

addGroupsNames
Type: []

A string array that specifies group names.

addGroupsPermissions
Type: []

An SPRights array that specifies permissions for a group.

addRolesNames
Type: []

A string array that specifies role names.

addRolesPermissions
Type: []

An SPRights array that specifies permissions for a role.

The parameters for the AddCollection method work in pairs, one pair for adding or modifying users and their permissions, one pair for modifying the permissions of roles, and one pair for modifying the permissions of groups. The number of elements contained in the arrays for each pair must match. Pass a null reference (Nothing in Visual Basic) for parameters that you do not intend to implement.

If you change the permissions of a user, role, or cross-site group for a list, the original permissions for the site are maintained. The changes apply only to the list. If you add users and permissions to a list but not to the site as a whole, the users acquire Guest permissions for the site.

The following code example uses all six parameters of the AddCollection method to modify the permissions of site users for whom the Notes property contains "2", and to modify the list permissions for specified roles and groups.

The example iterates twice through the collection of users for the specified site, once to count the number of users whose notes contain "2" and to use this number to set the size of the associated SPRights array, and once to assign each of these users to an SPUserInfo array.

The pipe symbol ("|") in C# or Or in Microsoft Visual Basic is used to delimit multiple rights for a single permission.

SPSite oSiteCollection = SPContext.Current.Site;
SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"];
SPList oList = oWebsite.Lists["List_Name"];
SPPermissionCollection collPermissions = oList.Permissions;
SPUserCollection collUsers = oWebsite.Users;
int intIndexCounter = 0;
int intIndexCurrent = 0;

foreach (SPUser oUser in collUsers)
{

    if (oUser.Notes == "2")
    {
        intIndexCounter = intIndexCounter + 1;
    }
}

SPUserInfo[] oUserInfo = new SPUserInfo[intIndexCounter];

foreach (SPUser oUser in collUsers)
{

    if (oUser.Notes == "2")
    {
        userInfo[intIndexCurrent].Email = oUser.Email;
        userInfo[intIndexCurrent].LoginName = oUser.LoginName;
        userInfo[intIndexCurrent].Name = oUser.Name;
        userInfo[intIndexCurrent].Notes = oUser.Notes + " changed to 3";

        intIndexCurrent = intIndexCurrent + 1;
    }
}

SPRights[] collIndividualPermissions = new SPRights[oUserInfo.Length];

for (int k=0; k<collIndividualPermissions.Length; k++)
{
    collIndividualPermissions[k] = SPRights.FullMask;
}

string[] strRoles = new string[3]{"Role_1", "Role_2", 
    "Role_3"};

SPRights[] collRolesPermissions = new SPRights[strRoles.Length];

for (int l = 0; l<collRolesPermissions.Length; l++)
{
    collRolesPermissions[l] = SPRights.AddListItems | SPRights.ManageLists;
}

string[] strGroups = new String[3]{"Group_1", 
    "Group_2", "Group_3"};

SPRights[] collGroupPermissions = new SPRights[strGroups.Length];

for (int m = 0; m<collGroupPermissions.Length; m++)
{
    collGroupPermissions[m] = SPRights.AddListItems | 
        SPRights.ManageLists;
}

collPermissions.AddCollection(oUserInfo, 
    collIndividualPermissions, strGroups, 
    collGroupPermissions, strRoles, collRolesPermissions);
oWebsite.Dispose();
NoteNote

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

Show: