Get Security Role based on Role Name

Today I had to build a method to retrieve a Security Role based on its name and I would like to share it so hopefully it can help anyone. LINQ is used to do the actual search.

///
/// Retrieve a Security Role based on the Role Name
///
///  Return the retrieved Role record. NOTE: If role not found, it will return null
public Role Get_Role(string Role_Name, IOrganizationService Service)
{
    XrmServiceContext Context = new XrmServiceContext(Service);
    Role RetrievedRole = (from x in Context.RoleSet
                           where x.Name.Equals(Role_Name)
                           select x).FirstOrDefault().ToEntity(); // Change the selected attributes as desired. 

    return RetrievedRole;
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s