Last active
December 21, 2015 04:09
-
-
Save sixeyed/6247583 to your computer and use it in GitHub Desktop.
Saving a quote with the Dynamics CRM SDK, using a service identity in a back-end process.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.ServiceModel.Description; | |
using x.y.z.ServiceAgents.DynamicsCrm.OrganizationService; | |
using Microsoft.Xrm.Sdk; | |
using Microsoft.Xrm.Sdk.Client; | |
namespace x.y.z.Services.Quotes | |
{ | |
public static class SaveQuote | |
{ | |
public static void Handle() | |
{ | |
var devicecredentials = new ClientCredentials(); | |
devicecredentials.UserName.UserName = "YOUR-Crm.DeviceId"); | |
devicecredentials.UserName.Password = "YOUR-Crm.DevicePassword"); | |
var credentials = new ClientCredentials(); | |
credentials.UserName.UserName = "YOUR-Crm.UserName"); | |
credentials.UserName.Password = "YOUR-Crm.Password"); | |
var organizationUri = new Uri("YOUR-Crm.OrganizationServiceUrl")); | |
using (var serviceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, devicecredentials)) | |
{ | |
//add behavior for early-bound, LINQ entity access: | |
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior()); | |
using (var context = new CrmContext((IOrganizationService)serviceProxy)) | |
{ | |
var account = new Account(); | |
account.Name = "FirstName LastName"; | |
context.AddObject(account); | |
context.SaveChanges(); | |
context.Attach(account); | |
var contact = new Contact(); | |
contact.FirstName = "FirstName"; | |
contact.LastName = "LastName"; | |
contact.BirthDate = DateTime.Today; | |
contact.EMailAddress1 = "[email protected]"; | |
context.AddObject(contact); | |
context.AddLink( | |
contact, | |
new Relationship("contact_customer_accounts"), | |
account); | |
context.AddLink( | |
account, | |
new Relationship("account_primary_contact"), | |
contact); | |
var priceList = context.PriceLevelSet.FirstOrDefault(); | |
var newQuote = new Quote() | |
{ | |
Name = "SDK quote for: " + account.Name, | |
PriceLevelId = priceList.ToEntityReference(), | |
Id = Guid.NewGuid(), | |
CustomerId = account.ToEntityReference() | |
}; | |
context.AddObject(newQuote); | |
context.AddLink( | |
newQuote, | |
new Relationship("quote_customer_accounts"), | |
account); | |
context.SaveChanges(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment