mylifeinaminute.com

Creating and Populating Cross-Site Groups in SharePoint

by liquidpooled on Aug.11, 2008, under .NET, Microsoft, Office, Sharepoint Server, Visual Studio 2005, Visual Studio 2008, Windows SharePoint Services

The following is a method to create new cross-site groups in SharePoint 2007/WSS 3.0. Along with creating the group, users are also added to the group based on selections from a PeopleEditor control (better known as the People Picker).


private enum GroupType
{
	Owners,
	Members,
	Visitors
}

public SPWeb CurrentWeb
{
	get { return SPContext.GetContext(HttpContext.Current).Web; }
}

private void CreateAndPopulateGroup(string groupName, GroupType groupType, bool createNewGroup)
{
	// Create the group
	if (createNewGroup)
	{
		SPMember owner = SPContext.GetContext(HttpContext.Current).Web.CurrentUser;
		this.CurrentWeb.SiteGroups.Add(groupName, owner, null, groupName);
		SPGroup grp = this.CurrentWeb.SiteGroups[groupName];
		switch (groupType)
		{
			case GroupType.Owners:
				this.CurrentWeb.AssociatedOwnerGroup = grp;
				break;
                    	case GroupType.Members:
				this.CurrentWeb.AssociatedMemberGroup = grp;
				break;
			case GroupType.Visitors:
				this.CurrentWeb.AssociatedVisitorGroup = grp;
				break;
		}
		SPRoleDefinitionCollection roleDef = this.CurrentWeb.RoleDefinitions;
		SPRoleAssignmentCollection roleAssigns = this.CurrentWeb.RoleAssignments;
		SPRoleAssignment roleAssign = new SPRoleAssignment(grp);
		SPRoleDefinitionBindingCollection definitions = roleAssign.RoleDefinitionBindings;
		switch (groupType)
		{
			case GroupType.Owners:
				definitions.Add(roleDef["Full Control"]); //Could be Contribute or Read
				break;
			case GroupType.Members:
				definitions.Add(roleDef["Contribute"]);
				break;
			case GroupType.Visitors:
				definitions.Add(roleDef["Read"]);
			break;
		}
		roleAssigns.Add(roleAssign);
		this.CurrentWeb.Update();
	}
	else // use an existing group
	{
		string grpName = string.Empty;
		// find the group reference
		foreach (SPGroup group in this.CurrentWeb.SiteGroups)
		{
			switch (groupType)
			{
				case GroupType.Owners:
					if (group.ID.ToString() == this.ddlOwnerGroup.SelectedValue)
					{
						grpName = group.Name;
					}
					break;
				case GroupType.Members:
					if (group.ID.ToString() == this.ddlMemberGroup.SelectedValue)
					{
						grpName = group.Name;
					}
					break;
				case GroupType.Visitors:
					if (group.ID.ToString() == this.ddlVisitorGroup.SelectedValue)
					{
						grpName = group.Name;
					}
					break;
			}
		}
		SPGroup grp = this.CurrentWeb.SiteGroups[grpName];
		switch (groupType)
		{
			case GroupType.Owners:
				this.CurrentWeb.AssociatedOwnerGroup = grp;
				break;
			case GroupType.Members:
				this.CurrentWeb.AssociatedMemberGroup = grp;
				break;
			case GroupType.Visitors:
				this.CurrentWeb.AssociatedVisitorGroup = grp;
				break;
		}

		SPRoleDefinitionCollection roleDef = this.CurrentWeb.RoleDefinitions;
		SPRoleAssignmentCollection roleAssigns = this.CurrentWeb.RoleAssignments;
		SPRoleAssignment roleAssign = new SPRoleAssignment(grp);
		SPRoleDefinitionBindingCollection definitions = roleAssign.RoleDefinitionBindings;
		switch (groupType)
		{
			case GroupType.Owners:
				definitions.Add(roleDef["Full Control"]); //Could be Contribute or Read
				break;
			case GroupType.Members:
				definitions.Add(roleDef["Contribute"]);
				break;
			case GroupType.Visitors:
				definitions.Add(roleDef["Read"]);
				break;
		}
		roleAssigns.Add(roleAssign);
		this.CurrentWeb.Update();
	}

	SPUserCollection grpUsers = this.CurrentWeb.SiteGroups[groupName].Users;

	// Add the users to the group;
	System.Collections.ArrayList entities = new System.Collections.ArrayList();

	switch (groupType)
	{
		case GroupType.Owners:
			entities = pickerOwnerGroupMembers.Entities;
			break;
		case GroupType.Members:
			entities = pickerMemberGroupMembers.Entities;
			break;
		case GroupType.Visitors:
			entities = pickerVisitorGroupMembers.Entities;
			break;
	}

	foreach (PickerEntity entity in entities)
	{
		// add the user only if they do not exist already
		try
		{
			SPUser user = grpUsers[entity.Key];
		}
		catch (SPException exc)
		{
			// user was not found, continue to add the user
			this.CurrentWeb.SiteGroups[groupName].Users.Add(entity.Key,
			Convert.ToString(entity.EntityData["Email"]),
			Convert.ToString(entity.EntityData["DisplayName"]),
			string.Empty);
		}
	}
}
No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...