Role Members

Role Members

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Items.Item
  • Sitecore.Security.Accounts.User
  • Sitecore.Security.Accounts.Role
  • Sitecore.StringUtil

Role Members C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

string targetUser1 = "Demo\\DemoUser1";
string targetUser2 = "Demo\\DemoUser2";
string targetUser3 = "Demo\\DemoUser3";
string targetUser4 = "Extranet\\Demo";
string targetRole = "Demo\\DemoRole";

Sitecore.Security.Accounts.User demoUser1 = Sitecore.Security.Accounts.User.FromName(targetUser1, false);
Sitecore.Security.Accounts.User demoUser2 = Sitecore.Security.Accounts.User.FromName(targetUser2, false);
Sitecore.Security.Accounts.User demoUser3 = Sitecore.Security.Accounts.User.FromName(targetUser3, true);
Sitecore.Security.Accounts.User demoUser4 = Sitecore.Security.Accounts.User.FromName(targetUser4, false);

Sitecore.Security.Accounts.Role demoRole = Sitecore.Security.Accounts.Role.FromName(targetRole);
if (demoRole == 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);
string itemName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using : for colon and ? for question mark and   for space
output.Write("{0}: {1}", "Item", itemName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Ul);

if (demoRole.IsMember(demoUser1, true, true))
{
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    output.Write("{0}{1}", targetUser1 + " is a member of ", targetRole);
    output.RenderEndTag();
}
else
{

    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    output.Write("{0}{1}", targetUser1 + " is NOT a member of ", targetRole);
    output.RenderEndTag();   
}

if (demoRole.IsMember(demoUser2, true, true))
{
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    output.Write("{0}{1}", targetUser2 + " is a member of ", targetRole);
    output.RenderEndTag();
}
else
{

    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    output.Write("{0}{1}", targetUser2 + " is NOT a member of ", targetRole);
    output.RenderEndTag();
}

if (demoRole.IsMember(demoUser3, true, true))
{
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    output.Write("{0}{1}", targetUser3 + " is a member of ", targetRole);
    output.RenderEndTag();
}
else
{

    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    output.Write("{0}{1}", targetUser3 + " is NOT a member of ", targetRole);
    output.RenderEndTag();
}
if (demoRole.IsMember(demoUser4, true, true))
{
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    output.Write("{0}{1}", targetUser4 + " is a member of ", targetRole);
    output.RenderEndTag();
}
else
{

    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Li);
    output.Write("{0}{1}", targetUser4 + " is NOT a member of ", targetRole);
    output.RenderEndTag();
}

output.RenderEndTag(); //Ul
output.RenderEndTag(); //Div


Role Members C# Web Control Output

  • Demo\DemoUser1 is a member of Demo\DemoRole
  • Demo\DemoUser2 is a member of Demo\DemoRole
  • Demo\DemoUser3 is a member of Demo\DemoRole
  • Extranet\Demo is NOT a member of Demo\DemoRole

Sources