SPPermissionCollection.Remove method (SPMember)

NOTE: This API is now obsolete.

Removes from the collection the permission that is associated with the specified member.

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

Syntax

'Declaration
<ObsoleteAttribute("Use the SPRoleAssignmentCollection class instead")> _
Public Sub Remove ( _
    member As SPMember _
)
'Usage
Dim instance As SPPermissionCollection
Dim member As SPMember

instance.Remove(member)
[ObsoleteAttribute("Use the SPRoleAssignmentCollection class instead")]
public void Remove(
    SPMember member
)

Parameters

Examples

The following code example removes permissions for users of a list for whom the Notes property contains "2".

Iteration through the permission collection works decrementally rather than incrementally because the collection is modified each time that a permission is removed.

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

For i = perms.Count - 1 To 0 Step -1

    Dim user As SPUser

    For Each user In users

        If user.ID = perms(i).Member.ID Then

            If user.Notes = "1" Then

                perms.Remove(perms(i).Member)

            End If

        End If

    Next user

Next i
SPSite oSiteCollection = SPContext.Current.Site;
SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"];
SPList oList = oWebsite.Lists["List_Name"];
SPPermissionCollection collPerms = oList.Permissions;
SPUserCollection collusers = oWeb.Users;

for (int intIndex = collPerms.Count - 1; intIndex>-1; intIndex--)
{

    foreach (SPUser oUser in collUsers)
    {

        if (oUser.ID == collPerms[intIndex].Member.ID)
        {

            if (oUser.Notes == "2")
            {
                collPerms.Remove(collPerms[intIndex].Member);
            }
        }
    }
}
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

Remove overload

Microsoft.SharePoint namespace