Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.37 KB | None | 0 0
  1. using CrmEarlyBound;
  2. using Microsoft.Crm.Sdk.Messages;
  3. using Microsoft.Xrm.Sdk;
  4. using Microsoft.Xrm.Sdk.Query;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Test_Console.Services;
  12. using Test_Console.Extensions;
  13. using System.IO;
  14. using Microsoft.Xrm.Sdk.Messages;
  15. using Test_Console.Enums;
  16. using Test_Console.Data;
  17. using System.ServiceModel.Description;
  18. using System.Runtime.Caching;
  19. using Microsoft.Xrm.Sdk.Client;
  20. using System.Net;
  21.  
  22. namespace Test_Console
  23. {
  24.     class Program
  25.     {
  26.         static void Main(string[] args)
  27.         {
  28.             var service = InitializeService(true, 300);
  29.             var e = new dk_kleshchovsentity();
  30.             e["dk_name"] = "Обновило 4";
  31.            
  32.             //var myEntity = new dk_kleshchovsentity();
  33.             //myEntity.KeyAttributes = new KeyAttributeCollection();
  34.             //myEntity.KeyAttributes.Add("dk_dwhkey", "123456");
  35.             //myEntity.dk_name = "Обновило 3";
  36.  
  37.             using (var context  = new CrmServiceContext(service))
  38.             {
  39.                 var mySet = (from c in context.dk_kleshchovsentitySet
  40.                              where c.dk_dwhkey == "123456"
  41.                              select new dk_kleshchovsentity() { Id = c.Id, dk_name = c.dk_name }).ToList();
  42.             }
  43.  
  44.             var query = new QueryExpression("dk_kleshchovsentity")
  45.             {
  46.                 ColumnSet = new ColumnSet("dk_name"),
  47.                 Criteria =
  48.                 {
  49.                     Conditions =
  50.                     {
  51.                         new ConditionExpression("dk_dwhkey", ConditionOperator.Equal, "123456")
  52.                     }
  53.                 }
  54.             };
  55.  
  56.             var result = service.RetrieveMultiple(query);
  57.            
  58.  
  59.            
  60.         }
  61.  
  62.  
  63.         static OptionSetValue GetIncomeType(string val)
  64.         {
  65.             switch (val)
  66.             {
  67.                 case ("Щомісячний"):
  68.                 case ("Ежемесячный"):
  69.                     return new OptionSetValue((int)IncomeTypes.Monthly);
  70.                 default:
  71.                     return null;
  72.             }
  73.         }
  74.  
  75.  
  76.  
  77.  
  78.         private static OrganizationServiceProxy InitializeService(Boolean impersonate, int timeout, Boolean earlybound = true, Boolean isWeb = false, double absoluteExpiration = 600)
  79.         {
  80.             ClientCredentials credentials = ConfigurationSettings.GetCredentialData(impersonate);
  81.             String url = ConfigurationSettings.GetServiceUri();
  82.             OrganizationServiceProxy service = null;
  83.             if (isWeb)
  84.             {
  85.                 var cache = MemoryCache.Default;
  86.                 IServiceManagement<IOrganizationService> orgServiceManagement = null;
  87.  
  88.                 if (cache.Get("orgServiceManagement") == null)
  89.                 {
  90.                     var cachePolicty = new CacheItemPolicy();
  91.                     cachePolicty.AbsoluteExpiration = DateTime.Now.AddMinutes(absoluteExpiration);
  92.  
  93.                     orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(url));
  94.  
  95.                     cache.Add("orgServiceManagement", orgServiceManagement, cachePolicty);
  96.                 }
  97.                 else
  98.                 {
  99.                     orgServiceManagement = cache.Get("orgServiceManagement") as IServiceManagement<IOrganizationService>;
  100.                 }
  101.  
  102.                 if (orgServiceManagement != null)
  103.                 {
  104.                     service = new OrganizationServiceProxy(orgServiceManagement, credentials);
  105.                 }
  106.                 else
  107.                 {
  108.                     service = new OrganizationServiceProxy(new Uri(url), null, credentials, null);
  109.                 }
  110.             }
  111.             else
  112.             {
  113.                 service = new OrganizationServiceProxy(new Uri(url), null, credentials, null);
  114.             }
  115.  
  116.             if (earlybound)
  117.             {
  118.                 service.EnableProxyTypes();
  119.             }
  120.  
  121.             if (timeout > 0)
  122.             {
  123.                 service.Timeout = new TimeSpan(0, timeout, 0);
  124.             }
  125.  
  126.             return service;
  127.         }
  128.     }
  129.  
  130.     public class ConfigurationSettings
  131.     {
  132.         public static String GetServiceUri()
  133.         {
  134.             String serviceUri = GetConfigurationValue("serviceUri");
  135.  
  136.             if (String.IsNullOrEmpty(serviceUri))
  137.             {
  138.                 throw new Exception("Invalid CRM URI.");
  139.             }
  140.  
  141.             return serviceUri;
  142.         }
  143.  
  144.         public static ClientCredentials GetCredentialData(Boolean impersonate)
  145.         {
  146.             ClientCredentials credentials = new ClientCredentials();
  147.  
  148.             if (!impersonate)
  149.             {
  150.                 String userName = GetConfigurationValue("userName");
  151.                 String userPassword = GetConfigurationValue("userPassword");
  152.                 String userDomain = GetConfigurationValue("userDomain");
  153.  
  154.                 if (String.IsNullOrEmpty(userName) || String.IsNullOrEmpty(userPassword) || String.IsNullOrEmpty(userDomain))
  155.                 {
  156.                     credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
  157.                 }
  158.                 else
  159.                 {
  160.                     String pwd = "";//Encryption.Decrypt(userPassword);
  161.                     credentials.Windows.ClientCredential = new NetworkCredential(userName, pwd, userDomain);
  162.                 }
  163.             }
  164.             else
  165.             {
  166.                 credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
  167.             }
  168.  
  169.             return credentials;
  170.         }
  171.  
  172.         private static String GetConfigurationValue(String key)
  173.         {
  174.             if (ConfigurationManager.AppSettings != null && ConfigurationManager.AppSettings[key] != null)
  175.             {
  176.                 return ConfigurationManager.AppSettings[key];
  177.             }
  178.  
  179.             return null;
  180.         }
  181.  
  182.         public static Boolean GetTraceEnable()
  183.         {
  184.             if (ConfigurationManager.AppSettings != null && ConfigurationManager.AppSettings["traceEnable"] != null)
  185.             {
  186.                 return bool.Parse(ConfigurationManager.AppSettings["traceEnable"]);
  187.             }
  188.  
  189.             return false;
  190.         }
  191.  
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement