I needed to add a group and several users to all site collections in a farm that consisted of 4000+ site collections and 20+ terabytes. This users would be a part of an administration group that could view and manage the collections. In order to add the users I use the spfarm class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace AddUser
{
class Program
{
static void Main(string[] args)
{
//Add all the users that will be included on the group
string[] username =
{
“laptopdev/jim”,
“laptopdev/administrator”
};
// Get a reference to the local farm
SPFarm oFarm = SPFarm.Local;
// Loop through the services in the farm
foreach (SPService oService in oFarm.Services)
{
// See if this service is a SPWebService
if (oService is SPWebService)
{
// Cast into a SPWebService
SPWebService oWebService = (SPWebService)oService;
// Loop through the web applications in the web service
foreach (SPWebApplication oWebApplication in oWebService.WebApplications)
{
foreach (SPSite oSite in oWebApplication.Sites)
{
// Get the root web object
SPWeb oWebRoot = oSite.RootWeb;
SPRoleDefinition readDef = oWebRoot.RoleDefinitions["Read"];
SPRoleDefinition contributeDef = oWebRoot.RoleDefinitions["Contribute"];
SPRoleDefinition admins2 = oWebRoot.RoleDefinitions["Full Control"];
SPRole admins = oWebRoot.Roles["Full Control"];
string FBARoleProviderPrefix = “CustomRoleProvider”;
string newGroupName = “Test”;
foreach (SPGroup spGroup in oWebRoot.Groups)
{
//if the group we want to add does not already exist, create it at the top level and make
//the owner the top level owners group.
if (!oWebRoot.SiteGroups.Xml.Contains(string.Format(”Name=\”{0}\”", newGroupName)))
oWebRoot.SiteGroups.Add(newGroupName, oWebRoot.SiteAdministrators[0], null, “This is for administrator for all sites” + newGroupName);
}
try
{
SPGroup newGroup = oWebRoot.SiteGroups[newGroupName];
newGroup.AllowMembersEditMembership = true;
SPRoleAssignment spRoleAssignment = new SPRoleAssignment(newGroup);
SPRoleDefinition role = oWebRoot.RoleDefinitions["Full Control"];
spRoleAssignment.RoleDefinitionBindings.Add(role);
oWebRoot.RoleAssignments.Add(spRoleAssignment);
oWebRoot.Update();
foreach (string UserName in username)
{
SPUser oUser = oWebRoot.AllUsers[UserName];
newGroup.AddUser(oUser);
newGroup.Update();
}
// Be sure to dispose the SPSite and SPWeb objects for memory cleanup
oWebRoot.Dispose();
oSite.Dispose();
}
catch { }
//Do the same for all the subsites under a site collection.
//Add Full Control to the group
foreach (SPWeb subWeb in oWebRoot.Webs)
{
SPGroup newGroupSubWeb = oWebRoot.SiteGroups[newGroupName];
SPRoleAssignment spRoleAssignmentSubWeb = new SPRoleAssignment(newGroupSubWeb);
SPRoleDefinition roleSubWeb = oWebRoot.RoleDefinitions["Full Control"];
spRoleAssignmentSubWeb.RoleDefinitionBindings.Add(roleSubWeb);
subWeb.RoleAssignments.Add(spRoleAssignmentSubWeb);
subWeb.Update();
}
}
}
}
}
}
}