using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
namespace Company.Plugin.LogicalRepository
{
public class Account:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//获取执行上下文
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//获取服务工厂实例
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
//获取管理员组织服务实例
IOrganizationService serviceAdmin = factory.CreateOrganizationService(null);// 该情况获取只针对于 沙盒模式注册的插件有效
if (serviceAdmin is OrganizationServiceProxy proxy)// 非沙盒模式 添加
{
proxy.CallerId = new Guid();
}
//获取当前个人组织服务实例
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
// 获取调试服务
ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// 获取组织服务上下文
OrganizationServiceContext orgContext = new OrganizationServiceContext(serviceAdmin);
}
}
}
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using Microsoft.Xrm.Sdk.Query;
namespace Company.WorkFlow.Opportunity
{
public class OpportunityWarn : CodeActivity
{
// 输入参数 (可省)
[Input("userid")]
public InArgument userid { get; set; }
[Input("accountid")]
public InArgument yzid { get; set; }
// 输出参数(可省)
[Output("flag")]
public OutArgument flag { get; set; }
protected override void Execute(CodeActivityContext context)
{
// 获取服务
IWorkflowContext workcontext = context.GetExtension();
IOrganizationServiceFactory factory = context.GetExtension();
IOrganizationService serviceAdmin = factory.CreateOrganizationService(null);
IOrganizationService service = factory.CreateOrganizationService(workcontext.UserId);
try
{
// 获取输入参数
var userId = userid.Get(context).ToLower();
var accountId = accountid.Get(context);
bool output = true;
// 设置输出参数
flag.Set(context, output);
}
catch(Exception ex)
{
throw new NotImplementedException(ex.Message);
}
}
}
}
https://xxxx.xxxx.cn
组织名
域名
部署管理员账号
密码
AD
Data Source=具体IP地址;Initial Catalog=数据库名称;User ID=登录账号;Pwd=密码;
日志记录路径
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace Company.Crm.Helper
{
public class CRMConfigHelper
{
public static CRMConfig InitialCRMConfig(string crmOrganizationName)
{
CRMConfig crmConfig = new CRMConfig();
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:\\CRMConfig\\" + crmOrganizationName + "Config.xml");
XmlNode ConfigNode = xmlDoc.SelectSingleNode("/CRMConfig");
string crmServiceUrl = string.Empty;
if (ConfigNode["CRMServiceUrl"] != null)
{
crmServiceUrl = ConfigNode["CRMServiceUrl"].InnerText.Trim();
if (crmServiceUrl.EndsWith("/"))
{
crmServiceUrl = crmServiceUrl.Substring(0, crmServiceUrl.Length - 1);
}
}
crmConfig.CRMServiceUrl = crmServiceUrl;
crmConfig.CRMOrganizationName = ConfigNode["CRMOrganizationName"] != null ? ConfigNode["CRMOrganizationName"].InnerText.Trim() : string.Empty;
crmConfig.CRMUserDomainName = ConfigNode["CRMUserDomainName"] != null ? ConfigNode["CRMUserDomainName"].InnerText.Trim() : string.Empty;
crmConfig.CRMUserName = ConfigNode["CRMUserName"] != null ? ConfigNode["CRMUserName"].InnerText.Trim() : string.Empty;
crmConfig.CRMUserPassword = ConfigNode["CRMUserPassword"] != null ? ConfigNode["CRMUserPassword"].InnerText.Trim() : string.Empty;
crmConfig.CRMAuthenticationType = ConfigNode["CRMAuthenticationType"] != null ? ConfigNode["CRMAuthenticationType"].InnerText.Trim() : string.Empty;
crmConfig.CRMSqlConnStr = ConfigNode["CRMSqlConnStr"] != null ? ConfigNode["CRMSqlConnStr"].InnerText.Trim() : string.Empty;
crmConfig.LogPath = ConfigNode["LogPath"] != null ? ConfigNode["LogPath"].InnerText.Trim() : string.Empty;
}
catch (Exception ex)
{
throw new Exception("Initial CRM Config Error : " + ex.Message);
}
return crmConfig;
}
[Serializable]
public class CRMConfig
{
private string crmServiceUrl;
private string crmOrganizationName;
private string crmUserDomainName;
private string crmUserName;
private string crmUserPassword;
private string crmAuthenticationType;
private string crmSqlConnStr;
private string logPath;
public string CRMServiceUrl
{
get { return crmServiceUrl; }
set { crmServiceUrl = value; }
}
public string CRMOrganizationName
{
get { return crmOrganizationName; }
set { crmOrganizationName = value; }
}
public string CRMUserDomainName
{
get { return crmUserDomainName; }
set { crmUserDomainName = value; }
}
public string CRMUserName
{
get { return crmUserName; }
set { crmUserName = value; }
}
public string CRMUserPassword
{
get { return crmUserPassword; }
set { crmUserPassword = value; }
}
public string CRMAuthenticationType
{
get { return crmAuthenticationType; }
set { crmAuthenticationType = value; }
}
public string CRMSqlConnStr
{
get { return crmSqlConnStr; }
set { crmSqlConnStr = value; }
}
public string LogPath
{
get { return logPath; }
set { logPath = value; }
}
}
}
}
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
namespace Company.Crm.Helper
{
public class CRMServiceHelper
{
public static IOrganizationService InitialCRMService(string crmServiceUrl, string crmOrganizationName, string crmAuthenticationType, string crmUserName, string crmUserPassword, string crmUserDomainName)
{
IOrganizationService crmService = null;
try
{
Uri orgServiceUri = new Uri(crmServiceUrl + "/" + crmOrganizationName + "/XRMServices/2011/Organization.svc");
ClientCredentials credentials = new ClientCredentials();
if (crmAuthenticationType == "AD")
{
credentials.Windows.ClientCredential = new System.Net.NetworkCredential(crmUserName, crmUserPassword, crmUserDomainName);
}
else if (crmAuthenticationType == "ADFS")
{
credentials.UserName.UserName = crmUserDomainName + "\\" + crmUserName;
credentials.UserName.Password = crmUserPassword;
}
OrganizationServiceProxy crmServiceProxy = new OrganizationServiceProxy(orgServiceUri, null, credentials, null);
crmService = (IOrganizationService)crmServiceProxy;
}
catch (Exception ex)
{
throw new Exception("Initial CRM Service Error : " + ex.Message);
}
return crmService;
}
}
}
// 获取CRM组织的 Config
CRMConfigHelper.CRMConfig crmConfig = CRMConfigHelper.InitialCRMConfig(crmOrganizationName);
// 初始化CRM的WebService
IOrganizationService crmService = CRMServiceHelper.InitialCRMService(crmConfig.CRMServiceUrl, crmConfig.CRMOrganizationName,
crmConfig.CRMAuthenticationType, crmConfig.CRMUserName, crmConfig.CRMUserPassword, crmConfig.CRMUserDomainName);
在web.config中的configuration中添加如下
考虑到安全性,可对配置的属性进行加密,解密在获取相关配置实例化服务的时候
private readonly static string Domain = EasyEncript.Decrypt(ConfigurationManager.AppSettings["Domain"], EasyEncript.PwdKey);// ConfigurationManager.AppSettings["Domain"];
private readonly static string UserName = EasyEncript.Decrypt(ConfigurationManager.AppSettings["UserName"], EasyEncript.PwdKey);//ConfigurationManager.AppSettings["UserName"];
private readonly static string PassWord = EasyEncript.Decrypt(ConfigurationManager.AppSettings["PassWord"], EasyEncript.PwdKey);// ConfigurationManager.AppSettings["PassWord"];
private static string baseUrl = ConfigurationManager.AppSettings["OrgUrl"];
private readonly static string OrganizationUri = EasyEncript.Decrypt(baseUrl, EasyEncript.PwdKey); // ConfigurationManager.AppSettings["OrgUrl"];
private readonly static string IFD = EasyEncript.Decrypt(ConfigurationManager.AppSettings["IFD"], EasyEncript.PwdKey);//ConfigurationManager.AppSettings["IFD"];
其中 EasyEncript.Decrypt 是解密的函数 ; EasyEncript.PwdKey 是密钥
public static OrganizationServiceProxy GetService(string OURL, string UserName, string PassWord, string Domain)
{
OrganizationServiceProxy organizationServiceProxy = null;
try
{
if (organizationServiceProxy == null)
{
ClientCredentials clientCredentials;
clientCredentials = new ClientCredentials
{
UserName = { UserName = UserName, Password = PassWord }
//Windows = { ClientCredential = new NetworkCredential(UserName, PassWord, Domain) }
};
organizationServiceProxy = new OrganizationServiceProxy(new Uri(OURL), null, clientCredentials, null);
}
return organizationServiceProxy;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private static OrganizationServiceProxy organizationServiceProxy = null;
private static OrganizationServiceProxy organizationServiceForuser = null;
///
/// 管理员组织服务
///
///
public static OrganizationServiceProxy OrganizationServiceAdmin()
{
return CrmService.GetService(OrganizationUri, UserName, PassWord, Domain);
}
///
/// 个人组织服务
///
///
///
public static OrganizationServiceProxy OrganizationServiceForUser(Guid _userId)
{
organizationServiceForuser = GetService(OrganizationUri, UserName, PassWord, Domain);
organizationServiceForuser.CallerId = _userId;
organizationServiceForuser.CallerRegardingObjectId = _userId;
return organizationServiceForuser;
}