Wednesday, November 28, 2012

CRM 2011-SSIS (helper class to connect with CRM)

Steps to integrating CRM 2011 using SSIS..click the following link and use the following code to suuport .NET 3.5 project

SSIS 2008 for CRM 2011
 http://a33ik.blogspot.in/2012/02/integrating-crm-2011-using-sql.html

SSIS  2012 for CRM 2011
http://a33ik.blogspot.com/2012/11/integrating-crm-2011-using-sql.html

SSIS for CRM 2013
http://microsoft-ssis.blogspot.com/2014/01/insert-update-and-delete-records-in-crm.html

Connecting to Dynamics CRM 2011 Online (365) from SSIS 2008
http://www.it-gems.com/2013/02/connecting-to-crm-2011-online-365-from.html

http://www.it-gems.com/2013/09/howtocrm2011ssis200801.html


SSIS vs Windows Service:
http://stackoverflow.com/questions/3698618/advantage-of-ssis-package-over-windows-scheduled-exe


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrmProxy.Crm;
using System.ServiceModel.Channels;
using System.ServiceModel.Security.Tokens;
using System.ServiceModel;
using System.Net;
using System.ServiceModel.Security;

namespace CrmProxy
    {
    [System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]
    [System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))]
    [System.Runtime.Serialization.KnownTypeAttribute(typeof(Money))]
    public class CrmHelper
        {
      
        public static IOrganizationService getservice(string url)
            {
            Uri orgURL = new Uri(url);
            SymmetricSecurityBindingElement security = SecurityBindingElement.CreateSspiNegotiationBindingElement();
            security.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Default;
            security.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
            security.IncludeTimestamp = true;
            security.KeyEntropyMode = SecurityKeyEntropyMode.CombinedEntropy;
            security.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncryptAndEncryptSignature;
            security.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
            security.LocalClientSettings.CacheCookies = true;
            security.LocalClientSettings.DetectReplays = true;
            security.LocalClientSettings.ReplayCacheSize = 900000;
            security.LocalClientSettings.MaxClockSkew = new TimeSpan(0, 5, 0);
            security.LocalClientSettings.MaxCookieCachingTime = new TimeSpan(23, 0, 0, 0);
            security.LocalClientSettings.ReplayWindow = new TimeSpan(0, 5, 0);
            security.LocalClientSettings.SessionKeyRenewalInterval = new TimeSpan(15, 0, 0);
            security.LocalClientSettings.SessionKeyRolloverInterval = new TimeSpan(0, 5, 0);
            security.LocalClientSettings.ReconnectTransportOnFailure = true;
            security.LocalClientSettings.TimestampValidityDuration = new TimeSpan(0, 5, 0);
            security.LocalClientSettings.CookieRenewalThresholdPercentage = 60;
            security.LocalServiceSettings.DetectReplays = true;
            security.LocalServiceSettings.IssuedCookieLifetime = new TimeSpan(10, 0, 0);
            security.LocalServiceSettings.MaxStatefulNegotiations = 128;
            security.LocalServiceSettings.ReplayCacheSize = 900000;
            security.LocalServiceSettings.MaxClockSkew = new TimeSpan(0, 5, 0);
            security.LocalServiceSettings.NegotiationTimeout = new TimeSpan(0, 1, 0);
            security.LocalServiceSettings.ReplayWindow = new TimeSpan(0, 5, 0);
            security.LocalServiceSettings.InactivityTimeout = new TimeSpan(0, 2, 0);
            security.LocalServiceSettings.SessionKeyRenewalInterval = new TimeSpan(15, 0, 0);
            security.LocalServiceSettings.SessionKeyRolloverInterval = new TimeSpan(0, 5, 0);
            security.LocalServiceSettings.ReconnectTransportOnFailure = true;
            security.LocalServiceSettings.MaxPendingSessions = 128;
            security.LocalServiceSettings.MaxCachedCookies = 1000;
            security.LocalServiceSettings.TimestampValidityDuration = new TimeSpan(0, 5, 0);

            TextMessageEncodingBindingElement textEncoding = new TextMessageEncodingBindingElement();
            textEncoding.MaxReadPoolSize = 64;
            textEncoding.MaxWritePoolSize = 16;
            textEncoding.MessageVersion = MessageVersion.Default;
            textEncoding.WriteEncoding = System.Text.Encoding.UTF8;



            HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
            httpTransport.ManualAddressing = false;
            httpTransport.MaxBufferSize = 65536;
            httpTransport.MaxReceivedMessageSize = 65536;
            httpTransport.AllowCookies = false;
            httpTransport.AuthenticationScheme = AuthenticationSchemes.Anonymous;
            httpTransport.BypassProxyOnLocal = false;
            httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            httpTransport.KeepAliveEnabled = true;
            httpTransport.MaxBufferPoolSize = 524288;
            httpTransport.ProxyAuthenticationScheme = AuthenticationSchemes.Anonymous;
            httpTransport.TransferMode = TransferMode.Buffered;
            httpTransport.UnsafeConnectionNtlmAuthentication = false;
            httpTransport.UseDefaultWebProxy = true;


            CustomBinding binding = new CustomBinding(new List<BindingElement> { security, textEncoding, httpTransport });
            EndpointAddress endpoint = new EndpointAddress(orgURL);

            OrganizationServiceClient client = new OrganizationServiceClient(binding, endpoint);





            IOrganizationService _orgservice = (IOrganizationService)client;
            return _orgservice;
            }
        }
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CrmProxy.Crm
    {
     [System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]
     [System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))]
     [System.Runtime.Serialization.KnownTypeAttribute(typeof(Money))]

    partial class Entity
        {
        public Entity()
            {
            this.FormattedValuesField = new FormattedValueCollection();
            this.RelatedEntitiesField = new RelatedEntityCollection();
            }

        public T GetAttributeValue<T>(string attributeLogicalName)
            {
            if (null == this.Attributes) { this.Attributes = new AttributeCollection(); };

            object value;
            if (this.Attributes.TryGetValue(attributeLogicalName, out value))
                {
                return (T)value;
                }

            return default(T);
            }

        public object this[string attributeName]
            {
            get
                {
                if (null == this.Attributes) { this.Attributes = new AttributeCollection(); };
                return this.Attributes.GetItem(attributeName);
                }

            set
                {
                if (null == this.Attributes) { this.Attributes = new AttributeCollection(); };
                this.Attributes.SetItem(attributeName, value);
                }
            }
        }

    public static class CollectionExtensions
        {
        public static TValue GetItem<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key)
            {
            TValue value;
            if (TryGetValue(collection, key, out value))
                {
                return value;
                }

            throw new System.Collections.Generic.KeyNotFoundException("Key = " + key);
            }

        public static void SetItem<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key, TValue value)
            {
            int index;
            if (TryGetIndex<TKey, TValue>(collection, key, out index))
                {
                collection.RemoveAt(index);
                }

            //If the value is an array, it needs to be converted into a List. This is due to how Silverlight serializes
            //Arrays and IList<T> objects (they are both serialized with the same namespace). Any collection objects will
            //already add the KnownType for IList<T>, which means that any parameters that are arrays cannot be added
            //as a KnownType (or it will throw an exception).
            Array array = value as Array;
            if (null != array)
                {
                Type listType = typeof(List<>).GetGenericTypeDefinition().MakeGenericType(array.GetType().GetElementType());
                object list = Activator.CreateInstance(listType, array);
                try
                    {
                    value = (TValue)list;
                    }
                catch (InvalidCastException)
                    {
                    //Don't do the conversion because the types are not compatible
                    }
                }

            collection.Add(new KeyValuePair<TKey, TValue>(key, value));
            }

        public static bool ContainsKey<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key)
            {
            int index;
            return TryGetIndex<TKey, TValue>(collection, key, out index);
            }

        public static bool TryGetValue<TKey, TValue>(this IList<KeyValuePair<TKey, TValue>> collection, TKey key, out TValue value)
            {
            int index;
            if (TryGetIndex<TKey, TValue>(collection, key, out index))
                {
                value = collection[index].Value;
                return true;
                }

            value = default(TValue);
            return false;
            }

        private static bool TryGetIndex<TKey, TValue>(IList<KeyValuePair<TKey, TValue>> collection, TKey key, out int index)
            {
            if (null == collection || null == key)
                {
                index = -1;
                return false;
                }

            index = -1;
            for (int i = 0; i < collection.Count; i++)
                {
                if (key.Equals(collection[i].Key))
                    {
                    index = i;
                    return true;
                    }
                }

            return false;
            }
        }

    }

No comments:

Post a Comment