Search code examples
c#sharepointsharepoint-2010web-parts

Remove SharePoint User with Only Partial Login Name


I have a need to remove a user's permissions from a site collection. The issue is that I do not have the entire loginName. Each user is given a unique loginName (e.g. DOMAIN/012345678.cnd) The 'cnd' portion can change per user and the numeric portion is always unique.

So what I want to do is pass in a variable where only the numeric portion is known and and then based on that, it removes the user.

I have the following code:

using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb){
            SPGroupCollection collGroups = oWebsiteRoot.SiteGroups;
            SPUser oUser = oWebsiteRoot.SiteUsers[""];
        }

So there need to be some way to set the oUser to be loginName given only the numeric part. I can always get the domain easily enough. It is the last part after the number sequence I cannot get.


Solution

  • It's not a clean solution but it's a start:

               SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb;
    
                SPGroupCollection collGroups = oWebsiteRoot.SiteGroups;
                foreach(SPGroup group in collGroups)
                {
                    for(int i=group.Users.Count; i>=0;i--)
                    {
                        SPUser user = group.Users[i];
                        if (listOfDecimalsToBeDeleted.Contains(getUserDecimalPart(user.LoginName)))
                        {
                            group.RemoveUser(user);
                        }
                    }
                }