CRM operation permission

Source: Internet
Author: User

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 ;}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.