Click or drag to resize

Manage group operations

You can perform various group operations via API.

Group operations
Add following namespaces in your code
C#
using Novalys.VisualGuard.Security;
using Novalys.VisualGuard.Security.Common;
using Novalys.VisualGuard.Security.CommonProfileAttribute;
using Novalys.VisualGuard.Security.Database;
using Novalys.VisualGuard.Security.Membership;
using Novalys.VisualGuard.Security.UserProfile;
using System;
using System.Collections.Generic;
Define VisualGuard runtime
C#
VGSecurityRuntime runtime = VGSecurityManager.Runtime;
Create/Update/Delete VGGroup
C#
//Create/Update/Delete Group 

 //create group 

 VGGroup groupHR = runtime.Groups.CreateGroup("HR Department", "This group represents a HR Division", null);

 //create group under specific group
 VGGroup groupHRBranchA = runtime.Groups.CreateGroup("HR Department- Branch A", groupHR);
 VGGroup groupHRBranchB = runtime.Groups.CreateGroup("HR Department- Branch B", groupHR);
 VGGroup groupHRBranchC = runtime.Groups.CreateGroup("HR Department- Branch C", groupHR);

 //update group
 groupHR.Description = "HR Division";
 groupHR.Data1 = "Data1 - You can store any important information here";
 groupHR.Data2 = "Data2 - You can store any important information here";
 groupHR.Data3 = "Data3 - You can store any important information here";
 groupHR.Name = "HR Dept";

 runtime.Groups.UpdateGroup(groupHR);

 //delete group
 runtime.Groups.DeleteGroup(groupHRBranchC);
Create group profile attributes
C#
//Create group profile attributes
 var attrBranchCode = runtime.Groups.CreateProfileAttribute("BranchCode",VGCommonProfileDataType.String, "BranchCode", "This attribute represents a branch code");
 var attrBranchActive = runtime.Groups.CreateProfileAttribute("IsBranchActive", VGCommonProfileDataType.Boolean, "IsActive", "This attribute represents if branch is active");


 var profileAttribteDef = (VGProfileAttributeDefinitionDropDownList) runtime.Groups.GetProfileAttributeDefinition(VGCommonProfileDataType.DropDownList);
 profileAttribteDef.Items = new string[] { "Zone 1", "Zone 2", "Zone 3" };
 profileAttribteDef.DefaultValue = "Zone 1";

 var attrBranchZone = runtime.Groups.CreateProfileAttribute("BranchZone",VGCommonProfileDataType.DropDownList, "BranchZone", "This attribute represents if branch is active", true, false, false, true, 1, "Basic Information", profileAttribteDef);
Get group profile values.
C#
string branchCode = groupHRBranchA.GetValue<string>("BranchCode");
bool isBranchActive = groupHRBranchA.GetValue<bool>("IsBranchActive");
string branchZone = groupHRBranchA.GetValue<string>("BranchZone");
Save/Update group profile values
C#
//Save/Update group profile values
 groupHRBranchA.SetValue<string>(attrBranchCode.Id, "Code_442");
 groupHRBranchA.SetValue<bool>(attrBranchActive.Id, true);
 groupHRBranchA.SetValue<string>(attrBranchZone.Id, "Zone 2");

 runtime.Groups.UpdateGroup(groupHRBranchA);

 //Save/Update group profile values
 groupHRBranchB.SetValue<string>(attrBranchCode.Id, "Code_443");
 groupHRBranchB.SetValue<bool>(attrBranchActive.Id, false);
 groupHRBranchB.SetValue<string>(attrBranchZone.Id, "Zone 1");

 runtime.Groups.UpdateGroup(groupHRBranchB);
Get all groups from storage depending on rights of the user

There are various prototypes and methods are available to get groups. Few examples have been shown below

C#
 //Get all groups
var allGroups = runtime.Groups.GetAllGroup();

//Get all descendant groups of particular group
var childGroups =runtime.Groups.GetAllDescendantGroupsFromGroup(groupHR);

//Get all parent groups of particular group
var parentGroups = runtime.Groups.GetAllParentGroups(groupHRBranchA);
Find groups by various parameters
C#
//find groups by groupname pattern
var findGroups = runtime.Groups.FindGroupsByName("HR Department");
findGroups = runtime.Groups.FindGroupsByName("%Department%");

//find groups by profile attribute
findGroups =  runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchCode.Id, "Code_442");

findGroups = runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchActive.Id, false);
findGroups = runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchActive.Id, true);

findGroups = runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchZone.Id, "Zone 1");
findGroups = runtime.Groups.FindGroupsByProfileAttributeValue(attrBranchZone.Id, "Zone 2");
Assign/Remove user to/from group
C#
var userJsmith = runtime.Membership.GetUser("jsmith");

//assign user to group
runtime.Groups.AddUserToGroup(userJsmith, groupHRBranchA);

//remove user from group
runtime.Groups.RemoveUserFromGroup(userJsmith, groupHRBranchA);