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)

Syntax

'Declaration
<ObsoleteAttribute("Use the SPRoleAssignmentCollection class instead")> _
Public Sub AddCollection ( _
    addUsersInfo As SPUserInfo(), _
    addUsersPermissions As SPRights(), _
    addGroupsNames As String(), _
    addGroupsPermissions As SPRights(), _
    addRolesNames As String(), _
    addRolesPermissions As SPRights() _
)
'Usage
Dim instance As SPPermissionCollection
Dim addUsersInfo As SPUserInfo()
Dim addUsersPermissions As SPRights()
Dim addGroupsNames As String()
Dim addGroupsPermissions As SPRights()
Dim addRolesNames As String()
Dim addRolesPermissions As SPRights()

instance.AddCollection(addUsersInfo, _
    addUsersPermissions, addGroupsNames, _
    addGroupsPermissions, addRolesNames, _
    addRolesPermissions)
[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.

Remarks

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.

Examples

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.

Dim siteCollection As SPSite = SPContext.Current.Site
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")
Dim list As SPList = site.Lists("List_Name")
Dim perms As SPPermissionCollection = list.Permissions
Dim users As SPUserCollection = site.Users

Dim i As Integer = 0
Dim j As Integer = 0

Dim user As SPUser

For Each user In  users

    If user.Notes = "2" Then

        i = i + 1

    End If

Next user

Dim userInfo(i) As SPUserInfo

For Each user In  users

    If user.Notes = "2" Then

        userInfo(j).Email = user.Email
        userInfo(j).LoginName = user.LoginName
        userInfo(j).Name = user.Name
        userInfo(j).Notes = user.Notes + " changed to 3"

        j = j + 1

    End If

Next user

Dim permsIndiv(userInfo.Length) As SPRights
Dim k As Integer

For k = 0 To permsIndiv.Length - 1

    permsIndiv(k) = SPRights.FullMask

Next k

Dim roles(2) As String = 
    {"Role_1", "Role_2", "Role_3"}
Dim permsRoles(roles.Length) As SPRights
Dim l As Integer

For l = 0 To permsRoles.Length - 1

    permsRoles(l) = SPRights.AddListItems Or SPRights.ManageLists

Next l

Dim groups(2) As String = 
    {"Group_1", "Group_2", "Group_3"}
Dim permsGroups(groups.Length) As SPRights
Dim m As Integer

For m = 0 To permsGroups.Length - 1

    permsGroups(m) = 
        SPRights.AddListItems Or SPRights.ManageLists

Next m

perms.AddCollection(userInfo, permsIndiv, groups, 
    permsGroups, roles, permsRoles)
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();

Note

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.

See also

Reference

SPPermissionCollection class

SPPermissionCollection members

Microsoft.SharePoint namespace