ASPX templates
Version 5.x > ASPX templates > Create a Group and add a User to it View modes: 
User avatar
Member
Member
hemanthray-gmail - 8/26/2010 4:27:55 PM
   
Create a Group and add a User to it
I am trying to Create a group in kentico programmatically and a User to it. Here is the code i am Using but it doesn't add a Group into Kentico . Please correct if I am wrong

gi = new GroupInfo();

// Set properties

gi.GroupDisplayName = "New group2";


gi.GroupName = "NewGroup2";

gi.GroupSiteID = 2;

gi.GroupDescription = "";

gi.GroupApproveMembers = GroupApproveMembersEnum.AnyoneCanJoin;

gi.GroupAccess = SecurityAccessEnum.AllUsers;

gi.GroupApproved = true;

// gi.GroupApprovedByUserID = CurrentUser.UserID;

// gi.GroupCreatedByUserID = CurrentUser.UserID;

gi.AllowCreate = SecurityAccessEnum.GroupMembers;

gi.AllowDelete = SecurityAccessEnum.GroupMembers;

gi.AllowModify = SecurityAccessEnum.GroupMembers;

gi.GroupNodeGUID = Guid.Empty;


// Save object to database

GroupInfoProvider.SetGroupInfo(gi);
gi = GroupInfoProvider.GetGroupInfo("NewGroup2", CMS.CMSHelper.CMSContext.CurrentSite.SiteName);

if (gi != null)
{
addUserToGrp(userAuth, gi.GroupID);
}


Thanks
A

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/30/2010 8:37:45 AM
   
RE:Create a Group and add a User to it
Hi,

following code creates a group and adds a member to it. For more information please check:
http://devnet.kentico.com/docs/devguide/modifying_and_creating_groups.htm?zoom_highlightsub=group
http://devnet.kentico.com/docs/devguide/managing_group_members.htm?zoom_highlightsub=group


private void createGroup() {

// Create empty group object
GroupInfo gi = new GroupInfo();

// Set properties
gi.GroupDisplayName = "New group2";
gi.GroupName = "NewGroup2";
gi.GroupSiteID = 2;
gi.GroupDescription = "";
gi.GroupApproveMembers = GroupApproveMembersEnum.AnyoneCanJoin;
gi.GroupAccess = CMS.SiteProvider.SecurityAccessEnum.AllUsers;
gi.GroupApproved = true;
gi.GroupApprovedByUserID = CurrentUser.UserID;
gi.GroupCreatedByUserID = CurrentUser.UserID;
gi.AllowCreate = SecurityAccessEnum.GroupMembers;
gi.AllowDelete = SecurityAccessEnum.GroupMembers;
gi.AllowModify = SecurityAccessEnum.GroupMembers;
gi.GroupNodeGUID = Guid.Empty;

// Save object to database
GroupInfoProvider.SetGroupInfo(gi);


// If group exist
if (gi != null)
{
// Get user object
UserInfo ui = UserInfoProvider.GetUserInfo("gold");

// If user exists
if (ui != null)
{
// Create new group member object
GroupMemberInfo gmi = new GroupMemberInfo();

//Set properties of member
gmi.MemberGroupID = gi.GroupID;
gmi.MemberApprovedByUserID = CurrentUser.UserID;
gmi.MemberApprovedWhen = DateTime.Now;
gmi.MemberInvitedByUserID = CurrentUser.UserID;
gmi.MemberUserID = ui.UserID;
gmi.MemberJoined = DateTime.Now;

// Save object to database
GroupMemberInfoProvider.SetGroupMemberInfo(gmi);
}
}
}


Best regards,
Ivana Tomanickova