Using system;
Using Microsoft. xrm. SDK;
Using Microsoft. xrm. SDK. query;
Using system. Collections. Generic;
Using Microsoft. CRM. SDK. messages;
/// <Summary>
/// Permission
/// </Summary>
Public class privilegehelper
{
Public static readonly string entityname = "Privilege ";
/// <Summary>
/// Query the permissions of an object
/// Generally, an object has eight basic permissions.
/// </Summary>
Public list <privilege> searchprivilegebyentityname (iorganizationservice service, string name)
{
List <privilege> List = new list <privilege> ();
// Generally, an object has eight permissions. Assume that the object is an account.
// Prvcreateaccount: Creates a customer, prvreadaccount: reads the customer record, and prvwriteaccount: modifies the customer
// Prvdeleteaccount: delete customer, prvappendaccount: append, prvappendtoaccount: append
// Prvassignaccount: assigns a customer and prv1_account: shares the customer.
Queryexpression query = new queryexpression ();
Query. entityname = entityname;
Query. columnset = new columnset ("name ");
Query. Criteria. addcondition (New conditionexpression ("name", conditionoperator. Like, "PRV %" + name ));
Entitycollection EC = service. retrievemultiple (query );
If (EC! = NULL & EC. Entities. Count> 0)
{
Foreach (entity en in EC. Entities)
{
Privilege P = new privilege ();
P. ID = en. ID;
P. Name = en ["name"]. tostring ();
List. Add (P );
}
}
Return list;
}
/// <Summary>
/// Query a permission by ID
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "privilegeid"> permission id </param>
Public privilege searchprivilegebyid (iorganizationservice service, guid privilegeid)
{
Entity en = service. Retrieve (entityname, privilegeid, new columnset ("name "));
Privilege P = new privilege () {id = privilegeid };
P. Name = en ["name"]. tostring ();
Return P;
}
/// <Summary>
/// Query the permissions of a user
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "userid"> User </param>
Public void searchprivilegebyuserid (iorganizationservice, guid userid)
{
Retrieveuserprivilegesrequest request = new retrieveuserprivilegesrequest ();
Request. userid = userid;
Retrieveuserprivilegesresponse response = (retrieveuserprivilegesresponse) service. Execute (request );
If (response. roleprivileges! = NULL)
{
Roleprivilege [] arrayresult = response. roleprivileges;
}
}
/// <Summary>
/// Retrieve the privileged set defined in the system
/// </Summary>
/// <Param name = "service"> service </param>
Public list <privilege> searchallprivilege (iorganizationservice Service)
{
List <privilege> List = new list <privilege> ();
Retrieveprivilegesetrequest request = new retrieveprivilegesetrequest ();
Retrieveprivilegesetresponse response = (retrieveprivilegesetresponse) service. Execute (request );
If (response. entitycollection! = NULL)
{
Entitycollection EC = response. entitycollection;
If (EC! = NULL & EC. Entities. Count> 0)
{
Foreach (entity en in EC. Entities)
{
Privilege P = new privilege ();
P. ID = en. ID;
P. Name = en ["name"]. tostring ();
List. Add (P );
}
}
}
Return list;
}
Public Class privilege
{
Public guid ID {Get; set ;}
Public string name {Get; set ;}
}
}