Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using CrmEarlyBound;
- using Microsoft.Crm.Sdk.Messages;
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Sdk.Query;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Test_Console.Services;
- using Test_Console.Extensions;
- using System.IO;
- using Microsoft.Xrm.Sdk.Messages;
- using Test_Console.Enums;
- using Test_Console.Data;
- using System.ServiceModel.Description;
- using System.Runtime.Caching;
- using Microsoft.Xrm.Sdk.Client;
- using System.Net;
- namespace Test_Console
- {
- class Program
- {
- static void Main(string[] args)
- {
- var service = InitializeService(true, 300);
- var e = new dk_kleshchovsentity();
- e["dk_name"] = "Обновило 4";
- //var myEntity = new dk_kleshchovsentity();
- //myEntity.KeyAttributes = new KeyAttributeCollection();
- //myEntity.KeyAttributes.Add("dk_dwhkey", "123456");
- //myEntity.dk_name = "Обновило 3";
- using (var context = new CrmServiceContext(service))
- {
- var mySet = (from c in context.dk_kleshchovsentitySet
- where c.dk_dwhkey == "123456"
- select new dk_kleshchovsentity() { Id = c.Id, dk_name = c.dk_name }).ToList();
- }
- var query = new QueryExpression("dk_kleshchovsentity")
- {
- ColumnSet = new ColumnSet("dk_name"),
- Criteria =
- {
- Conditions =
- {
- new ConditionExpression("dk_dwhkey", ConditionOperator.Equal, "123456")
- }
- }
- };
- var result = service.RetrieveMultiple(query);
- }
- static OptionSetValue GetIncomeType(string val)
- {
- switch (val)
- {
- case ("Щомісячний"):
- case ("Ежемесячный"):
- return new OptionSetValue((int)IncomeTypes.Monthly);
- default:
- return null;
- }
- }
- private static OrganizationServiceProxy InitializeService(Boolean impersonate, int timeout, Boolean earlybound = true, Boolean isWeb = false, double absoluteExpiration = 600)
- {
- ClientCredentials credentials = ConfigurationSettings.GetCredentialData(impersonate);
- String url = ConfigurationSettings.GetServiceUri();
- OrganizationServiceProxy service = null;
- if (isWeb)
- {
- var cache = MemoryCache.Default;
- IServiceManagement<IOrganizationService> orgServiceManagement = null;
- if (cache.Get("orgServiceManagement") == null)
- {
- var cachePolicty = new CacheItemPolicy();
- cachePolicty.AbsoluteExpiration = DateTime.Now.AddMinutes(absoluteExpiration);
- orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(url));
- cache.Add("orgServiceManagement", orgServiceManagement, cachePolicty);
- }
- else
- {
- orgServiceManagement = cache.Get("orgServiceManagement") as IServiceManagement<IOrganizationService>;
- }
- if (orgServiceManagement != null)
- {
- service = new OrganizationServiceProxy(orgServiceManagement, credentials);
- }
- else
- {
- service = new OrganizationServiceProxy(new Uri(url), null, credentials, null);
- }
- }
- else
- {
- service = new OrganizationServiceProxy(new Uri(url), null, credentials, null);
- }
- if (earlybound)
- {
- service.EnableProxyTypes();
- }
- if (timeout > 0)
- {
- service.Timeout = new TimeSpan(0, timeout, 0);
- }
- return service;
- }
- }
- public class ConfigurationSettings
- {
- public static String GetServiceUri()
- {
- String serviceUri = GetConfigurationValue("serviceUri");
- if (String.IsNullOrEmpty(serviceUri))
- {
- throw new Exception("Invalid CRM URI.");
- }
- return serviceUri;
- }
- public static ClientCredentials GetCredentialData(Boolean impersonate)
- {
- ClientCredentials credentials = new ClientCredentials();
- if (!impersonate)
- {
- String userName = GetConfigurationValue("userName");
- String userPassword = GetConfigurationValue("userPassword");
- String userDomain = GetConfigurationValue("userDomain");
- if (String.IsNullOrEmpty(userName) || String.IsNullOrEmpty(userPassword) || String.IsNullOrEmpty(userDomain))
- {
- credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
- }
- else
- {
- String pwd = "";//Encryption.Decrypt(userPassword);
- credentials.Windows.ClientCredential = new NetworkCredential(userName, pwd, userDomain);
- }
- }
- else
- {
- credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
- }
- return credentials;
- }
- private static String GetConfigurationValue(String key)
- {
- if (ConfigurationManager.AppSettings != null && ConfigurationManager.AppSettings[key] != null)
- {
- return ConfigurationManager.AppSettings[key];
- }
- return null;
- }
- public static Boolean GetTraceEnable()
- {
- if (ConfigurationManager.AppSettings != null && ConfigurationManager.AppSettings["traceEnable"] != null)
- {
- return bool.Parse(ConfigurationManager.AppSettings["traceEnable"]);
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement