Kentico CMS 6.0 Developer's Guide

Managing group members

Managing group members

Previous topic Next topic Mail us feedback on this topic!  

Managing group members

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example adds a user as a member of a group.

 

private bool CreateGroupMember()
{

  // Get the group
  GroupInfo group = GroupInfoProvider.GetGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

 
    if (group != null)
    {

      // Get the user
      UserInfo user = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserName);

 
        if (user != null)
        {
            // Create new group member object
            GroupMemberInfo newMember = new GroupMemberInfo();
 
            //Set the properties
            newMember.MemberGroupID = group.GroupID;
            newMember.MemberApprovedByUserID = CurrentUser.UserID;
            newMember.MemberApprovedWhen = DateTime.Now;
            newMember.MemberInvitedByUserID = CurrentUser.UserID;
            newMember.MemberUserID = user.UserID;
            newMember.MemberJoined = DateTime.Now;
            newMember.MemberComment = "New member added by API example.";
 
            // Save the member
            GroupMemberInfoProvider.SetGroupMemberInfo(newMember);
        }
 
        return true;
    }
 
    return false;
}

 

The following example gets and updates a group member.

 

private bool GetAndUpdateGroupMember()
{

  // Get the group
  GroupInfo group = GroupInfoProvider.GetGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

 
    if (group != null)
    {

      // Get the user
      UserInfo user = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserName);

 
        if (user != null)
        {

          // Get the group member
          GroupMemberInfo updateMember = GroupMemberInfoProvider.GetGroupMemberInfo(user.UserID, group.GroupID);

            if (updateMember != null)
            {

              // Update the properties
               updateMember.MemberComment = updateMember.MemberComment.ToLower();

 
                // Save the changes
                GroupMemberInfoProvider.SetGroupMemberInfo(updateMember);
 
                return true;
            }
        }
    }
 
    return false;
}

 

The following example gets and bulk updates group members.

 

private bool GetAndBulkUpdateGroupMembers()
{
    // Prepare the parameters
    string where = "MemberUserID =" + CMSContext.CurrentUser.UserID;
        
    // Get the data
    DataSet members = GroupMemberInfoProvider.GetGroupMembers(where, null);
    if (!DataHelper.DataSourceIsEmpty(members))
    {
        // Loop through the individual items
        foreach (DataRow memberDr in members.Tables[0].Rows)
        {
            // Create object from DataRow
            GroupMemberInfo modifyMember = new GroupMemberInfo(memberDr);
 
            // Update the properties
            modifyMember.MemberComment = modifyMember.MemberComment.ToUpper();
 
            // Save the changes
            GroupMemberInfoProvider.SetGroupMemberInfo(modifyMember);
        }
 
        return true;
    }
 
    return false;
}

 

The following example removes a member from a group.

 

private bool DeleteGroupMember()
{

  // Get the group
  GroupInfo group = GroupInfoProvider.GetGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

 
    if (group != null)
    {

      // Get the user
      UserInfo user = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserName);

 
        if (user != null)
        {

          // Get the group member
          GroupMemberInfo deleteMember = GroupMemberInfoProvider.GetGroupMemberInfo(user.UserID, group.GroupID);

            if (deleteMember != null)
            {
                // Save the changes
                GroupMemberInfoProvider.DeleteGroupMemberInfo(deleteMember);
 
                return true;
            }
        }
    }
 
    return false;
}

 

The following example creates an invitation to a group for a user.

 

private bool CreateInvitation()
{
  // Get the group
  GroupInfo group = GroupInfoProvider.GetGroupInfo("MyNewGroup", CMSContext.CurrentSiteName);

 
  if (group != null)
   {
      // Create new invitation
      InvitationInfo newInvitation = new InvitationInfo();

 
      // Set properties
       newInvitation.InvitationComment = "Invitation created by API example.";
       newInvitation.InvitationGroupID = group.GroupID;
       newInvitation.InvitationUserEmail = "admin@localhost.local";
       newInvitation.InvitedByUserID = CMSContext.CurrentUser.UserID;
       newInvitation.InvitationCreated = DateTime.Now;
       newInvitation.InvitationValidTo = DateTime.Now.AddDays(1);

 
      // Save object
      InvitationInfoProvider.SetInvitationInfo(newInvitation);

 
      return true;
   }

 
  return false;
}