Thursday, 16 June 2016

Class Vs Interface in C#

Hi,

We all hear Class and Interface words through our IT Career. Here are some important notes about.
An Interface is a Contract - We specify methods and properties in it and a class implements it. Interface doesn't have any implementations.

In a very simple words we can say Interfaces are used to enforce certain Methods/Properties. In a nutshell- an interface is a set of rules. Using the interface clearly states my intent to only use members of the interface. A class can be used to inherit/override base functionality.

For example, an Animal is a class of living things, and a Lion is a class of animals. Inheritance expresses this relationship: an Lion is an Animal where Giraffe inherits from Animal. It can do anything an animal can do and more.

We cannot create an instance of Interface. The only way to "Create an instance of a interface in c#" is to create an instance of a type implementing the interface. The point of the interface is that it guarantees that what ever implements it will provide the methods declared within it.

Here is the small example

We can call DoSomeThing using objClass1 or objClass2 objects like below.
Complete Program

Output

I would prefer to write the same code like below. We can pass Interface as a type like below
Depending the object you pass you get the different behavior.
In the above example, ThisMethodShowsHowItWorks the parameter it expects is of type IIamInterface. This can take anything that implements the variable.

Hope this helps.
--
Happy Coding
Gopinath

Wednesday, 15 June 2016

404 error on MVC web-site

Hi,

Today, I was started working on MVC application. I have taken the Template in Visual Studio and checked it from VS and it is working. Then I have published the things and hosted the same on IIS. When I browsed, it started giving 404 error. After some search, came to know that ASP.Net was not registered with IIS.

Follow below steps to register

Windows 7 and earlier
1. Run the Command Prompt (cmd.exe) as an administrator.
2. Navigate to the appropriate .NET Framework location. (e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319)
3. Run aspnet_regiis.exe -i


For Windows 8 and above
1. From the start menu, type "Turn windows features on or off" and select the first result.
2. Expand Internet Information Services: World Wide Web Services: Application Development Features and select ASP.NET 4.5/4.6/whatever version you see (or ASP.NET 3.5 if you need to support projects on .NET Framework 2.0-3.5).
3. Click OK.


--
Happy Coding

Gopinath

Tuesday, 14 June 2016

Customer Lookup in CRM 2016 Update 1 (8.1)

Hi,

From long time, we all know that there is a field called Customer where we can store two entities information Account and Contact.

Normally Account is used to represent a company and Contact is used to represent Individual Clients (Who are point of contact for a company). Based on need we choose Account or Contact to be stored in that field. OOB already has this type field on the following entities.

Lead
Opportunity
Quote
Order
Contract
Invoice
Case


There is no way where we can create this type of field on Custom Entities till CRM 2016 Update 1. We used to create two lookup fields one for Account and the other for Contact. Now, we can create a field of type Customer to handle this

1) Navigate to Settings --> Customizations --> Select an Entity --> Fields --> New
2) Give the Display Name and select Data Type as Customer


It will create two Many to One (N :1) relation ships from the entity you have selected and 1:N relationship from Account and Contact entities.

Hope this helps

--
Happy CRM'ing
Gopinath

Business Rule on Business Process Flow in CRM 2016

Hi,

When working with Business Process Flows most of the times we get a requirement to show/hide a field based on business conditions. We used to write a Code for it on the Stage Change and Stage Select Events.
In CRM 2016, these kind of things can be achieved using combination of Business Process Flow and Business Rule.

Here are the steps for it.

1) Open Entity Customizations and Business Rule. I have taken Opportunity as an example.

2) Create a New Business Rule as shown in the below image. I have an example to hide Proposed Solution field when Active Stage is Develop and Customer Need field contains data.

We can also write Business Rule on the Stage Change. Here is the sample for it.

In the same way, you can write business rule for setting Required Level/Visibility/Populate some value etc..

Hope this helps.

--
Happy CRM'ing
Gopinath

Tuesday, 7 June 2016

Document Templates in CRM 2016

Whenever there is an update in CRM, we all know that there will be some exciting features in it.
Dynamics 2016 has brought a new way to represent the data by allowing user to create there own templates. This is almost similar to mail merge but very easy to work and use. Templates can be used for Word and Excel.
Word Templates
Here are steps that shows how to create and update a word template on Opportunity Entity.
Open any opportunity and click on Work Templates as shown in Screen shot.
We can create templates from Templates section too.

Navigate to Settings --> Templates --> Click on New --> Select Word Template and Entity and then click on Select Entity button.

This will open a popup to select the related entity relationships.
Select the relation ship and click on Download Template.

Save and Open the template
The template will be empty. To map CRM attributes, we need to enable Developer settings.
Go to File --> Options --> Customize Ribbon and Select Developer --> Click OK
After that select Developer from the Ribbon and click on XML Mapping Pane
Change the Custom XML Part to the link which starts with "urn"
It will display the Entity attributes and relation ship which we have selected while downloading the document template.

Select the fields and right click and select 'Insert Content Control' and create your layout using word tools.

