Domain Roles

Domain Roles

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Configuration.Factory
  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Security.Accounts.Role
  • Sitecore.Security.Accounts.User
  • Sitecore.Security.Domains.Domain
  • System.Collections.Generic.IEnumerable
  • System.Web.HttpContext

Domain Roles C# Web Control Code

Sitecore.Security.Domains.Domain domain = Sitecore.Configuration.Factory.GetDomain("demo");
Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (domain == null || item == null) return;

System.Collections.Generic.IEnumerable<Sitecore.Security.Accounts.Role> roles = domain.GetRoles();
System.Collections.Generic.IEnumerable<Sitecore.Security.Accounts.User> users = domain.GetUsers();
if (roles == null || users == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
///using &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

foreach (Sitecore.Security.Accounts.Role role in roles)
{
    System.Web.HttpContext.Current.Response.Write("Name: <b>" + role.Name + "</b><br/>"); 
    System.Web.HttpContext.Current.Response.Write("Description: <b>" + role.Description + "</b><br/>");
    System.Web.HttpContext.Current.Response.Write("Display Name: <b>" + role.DisplayName + "</b><br/>");
    System.Web.HttpContext.Current.Response.Write("Account Type: <b>" + role.AccountType.ToString() + "</b><br/>");
    System.Web.HttpContext.Current.Response.Write("Domain: <b>" + role.Domain.ToString() + "</b><br/>");
    System.Web.HttpContext.Current.Response.Write("Is member of Everyone?: <b>" + role.IsEveryone.ToString() + "</b><br/>");
    System.Web.HttpContext.Current.Response.Write("Is Global: <b>" + role.IsGlobal.ToString() + "</b><br/>");
    System.Web.HttpContext.Current.Response.Write("Local Name: <b>" + role.LocalName + "</b><br/>");

    foreach (Sitecore.Security.Accounts.User user in users)
    {
        string roleMember = role.IsMember(user, true, true).ToString();
        System.Web.HttpContext.Current.Response.Write("Member: <b>" + user.DisplayName + "</b><br/>");
    }
}
output.RenderEndTag(); //Div


Name: demo\DemoRole
Description: Role
Display Name: demo\DemoRole
Account Type: Role
Domain: demo
Is member of Everyone?: False
Is Global: False
Local Name: demo\DemoRole
Member: demo\Anonymous
Member: demo\DemoUser1
Member: demo\DemoUser2
Member: demo\DemoUser3
C# Web Control Real-time Output

Item: Domain Roles

Notes

  • A security domain named demo was created on this site
  • A role named DemoRole was created in the demo security domain
  • Three user accounts named DemoUser1, DemoUser2, and DemoUser3 were created in the demo security domain
  • The anonymous user was added to the demo security domain automatically by Sitecore