Monday, July 9, 2012

How to Attach Notes in Entity in CRM 2011.


Introduction:
                   Hello Today I am going to show tou how to Add Notes in Entity.Here I have insert one Record into the Account entity and after that i am inserting Notes into that Record.

For this First you have to create Console Application.

See the Class Code here.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

using System.ServiceModel.Description;
using System.ServiceModel;
public class Program
    {
        private static IOrganizationService _service;
        private static OrganizationServiceProxy _serviceProxy;
        private static OrganizationServiceContext orgContext;
        static public void Main(string[] args)
        {
            Program p = new Program();
            p.InsertAccount();
            Console.WriteLine("Inserted eith Notes");
            Console.ReadKey();
        }
        private void InsertAccount()
        {
            try
            {
                string file = "insertlead.txt";
                GetCRMService();
                Entity entity = new Entity("account");
                entity["name"] = "Kartik G Patel";

                entity["emailaddress1"] = "kartik@gmail.com";
                 Guid Accountid=_service.Create(entity);

                


                Entity annotation = new Entity("annotation");
                annotation["objectid"] = new EntityReference("account", Accountid);
                annotation["subject"] = "Kartik Comments";
                annotation["notetext"] = Convert.ToString("This is Kartik's Comment.");
                _service.Create(annotation);
            



            }
            catch (FaultException ex)
            {
                File.AppendAllLines("error.txt", new string[] { (ex.InnerException == null ? ex.Message.ToString() : ex.InnerException.ToString()) });
            }
        }
    
        public static void GetCRMService()
        {
            try
            {

                string file = "connection.txt";
                Uri organizationuri = new Uri("https://kartik.crm5.dynamics.com/XRMServices/2011/Organization.svc");
                Uri homeRealmUri = null;

                /*Live CRM*/
                var deviceIDcreds = new ClientCredentials();
                deviceIDcreds.UserName.UserName = "11pqlm4ldg5nqps9b4ivlngbfv";
                deviceIDcreds.UserName.Password = "J-n60IBG7h;Ga`b5##SsDQhP";



                /*Live CRM*/
                var liveIDCreds = new ClientCredentials();
                liveIDCreds.UserName.UserName = "username";
                liveIDCreds.UserName.Password = "password";

                /* If you are not using Live CRM
                ClientCredentials credential = new ClientCredentials();
                credential.Windows.ClientCredential = new NetworkCredential("username", "password", "Organization Name");
                */
                //_serviceProxy = new OrganizationServiceProxy(organizationuri, homeRealmUri, credential, null);
                _serviceProxy = new OrganizationServiceProxy(organizationuri, homeRealmUri, liveIDCreds, deviceIDcreds);
                _serviceProxy.EnableProxyTypes();

                _service = (IOrganizationService)_serviceProxy;
                orgContext = new OrganizationServiceContext(_serviceProxy);
                File.AppendAllLines(file, new string[] { "connection tested" });
            }
            catch (FaultException ex)
            {
                File.AppendAllLines("error.txt", new string[] { (ex.InnerException == null ? ex.Message.ToString() : ex.InnerException.ToString()) });
            }
        }
    }

In My code you can see the comments where i show you if you are not using Live CRM then just Replace

var deviceIDcreds = new ClientCredentials();
deviceIDcreds.UserName.UserName = "11pqlm4ldg5nqps9b4ivlngbfv";
deviceIDcreds.UserName.Password = "J-n60IBG7h;Ga`b5##SsDQhP";

var liveIDCreds = new ClientCredentials();
liveIDCreds.UserName.UserName = "username";
liveIDCreds.UserName.Password = "password";

with

ClientCredentials credential = new ClientCredentials();
credential.Windows.ClientCredential = new NetworkCredential("username", "password", "Organization Name");
_serviceProxy = new OrganizationServiceProxy(organizationuri, homeRealmUri, credential, null);

Download the Sample Code from Here
 Download

1 comment: