CRM 2011 - C# ILE ACCOUNT ,CONTACT OLUŞTURMA VE CONNECTION ROLE ATAMA (MICROSOFT DYNAMICS CRM ONLINE)



Aşağıda örnek kod yazılımı verilmiştir.
Namespacelerin dll lerini projemize dahil edelim ve framework=4.0 olmalı

Device ıd mizi commandprompt da olusturuyoruz. ve olusan classı projemıze dahıl edıyoruz.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.ServiceModel.Description;
using Microsoft.Crm;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Messages;


namespace device_Id_kullanimli
{
    class Program
    { 
       IOrganizationService ser;
       Guid _connectionRoleId;
       Guid _connectionId;
       Guid _accountId;
       Guid _contactId;
      
        static void Main(string[] args)
        {
            Program uygulama = new Program();
            
            uygulama.RetrieveMultipleWithRelatedEntityColumns();
            uygulama.GetConnectionRole();

            Console.ReadKey();
        }//main ends

      
        public  void RetrieveMultipleWithRelatedEntityColumns()
        {
            ser = Factory.getCRMOnlineService();

            Console.WriteLine("Entering:RetrieveMultipleWithRelatedEntityColumns");
            //Create multiple accounts with primary contacts
            
            Entity contact = new Entity("contact");
            contact.Attributes["firstname"] = "DenemeFirstName";
            contact.Attributes["lastname"] = "DenemeLastName";   
      
            _contactId = ser.Create(contact);
            
            Entity account = new Entity("account");
            account["name"] = "Test Account1";
            account["websiteurl"] = "gokhanmentese.com";
            account["fax"]="21221212";
            account["address1_postalcode"] = "98052";
            account["new_secenek"]=true;
            account["donotemail"]=true;

            var Categories = new
            {
                gokhan = 11,
               onnur = 12,
                isa = 13,            
            };

            var Categories2 = new
            {
               AirBorne=1,
               DHL=2,
               FedEx=3,
               UPS=4,
               PostalMail=5,
               FullLoad=6,
               WillCall=7
            };

            account["new_optionset"] = new OptionSetValue(Categories.gokhan);
            account["address1_shippingmethodcode"]=new OptionSetValue(Categories2.WillCall);
            account["new_twooptions"] = false;

            EntityReference primaryContactId ;

            primaryContactId= new EntityReference("contact", _contactId);
            account["primarycontactid"] = primaryContactId;
          
            _accountId = ser.Create(account);
            //account["name"] = "Test Account2";
            //Guid accountId2 = ser.Create(account);
            //account["name"] = "Test Account3";
            //Guid accountId3 = ser.Create(account);
          
            QueryExpression qe = new QueryExpression();
            qe.EntityName = "account";
            qe.ColumnSet = new ColumnSet();
            qe.ColumnSet.Columns.Add("name");
           
            LinkEntity lentity ;

            lentity = new LinkEntity("account", "contact", "primarycontactid", "contactid", JoinOperator.Inner);         
            qe.LinkEntities.Add(lentity);
            

            qe.LinkEntities[0].Columns.AddColumns("firstname", "lastname");
            qe.LinkEntities[0].EntityAlias = "primarycontact";

            EntityCollection ec = ser.RetrieveMultiple(qe);

            Console.WriteLine("Retrieved {0} entities", ec.Entities.Count);
            Console.ReadKey();

            foreach (Entity act in ec.Entities)
            {
                Console.WriteLine("account name:" + act["name"]);
                Console.WriteLine("primary contact first name:" + act["primarycontact.firstname"]);
                Console.WriteLine("primary contact last name:" + act["primarycontact.lastname"]);           
            }
            Console.ReadKey();
        }

        public void GetConnectionRole()
        {
            try
            {     
                var Kategori = new
                {
                    Business = 1,
                    Family = 2,
                    Social = 3,
                    Sales = 4,
                    Other = 5
                };

                ConnectionRole cRole = new ConnectionRole
                {
                    Name = "Ornek Connection Role",
                    Category = new OptionSetValue(Kategori.Family)   
                };

                _connectionRoleId = Factory.serviceProxy.Create(cRole);


                Console.WriteLine("Created {0}.", cRole.Name);
               
                // Create a related Connection Role Object Type Code record for Account
                ConnectionRoleObjectTypeCode newAccountConnectionRole1TypeCode
                    = new ConnectionRoleObjectTypeCode
                    {
                        ConnectionRoleId = new EntityReference(
                            ConnectionRole.EntityLogicalName, _connectionRoleId),
                        AssociatedObjectTypeCode = Account.EntityLogicalName
                    };

                Factory.serviceProxy.Create(newAccountConnectionRole1TypeCode);

                Console.WriteLine(
                    "Created a related Connection Role 1 Object Type Code record for Account."
                    );

                // Create a related Connection Role Object Type Code record for Contact
                ConnectionRoleObjectTypeCode newContactConnectionRoleTypeCode
                    = new ConnectionRoleObjectTypeCode
                    {
                        ConnectionRoleId = new EntityReference(
                            ConnectionRole.EntityLogicalName, _connectionRoleId),
                        AssociatedObjectTypeCode = Contact.EntityLogicalName
                    };

                Factory.serviceProxy .Create(newContactConnectionRoleTypeCode);
                Console.WriteLine(
                    "Created a related Connection Role Object Type Code record for Contact."
                    );

                // Associate the connection role with itself.
                AssociateRequest associateConnectionRoles = new AssociateRequest
                {
                    Target = new EntityReference(ConnectionRole.EntityLogicalName,
                        _connectionRoleId),
                    RelatedEntities = new EntityReferenceCollection()
                        {
                            new EntityReference(ConnectionRole.EntityLogicalName,
                                _connectionRoleId)
                        },
                    // The name of the relationship connection role association 
                    // relationship in MS CRM.
                    Relationship = new Relationship()
                    {
                        PrimaryEntityRole = EntityRole.Referencing, // Referencing or Referenced based on N:1 or 1:N reflexive relationship.
                        SchemaName = "connectionroleassociation_association"
                    }
                };

                Factory.serviceProxy .Execute(associateConnectionRoles);
                Console.WriteLine("Associated the connection role with itself.");

                Connection newConnection = new Connection
                {
                    Record1Id = new EntityReference(Account.EntityLogicalName,
                        _accountId),
                    Record1RoleId = new EntityReference(ConnectionRole.EntityLogicalName,
                        _connectionRoleId),
                    Record2RoleId = new EntityReference(ConnectionRole.EntityLogicalName,
                        _connectionRoleId),
                    Record2Id = new EntityReference(Contact.EntityLogicalName,
                        _contactId)
                };

                _connectionId =Factory.serviceProxy.Create(newConnection);

                Console.WriteLine(
                    "Created a connection between the account and the contact.");
            

            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            {
                
                throw;
            }
         
        }
        
        public void DeleteConnectionRole(bool prompt)
        {
            bool deleteRecords = true;

            if (prompt)
            {
                Console.WriteLine("\nDo you want these entity records deleted? (y/n)");
                String answer = Console.ReadLine();

                deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y"));
            }

            if (deleteRecords)
            {
                Factory.serviceProxy.Delete(ConnectionRole.EntityLogicalName, _connectionRoleId);

                Console.WriteLine("Entity records have been deleted.");
            }
        }
        
    }

       public class Factory
        {
           public  static OrganizationServiceProxy serviceProxy;

        public static IOrganizationService getCRMOnlineService()
        {
            String userName;
            String password;
            String organizationUrl;
            userName = "livemailaddress";
            password = "password";
            organizationUrl = "https://deneme.crm4.dynamics.com/XRMServices/2011/Organization.svc";

            
            Uri OrganizationUri =new Uri(organizationUrl);

            ClientCredentials DeviceCredentials = new ClientCredentials();
            DeviceCredentials.UserName.UserName = "11c8kfpjig2pg5z4mn2t6a6zjq";
            DeviceCredentials.UserName.Password = "IA_ARbUy2qi!H0=fRlOc_/`5";
            
            ClientCredentials credentials = new ClientCredentials();
            credentials.UserName.UserName = userName;
            credentials.UserName.Password = password;
            
            serviceProxy = new OrganizationServiceProxy(OrganizationUri, null, credentials, DeviceCredentials);
            serviceProxy.EnableProxyTypes();
    
            IOrganizationService _service = (IOrganizationService)serviceProxy;           
            return _service;


        }
        }
}

Anlaşılmayan bölümler için yorum yazınız.

Hiç yorum yok:

Yorum Gönder