The Associated entity fields are at the button of the list.
Insert a Table on the document with Two Rows (One is Header and other for data mapping)
Select each cell and map the required fields.
The most important part, Select the entire row (all the cells of the row, make sure you don't select  the table or the row from the table border).

Once the entire row is selected, right click the relation ship and select Repeating from Insert Content Control menu.
The table should look like the below screen shot.
Save the document.

Open CRM, navigate to Settings --> Templates --> Document Templates --> Click on Upload Template button. 

Browse the file from your location machine and click on Upload. CRM creates a document template record as below.
Open the record in CRM and Select the Template which you have uploaded as shown in the screen shot.
Here is the document generated for us.
--
Happy CRM'ing
Gopinath

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

Friday, 22 April 2016

The expected parameter has not been supplied for the report (Report render failure. Error: The 'CRM_CalendarType' parameter is missing a value)

Hi,
 
Today when we are trying to run the report for one of the user we are getting error saying
 
"The expected parameter has not been supplied for the report."

 
We did check in the Event Viewer and observed the below error message
 
"The expected parameter has not been supplied for the report."
 
I was pretty sure that there some thing we were missing as I was running OOB report.
 
The solution for this issue is very simple as the user does not have read permission for user settings , report not able to retrieve his calendar details (like Year format)
once assign read permission for User settings everything works fine.
 
  • Click Settings , click Administration , and then click Security Roles .
  • Double-click the security role that you use.
  • On the Business Management tab, set the Read privilege of the User Settings entity at least to the Business Unit level.
  • Click Save and Close.
 
Hope this helps.

--
Happy CRM'ing

Gopinath

Thursday, 14 April 2016

Close Opportunity as Lost in CRM 2011/2013/2015/2016

Hi,
 
Here is the code to close an Opportunity as Lost using the MS Dynamics CRM SDK.
 
In this example, I am using LoseOpportunityRequest to close an Opportunity as Lost.

public static void CloseOpportunityAsLose(IOrganizationService crmService, Guid guidOpportunityId)
{
      LoseOpportunityRequest req = new LoseOpportunityRequest();
      Entity opportunityClose = new Entity("opportunityclose");
      opportunityClose.Attributes.Add("opportunityid", new EntityReference("opportunity", guidOpportunityId));
      opportunityClose.Attributes.Add("subject", "Lost the Opportunity!");
      req.OpportunityClose = opportunityClose;
      OptionSetValue osvLostValue = new OptionSetValue();
      osvLostValue.Value = 4;
      req.Status = osvLostValue;
      LoseOpportunityResponse resp = (LoseOpportunityResponse)crmService.Execute(req);
}

Hope this helps.
 
--
Happy CRM'ing

Gopinath

 

Close Opportunity as Won in CRM 2011/2013/2015/2016

Hi,
 
Here is the code to close an Opportunity as Won using the MS Dynamics CRM SDK.
 
In this example, I am using WinOpportunityRequest to close an Opportunity as Won.

public static void CloseOpportunityAsWon(IOrganizationService crmService, Guid guidOpportunityId)
{
       WinOpportunityRequest req = new WinOpportunityRequest();
       Entity opportunityClose = new Entity("opportunityclose");
       opportunityClose.Attributes.Add("opportunityid", new EntityReference("opportunity", guidOpportunityId));
       opportunityClose.Attributes.Add("subject", "Won the Opportunity!");
       req.OpportunityClose = opportunityClose;
       OptionSetValue osvWon = new OptionSetValue();
       osvWon.Value = 3;
       req.Status = osvWon;
       WinOpportunityResponse resp = (WinOpportunityResponse)crmService.Execute(req);
}

Hope this helps.
 
--
Happy CRM'ing

Gopinath

 

Tuesday, 12 April 2016

C# Code for Removing Security Role to a User - CRM 2011/2013/2015/2016

Hi,

In the last post we have seen how to assign a security role to the user programmatically.

Here is the code for removing the security roles of the users.

public void RemoveUserSecurityRole(Guid guidSystemUserId, Guid guidSecurityRoleId, IOrganizationService crmService)
{
         // Create new Disassociate Request object for creating a N:N link between User and Security
         DisassociateRequest objDisassociateRequest = new DisassociateRequest();
         // Create related entity reference object for associating relationship
         // we will pass System User record reference of user for which the role is required to be removed 
         objDisassociateRequest.RelatedEntities = new EntityReferenceCollection();
         objDisassociateRequest.RelatedEntities.Add(new EntityReference("systemuser", guidSystemUserId));
        // Create new Relationship object for System User & Security Role entity schema and assigning it 
        // to request relationship property
        objDisassociateRequest.Relationship = new Relationship("systemuserroles_association");
        // Create target entity reference object for associating relationship
        objDisassociateRequest.Target = new EntityReference("role", guidSecurityRoleId);
        // Passing DisassociateRequest object to Crm Service Execute method for removing Security Role to User
        crmService.Execute(objDisassociateRequest);
}

Hope this helps.

--
Happy CRM'ing
Gopinath