Thursday, 5 May 2016

PLUGIN STEP BY STEP MS CRM 2O13



For the scope of this article, I assume that you are familiar with C# OOP concepts and C# projects.
  • We will create a plug-in in the C# Class library, you can also use a portable class library.
  • Add a class library project into the C# solution and name your class "MyDynamicPlugin". 


  • Now add references to the 2 DLLs. 

    • Microsoft.Crm.Sdk.Proxy.dll
    • Microsoft.Xrm.Sdk.dll
  • And you will find both of the DLLs in the SDK/bin folder of your Microsoft SDK. 
  • And inherit the IPlugin interface. 

    create IPlugin
  • Implement the IPlugin Interface and you will get an Execute method with the IserviceProvider interface object as a parameter.
  • Execute method: This method runs as an entry point for any plugin in Microsoft Dynamic CRM.
  • IserviceProvider interface: This interface provides access to various services of dynamic, this interface has a method called GetService() that uses the reflection feature of .NET and allows us to get any type of service we want. 

    cs code
  • This Execute method is all the code you need to fire before or after any event occurred.
Let's see how 
For this article I will create a default Contact whenever anyone creates an account.

For example, If an employee of an organization creates an account then a default contact with basic details should be added to that account automatically (using a plug-In).

Procedure

Step 1
Since we need to trigger our plug-in when an event is executed, we need to get the service of IPluginExecutionContext using the IServiceProvider object.
  1. IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));  
And the GetService method returns an object of the specified type. We need to type caste that object. 

Step 2
Plugin execution is an object with a property called InputParameter. 
  • Using this Inputparamter we can get the entity and other types of dynamic CRM. 
  • Target is one of the main parameters of this IPluginExecutionContext object.
  • All the plug-in events are first stored in this variable called “Target”.
  • So when any events occur we need to check if this variable is there in our current context execution.
  • And if so then we need to check if it's an entity. Because for this article we will trigger a plug-in if a new account (Entity) is created.
Entity

Step 3
Now when you know, we have a value in the target and it's an Entity. 

We can typecast it into an Entity and using its logicalname property we can determine if the current executing event is for an account Entity or not.

And if it's an account entity we can write our creation of contact code here and associate that contact with the currently executing account.

executing account

Step 4: The last step is to create an Organizationservicefactory instance and we need to create a new plug-in, add a pluginexecutioncontext object ID to create an Organization service and based on that service client we can create our custom Event. 
  1. IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));  
  2. IOrganizationService client = servicefactory.CreateOrganizationService(context.UserId);   
  3. client.Create(createcontact);  
Your Microsoft Dynamic CRM plug-in is created. 

This is the sample code, you can find the attachment of this code with my article. 

code

Build your Project. 

Important Step 
Once you build your project, you need to generate a key that will act as certificate for your plug-in. Without this key you cannot deploy a plug-in onto a server. 

To create a key, go to project property (ALT+ Enter) and go to the signing tab and check sign the assembly. 

Select new in the drop down and add a key name and your dynamic CRM Password. And select save (CTRL+ S).

If you have done this then it will add a pfx encrypted key for your current project. 

Rebuild your project 
Procedure to deploy this assembly using SDk\Tools\PluginRegistration\PluginRegistration.exe.

Step 1 
  • Run it and Click Create New connection,
  • Enter your Login Credentials for Connection with Dynamic CRM.
  • The first time you login, you need to pass all the details of your CRM Account.
Step 2
After logging in successfully, click Register > Register New Assembly.

Register New Assembly

Browse to the DLL you just created and click OK. 

Step 3
  • Now we need to add steps of this DLL execution.
  • So select the assembly and again click Register > Register new Steps.
  • Or just right-click the assembly you just added and select Register new Steps.

    Register New
This Window includes an option for how and when you want to execute or fire your assembly or plug-in. 

Message: It defines on which event you want to run the plug-in.

Example: Create.

Primary Entity: It define on which Entity execution you want to run the plug-in.

Example: account.

Event pipeline stage of Execution: It defines when you want to run the DLL, before the core operations or after the core operations.

Execution Mode:
 Execution can be synchronous by default or can be asynchronous (that will be handled further by the Dynamic CRM Queue manager).

