Tuesday 31 May 2016

Get the list of all properties of a class in C#

Hi,

Today we got a requirement to get the list of all the properties of a class including the values.

Here is a C# code for it.

class Customer
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public string MiddleName { get; set; }
}

class Program
{
     static void Main(string[] args)
     {
         // Creating an object for a class
         Customer objCustomer = new Customer();
         objCustomer.FirstName = "John";
         objCustomer.LastName = "Hung";
         objCustomer.MiddleName = "Maro";

         // Reading the properties and displaying the Name and Value.
         foreach (var prop in objCustomer.GetType().GetProperties())
         {
             Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(objCustomer, null));
         }
         Console.Read();
      }
}

Hope this helps.

--
Happy Coding

Gopinath

Monday 30 May 2016

Permissions of the User on a record - RetrievePrincipalAccessRequest in CRM 2001//2013/2015/2016


Hi,


Today we got a requirement what are the permissions of the user on a particular record. To fullfill this requirement, there is a SDK message called "RetrievePrincipalAccessRequest"


Here is the sample code of it.

RetrievePrincipalAccessRequest retrieveRequest = new RetrievePrincipalAccessRequest();
// record for which we want to check the access
retrieveRequest.Target = new EntityReference("opportunity", new Guid("A9F18D5F-6A08-E611-80DE-000D3AA03DA0"));
// User or Team entity Reference
retrieveRequest.Principal = new EntityReference("systemuser", new Guid("1C5A2AE8-AD00-E611-80DD-000D3AA03DA0"));
RetrievePrincipalAccessResponse retrieveResponse = (RetrievePrincipalAccessResponse)crmSerivce.Execute(retrieveRequest);


In the response we get the combination of all the rights either through sharing or his own security roles


Hope this helps.

--
Happy CRM'ing

Gopinath

Saturday 28 May 2016

Retrieve Users/Teams who has access on the record

Hi,

Today, we got a requirement to retrieve list of users/Teams who has access on the record as part of business requirement.

SDK has a message to full fill this requirement. i.e RetrieveSharedPrincipalsAndAccessRequest

Here is the C# sample code for using it.


RetrieveSharedPrincipalsAndAccessRequest req = new RetrieveSharedPrincipalsAndAccessRequest();
req.Target = erfRecord;
RetrieveSharedPrincipalsAndAccessResponse resp = (RetrieveSharedPrincipalsAndAccessResponse)crmService.Execute(req);
foreach(PrincipalAccess prinAccess in resp.PrincipalAccesses)
{
     // prinAccess.AccessMask
     // prinAccess.Principal
}

AccessMask holds the access information
Principal holds the User/Team information.


--
Happy CRM'ing

Gopinath