Roland Oldengarm - Independent IT Contractor

Living in the coolest little capital Wellington, New Zealand!

Add Users to SPGroup with claims based authentication (SharePoint 2010)

We had some problems while adding users to a SPGroup using SPGroup.Add(username, …) with Claims Based authentication. The code was executed, but the users could not login. When we add the users through the userinterface (aclinv.aspx) everything works fine.

Thus, we ran Reflector and saw the following internal method was called by AclInv.aspx: SPGroup.Users.AddCollection(SPUserInfo[] userInfos, SPUser[] users)

SPGroup.Users.AddCollection(SPUserInfo[] userInfos) calls above method with users == null, SPGroup.Users.Add(string name, …) calls SPGroup.Users.AddCollection with 1 userinfo-object and also users == null.

We tried to pass a SPUser object to the internal method, using Reflection. Et voila, everything works!

private static void AddUser(SPSite site, SPUserCollection userCollection, string userName)        {            
  var type = typeof(SPUserCollection);            
  var method = type.GetMethod("AddCollection", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);            
  var userInfoArray = new SPUserInfo[0];          
  SPUser[] userArray = null;
  userArray = new SPUser[1];            
  userArray[0] = site.RootWeb.EnsureUser(userName);
  method.Invoke(userCollection, new object[] { userInfoArray, userArray });      

1 Comment

  1. How do you pass value for SPUserCollection field? How to send the parameters?

Leave a Reply

Your email address will not be published.