Deployment: Server where every plug-in is deployed.

Offline where you can deploy it on Outlook for offline use. 

Click Register New Step and whoo, it's done.

Your assembly or plug-in is deployed on the server. Now go to Dynamic CRM and create an account, an automated contact will be added by default in that account without any manual entry from employees. 

Thursday, 28 April 2016

MALWARE CLICKEU.TRACTIONIZE.COM

BASIC PLUGIN STEPS IN MS CRM 2013

BASIC PLUGIN STEPS IN MS CRM 2011/2013/2015/2016

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace LeadRecordw
{
    public class Class1
    {

        public static void Read()
        {
            Entity account = new Entity("account");
            Entity cass = new Entity("incident");
            Entity account2 = new Entity();
            account2.LogicalName = "account";



            // read the values from the entity (any entity which is defined)
            // read means, retrieve the entity's fields name values from the entity is called as "reading" the values 
            account.Attributes.ContainsKey("name");
            string accname = account["name"].ToString();

            DateTime date = (DateTime)account["createdon"];
            DateTime date2 = Convert.ToDateTime(account2["createdon"]);

            string accname1 = account2["name"].ToString();
            //optionset
            OptionSetValue optname = (OptionSetValue)account["new_accounttype"];
            //lookup
            EntityReference Parentaccount = (EntityReference)account["parentaccountid"];
            string parentname = Parentaccount.LogicalName;
            Guid parentid = Parentaccount.Id;

            int age = (int)account["new_age"];

            int age2 = Convert.ToInt32(account["new_age"]);

        }

        public static void Write()
        {

            Entity contact = new Entity("contact");
            contact["name"] = "abc";
            contact["new_age"] = 68;
            DateTime aa = new DateTime(1999, 12, 09);
            contact["createdon"] = aa;

            contact["createdon"] = new DateTime(1999, 12, 09);

            OptionSetValue ad = new OptionSetValue(12);
            contact["new_accounttype"] = ad;
        }


        public static void Main()
        {
            Entity account = new Entity("account");

            string accname = account["accountname"].ToString();

            Entity csd = new Entity("incident");
            csd["incidentname"] = accname;

        }



        public static void read1()
        {
            Entity lead = new Entity("lead");
            Entity lead1 = new Entity();
            lead1.LogicalName = "lead";

            string sub = lead1["subject"].ToString();
            string name = lead1["fullname"].ToString();
            string jobtitle = lead1["jobtitle"].ToString();
            int businessphone = Convert.ToInt32(lead1["telephone1"]);
            int mobilephone = Convert.ToInt32(lead1["mobilephone"]);

            EntityReference parentaccountoflead = (EntityReference)lead1["parentaccountid"];
            string parentaccount = lead1.LogicalName;
            Guid parentid = lead1.Id;

            string email = lead1["email"].ToString();

            EntityReference parentcontact = (EntityReference)lead1["parentcontactid"];
            string parentcont = lead1.LogicalName;
            Guid parentsid = lead1.Id;

            string companyname = lead1["companyname"].ToString();
            string websiteurl = lead1["websiteurl"].ToString();
            string address = lead1["address1_composite"].ToString();
            string description = lead1["description"].ToString();

            int annualrevenue = Convert.ToInt32(lead1["revenue"]);
            int numbofemployyee = Convert.ToInt32(lead1["numberofemployees"]);
            OptionSetValue industry = (OptionSetValue)lead1["industrycode"];
            string SIC = lead1["SIC"].ToString();

            EntityReference currency = (EntityReference)lead1["transcationcurrencyid"];
            string curren = lead1.LogicalName;
            Guid currenid = lead1.Id;
        }


        public static void Write2()
        {
            Entity contact = new Entity("contact");
            contact["fullname"] = "malla";
            contact["jobtitle"] = "MANAGER";

            // here retrieve values from account entity then assign to contact entity fields.

            Entity account = new Entity("account");
            string accname = account["name"].ToString();

            Entity contat = new Entity("contact");
            contat["name"] = accname;
            //contact["fullname"] = accname;

            DateTime date = new DateTime(2001, 12, 12);
            contat["new_date"] = date;

            OptionSetValue acctype = new OptionSetValue(2);
            account["new_accounttype"] = acctype;
        }


        public static void Main1()
        {
            // here iam assigning the contact field name companyname = lead field name parent account for lead 
            Entity contact = new Entity("contact");
            EntityReference compname = (EntityReference)contact["parentcustomerid"];
            string companyname = contact.LogicalName;
            Guid companyid = contact.Id;

            Entity lead = new Entity("lead");
            lead["parentaccountid"] = contact.Id;
        }

        public static void Read1()
        {
            Entity annotation = new Entity("annotation");
            // annotation["createdby"] = "mallareddy";

            Entity cad = new Entity();
            cad.LogicalName = "incident";

            DateTime date = (DateTime)cad["new_date"];

            Entity cad1 = new Entity("incident");
            int casid = Convert.ToInt32(cad1["caseid"]);
            string casename = cad1["casename"].ToString();

            OptionSetValue caseoption = (OptionSetValue)cad1["caseoption"];

            EntityReference caserefer = (EntityReference)cad1["casereference"];
            string casefer = cad1.LogicalName;
            Guid caseferid = cad1.Id;

        }

        public static void Write3()
        {
            Entity account = new Entity("account");
            account["name"] = "kashmir";
            account["address1"] = "london";

            OptionSetValue ad = new OptionSetValue(2);
            account["new_accountype"] = ad;

            DateTime dat = new DateTime(2011, 12, 11);
            account["createdon"] = dat;

        }

        public static void Main3()
        {
            Entity contact = new Entity("contact");
            string companyname = contact["companyname"].ToString();

            Entity cass = new Entity("incident");
            cass["casename"] = companyname;

        }
        public static void Read4()
        {
            Entity contact = new Entity("contact");
            Entity csd = new Entity();
            csd.LogicalName = "incident";

            Entity activity = new Entity();
            activity.LogicalName = "activity";


            Entity account = new Entity();
            string accname = account["accountname"].ToString();
            int accno = Convert.ToInt32(account["accountnumber"]);
            DateTime date = (DateTime)account["new_date"];
            EntityReference companyname = (EntityReference)account["companynameid"];
            OptionSetValue region = (OptionSetValue)account["new_region"];
            bool gender = (bool)account["new_gender"];

            EntityReference parentaccount = (EntityReference)account["parentaccountid"];
            string parentaccountname = account.LogicalName;
            Guid parentaccountid = account.Id;
        }

        public static void Write6()
        {
            Entity acc = new Entity("account");
            acc["accountname"] = "Malla reddy";
            acc["phone"] = 1234555;
            acc["new_weburl"] = "www.excitech.co.uk";

            Guid parentcusromerid = new Guid();
            EntityReference accparentid = new EntityReference();
            accparentid.Id = parentcusromerid;
            accparentid.LogicalName = "account";
            acc["parentaccountid"] = accparentid;




            OptionSetValue region = new OptionSetValue(5);
            acc["new_region"] = region;

            DateTime dat = new DateTime(2001, 12, 03);
            acc["new_date"] = dat;

        }

        public static void Main5()
        {
            Entity account1 = new Entity("account");
            DateTime createdon = (DateTime)account1["createdon"];

            Entity lead = new Entity("lead");
            lead["datecreatedlead"] = createdon;


            string accname = account1["accountname"].ToString();
            lead["leadname"] = accname;

            int accountnumber = Convert.ToInt32(account1["accountnumber"]);
            lead["leadnumber"] = accountnumber;
            //or
            lead["leadnumber"] = Convert.ToInt32(account1["accountnumber"]);

        }
        public static void Retrieve(Guid id, IOrganizationService service)
        {
            Entity account = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("accountname"));
            string accname = account["accountname"].ToString();

            Entity account1 = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("telephone1"));
            int phone = Convert.ToInt32(account["telephone1"]);

            Entity account2 = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("fax"));
            int fax1 = Convert.ToInt32(account["fax"]);

            Entity account3 = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("address1_addresstypecode"));
            OptionSetValue address = (OptionSetValue)account["address1_addresstypecode"];

            Entity account4 = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("createdon"));
            DateTime createdon1 = (DateTime)account["createdon"];

            Entity account5 = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("parentaccountid"));

            EntityReference parentaccount = (EntityReference)account["parentaccountid"];
            string parentacc = account.LogicalName;
            Guid parentaccid = account.Id;

            Entity account6 = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("new_employeesworking"));
            OptionSetValue employeeworking = (OptionSetValue)account["new_employeesworking"];

            //--------------------------------------------------------------------------------------------------

            Entity account7 = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("new_name", "new_age", "emailaddress1"));
            string accountname = account7["new_name"].ToString();
            int accage = Convert.ToInt32(account7["new_age"]);
            string email = account7["emailaddress1"].ToString();


            Entity account8 = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("new_age", "new_bitfield", "new_existingcustomer"));
            int accountage = Convert.ToInt32(account8["new_age"]);
            OptionSetValue bitfield = (OptionSetValue)account8["new_bitfield"];
            OptionSetValue existingcustomer = (OptionSetValue)account8["new_existingcustomer"];


            Entity account9 = service.Retrieve("account", id, new Microsoft.Xrm.Sdk.Query.ColumnSet("new_accounttype", "address1_line1", "new_employeesworking", "new_lastmodifiedby"));
            OptionSetValue acctype = (OptionSetValue)account9["new_accounttype"];
            string addressline = account9["address1_line1"].ToString();
            OptionSetValue employeeworking1 = (OptionSetValue)account9["new_employeesworking"];
            DateTime lastmodifiedby = (DateTime)account9["new_lastmodifiedby"];


        }

        public static void read3()
        {
            Entity lead1 = new Entity("lead");

            Entity lead = new Entity();
            lead.LogicalName = "lead";

            string subject = lead["subject"].ToString();
            string fullname = lead["fullname"].ToString();
            string jobtitle = lead["jobtitle"].ToString();
            int businessphone = Convert.ToInt32(lead["telephone1"]);

            int mobilephone = Convert.ToInt32(lead["mobilephone"]);
            EntityReference parentaccountforlead = (EntityReference)lead["parentaccountid"];
            string email = lead["email"].ToString();

            EntityReference parentcontactforlead = (EntityReference)lead["parentcontactid"];
            string parentcontactname = lead.LogicalName;
            Guid parentcontactid = lead.Id;

            string company = lead["companyname"].ToString();
            string website = lead["websiteurl"].ToString();
            string Address1 = lead["address1_composite"].ToString();

            string description = lead["description"].ToString();

            OptionSetValue industry = (OptionSetValue)lead["industrycode"];
            int annualrevenue = Convert.ToInt32(lead["revenue"]);// currency data type??
            int noofemployees = Convert.ToInt32(lead["numberofemployees"]);

            string SIC = lead["sic"].ToString();

            EntityReference currency = (EntityReference)lead["transcationcurrencyid"];
        }

        public static void Write3()
        {
            Entity contract = new Entity("contract");
            contract["contractnumber"] = 12333333;
            contract["title"] = "BSSYSTEMS";

            //EntityReference accparentid = new EntityReference();
            //accparentid.Id = parentcusromerid;
            //accparentid.LogicalName = "account";
            //acc["parentaccountid"] = accparentid;

            EntityReference customer = new EntityReference();
            customer.LogicalName = "contract";
            contract["customerid"] = customer;  // doubt how to assign values of entity reference

            EntityReference contractaddress = new EntityReference();
            contract.LogicalName = "contract";
            contract["serviceaddress"] = contractaddress; // ??

            DateTime contractstartdate = new DateTime(1988, 12, 09);
            contract["activeon"] = contractstartdate;


            DateTime contractenddate = new DateTime(1999, 12, 12);
            contract["expireson"] = contractenddate;

            contract["duration"] = 12;
        }

        public static void Main5()
        {
            Entity account = new Entity("account");
            string accountnames = account["accountname"].ToString();

            Entity contract = new Entity("contract");
            contract["title"] = accountnames;

        }

        public static void Retreive(Guid id, IOrganizationService service)
        {
            Entity leaddetails = service.Retrieve("leaddetails", id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
        }

        public static void RetreiveMuliple(IOrganizationService service)
        {
            QueryExpression query = new QueryExpression();

            query.EntityName = "lead";
            query.ColumnSet = new ColumnSet(true);
            query.Criteria.Conditions.Add(new ConditionExpression("emailaddress1", ConditionOperator.Equal, "malla@gmail.com"));
            query.Criteria.Conditions.Add(new ConditionExpression("fullname", ConditionOperator.Equal, "malla"));

            EntityCollection leadrecords = service.RetrieveMultiple(query);

            foreach (Entity lead in leadrecords.Entities)
            {
                string email = lead["subject"].ToString();
                Console.WriteLine(email);
            }
        }

        public static void RetreiveMultiple(IOrganizationService service)
        {
            QueryExpression query1 = new QueryExpression();

            query1.EntityName = "contact";
            query1.ColumnSet = new ColumnSet(true);
            query1.Criteria.Conditions.Add(new ConditionExpression("fullname", ConditionOperator.EndsWith, "das"));
            query1.Criteria.Conditions.Add(new ConditionExpression("telephone1", ConditionOperator.EndsWith, "22"));

            EntityCollection contactrecords = service.RetrieveMultiple(query1);

            foreach (Entity contact in contactrecords.Entities)
            {
                string fullname = contact["fullname"].ToString();
                Console.WriteLine(fullname);
            }
        }

        public static void RetreiveMultiple(IOrganizationService service)
        {
            QueryExpression query2 = new QueryExpression();
            
            query2.EntityName = "account";
            query2.ColumnSet = new ColumnSet(true);
            query2.Criteria.Conditions.Add(new ConditionExpression("name", ConditionOperator.DoesNotEndWith, "hsjs"));
            query2.Criteria.Conditions.Add(new ConditionExpression("telephone1", ConditionOperator.DoesNotEndWith, "3222"));

            EntityCollection accountrecord = service.RetrieveMultiple(query2);
            foreach (Entity account in accountrecord.Entities)
            {
                string name = account["name"].ToString();
                Console.WriteLine(name);
            }

        }

        public static void RetreiveMultiplesss(IOrganizationService service)
        {
            QueryExpression query3 = new QueryExpression();
            query3.EntityName = "new_leaddetails";
            query3.ColumnSet = new ColumnSet(true);
            query3.Criteria.Conditions.Add(new ConditionExpression("new_name", ConditionOperator.DoesNotContain, "his"));
            query3.Criteria.Conditions.Add(new ConditionExpression("new_age", ConditionOperator.DoesNotContain, "12"));
            query3.Criteria.Conditions.Add(new ConditionExpression("new_email", ConditionOperator.DoesNotContain, "mll@gmail.com"));
            query3.Criteria.Conditions.Add(new ConditionExpression("new_dob", ConditionOperator.DoesNotContain, "23"));
            query3.Criteria.Conditions.Add(new ConditionExpression("new_rating", ConditionOperator.DoesNotContain, "coolest"));
            query3.Criteria.Conditions.Add(new ConditionExpression("new_connection", ConditionOperator.DoesNotContain, "sample"));
            query3.Criteria.Conditions.Add(new ConditionExpression("new_account", ConditionOperator.DoesNotContain, "simple"));

            EntityCollection leaddetailsrecord = service.RetrieveMultiple(query3);
            foreach (Entity leaddetail in leaddetailsrecord.Entities)
            {
                int name = Convert.ToInt32(leaddetail["new_age"]);
                Console.WriteLine(name);
            }

        }

        public static void RetreiveMultipleA(IOrganizationService service)
        {
            QueryExpression query4 = new QueryExpression();
            query4.EntityName = "opportunity";
            query4.ColumnSet = new ColumnSet("purchaseprocess");
            query4.Criteria.Conditions.Add(new ConditionExpression("purchaseprocess", ConditionOperator.DoesNotContain, "kls"));

            EntityCollection Opportunity = service.RetrieveMultiple(query4);
            foreach (Entity opportunity in Opportunity.Entities)
            {
                OptionSetValue purchaseprocess = (OptionSetValue)opportunity["purchaseprocess"];
                Console.WriteLine(purchaseprocess);
            }
        }
    }
}