Microsoft CRM
Below is example that illustrates using the service add or load configurations against a Microsoft CRM installation:
public sealed class ConfigurationHelper {
/// <summary>
/// Add a new configuration to an existing CRM entity record.
/// </summary>
public void AddConfig(string entity, Guid id, string seriesId, string modelId) {
using ( var client = getClient() ) {
var header = new ConfigHeader(entity, id.ToString());
var p = new NewModelConfigParameters(header, seriesId, modelId);
var createResult = client.CreateConfigurationAddModel(p);
client.Save();
}
}
/// <summary>
/// Load and resave the configuration associated with an existing CRM entity record.
/// This will ensure that any updated rules, prices, etc. get enforced.
/// </summary>
public void ResaveConfig (string entity, Guid id, int lineItemId) {
using ( var client = getClient() ) {
var header = new ConfigHeader(entity, id.ToString());
var p = new LoadConfigParameters(header, lineItemId);
var loadResult = client.LoadConfiguration(p);
if ( loadResult.Configuration.HasErrors ) {
var fixResult = client.FixConfiguration();
if ( !fixResult.Configuration.HasErrors )
client.SaveConfiguration();
else
client.CancelConfiguration();
}
else {
client.SaveConfiguration();
}
}
}
private WebConfigSvcClient getClient() {
var uri = new Uri("https://server:port/experlogix/services/webconfig.svc");
var x509Cert = getServiceCertificate();
var result = WebConfigSvcClient.Create(uri, x509Cert);
var connectInfo = new MSCrmClientConnectInfo() {
AuthUserName = @"DOMAIN\username",
AuthPassword = "******",
CrmOrganizationName = "OrganizationName",
// assign the CrmUserId to impersonate a different user
// CrmUserId = new Guid("…GUID…"),
};
result.AuthenticateClient(connectInfo);
return result;
}
private X509Certificate2 getServiceCertificate() {
// NOTE: This certificate must be the same certificate that is installed
// on the server and referenced by the service.
const string CertName = "experlogix-webconfigsvc",
StoreName = "Root";
X509Store store = new X509Store(StoreName, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindBySubjectName,
CertName, true);
if ( null == certs || 0 == certs.Count )
throw new ArgumentException(string.Format("Unable to locate service certificate: {0}
(Store: {1}, Location: {2}).", CertName, StoreName, StoreLocation.CurrentUser.ToStringValue()));
return certs[0];
}
}
public sealed class Program {
public static void Main() {
ConfigurationHelper helper = new ConfigurationHelper();
// add a new configuration to the quote using the 'MySeries' series and
// 'MyModel' model.
helper.AddConfig("quote", new Guid("…GUID…"), "MySeries", "MyModel");
}
}
Microsoft AX
Below is the example that illustrates using the service to add or load configurations against a Microsoft AX installation.
public sealed class ConfigurationHelper {
/// <summary>
/// Add a new configuration to an existing CRM entity record.
/// </summary>
public void AddConfig(string entity, Guid id, string seriesId, string modelId) {
using ( var client = getClient() ) {
var header = new ConfigHeader(entity, id.ToString());
var p = new NewModelConfigParameters(header, seriesId, modelId);
var createResult = client.CreateConfigurationAddModel(p);
client.Save();
}
}
/// <summary>
/// Load and resave the configuration associated with an existing CRM entity record.
/// This will ensure that any updated rules, prices, etc. get enforced.
/// </summary>
public void ResaveConfig (string entity, Guid id, int lineItemId) {
using ( var client = getClient() ) {
var header = new ConfigHeader(entity, id.ToString());
var p = new LoadConfigParameters(header, lineItemId);
var loadResult = client.LoadConfiguration(p);
if ( loadResult.Configuration.HasErrors ) {
var fixResult = client.FixConfiguration();
if ( !fixResult.Configuration.HasErrors )
client.SaveConfiguration();
else
client.CancelConfiguration();
}
else {
client.SaveConfiguration();
}
}
}
private WebConfigSvcClient getClient() {
var uri = new Uri("https://server:port/experlogix/services/webconfig.svc");
var x509Cert = getServiceCertificate();
var result = WebConfigSvcClient.Create(uri, x509Cert);
var connectInfo = new MSAxClientConnectInfo() {
AuthUserName = @"DOMAIN\username",
AuthPassword = "******",
AxCompanyName = "CompanyName",
};
result.AuthenticateClient(connectInfo);
return result;
}
private X509Certificate2 getServiceCertificate() {
// NOTE: This certificate must be the same certificate that is installed
// on the server and referenced by the service.
const string CertName = "experlogix-webconfigsvc",
StoreName = "Root";
X509Store store = new X509Store(StoreName, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindBySubjectName,
CertName, true);
if ( null == certs || 0 == certs.Count )
throw new ArgumentException(string.Format("Unable to locate service certificate: {0}
(Store: {1}, Location: {2}).", CertName, StoreName, StoreLocation.CurrentUser.ToStringValue()));
return certs[0];
}
}
public sealed class Program {
public static void Main() {
ConfigurationHelper helper = new ConfigurationHelper();
// add a new configuration to the quote using the 'MySeries' series and
// 'MyModel' model.
helper.AddConfig("SalesQuotation", "QuoteId", "MySeries", "MyModel");
}
}
Custom
If you decide to create your own service client (either via Visual Studio’s Service Reference or svcutil), you will need to do some additional work. As the service relies on ASP.NET session state, you must set the proper HTTP headers for each request.
The following example demonstrates a simple client that uses the Experlogix Contracts DLL:
public sealed class ConfigurationHelper {
/// <summary>
/// Add a new configuration to an existing CRM entity record.
/// </summary>
public void AddConfig(string entity, Guid id, string seriesId, string modelId) {
using ( var client = getClient() ) {
var header = new ConfigHeader() {
Type = entity,
Id = id.ToString(),
};
var p = new NewModelConfigParameters() {
Header = header,
SeriesId = seriesId,
ModelId = modelId,
};
var createResult = client.CreateConfigurationFromModel(p);
client.Save();
}
}
/// <summary>
/// Load and resave the configuration associated with an existing CRM entity record.
/// This will ensure that any updated rules, prices, etc. get enforced.
/// </summary>
public void ResaveConfig (string entity, Guid id, int lineItemId) {
using ( var client = getClient() ) {
var header = new ConfigHeader() {
Type = entity,
Id = id.ToString(),
};
var p = new LoadConfigParameters() {
Header = header,
LineItemId = lineItemId,
};
var loadResult = client.LoadConfiguration(p);
if ( loadResult.Configuration.HasErrors ) {
var fixResult = client.FixConfiguration();
if ( !fixResult.Configuration.HasErrors )
client.SaveConfiguration();
else
client.CancelConfiguration();
}
else {
client.SaveConfiguration();
}
}
}
private WebConfigClient getClient() {
var result = new WebConfigClient();
result.ClientCredentials.ClientCertificate.Certificate = getServiceCertificate();
var connectInfo = new MSCrmClientConnectInfo() {
AuthUserName = @"DOMAIN\username",
AuthPassword = "******",
CrmOrganizationName = "OrganizationName",
// assign the CrmUserId to impersonate a different user
// CrmUserId = new Guid("…GUID…"),
};
result.AuthenticateClient(connectInfo);
return result;
}
private X509Certificate2 getServiceCertificate() {
// NOTE: This certificate must be the same certificate that is installed
// on the server and referenced by the service.
const string CertName = "experlogix-webconfigsvc",
StoreName = "Root";
X509Store store = new X509Store(StoreName, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindBySubjectName,
CertName, true);
if ( null == certs || 0 == certs.Count )
throw new ArgumentException(string.Format("Unable to locate service certificate: {0}
(Store: {1}, Location: {2}).", CertName, StoreName, StoreLocation.CurrentUser.ToStringValue()));
return certs[0];
}
}