26. Form Level 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Property CRM 4.0 CRM 2011 Organization Name ORG_UNIQUE_NAME Xrm.Page.context.getOrgUniqueName() UserID By making service request using SOAP XML Xrm.Page.context. getUserId() UserLcid (Language ID) By making service request using SOAP XML Xrm.Page.context. getUserLcid() UserRoles By making service request using SOAP XML Xrm.Page.context. getUserRoles() Record ID crmForm.ObjectId Xrm.Page.data.entity.getId() Entity Name crmForm.ObjectTypeName Xrm.Page.data.entity.getEntityName() IsDirty crmForm.IsDirty Xrm.Page.data.entity.getIsDirty() getDataXml N/A Xrm.Page.data.entity.getDataXml() Add function onSave crmForm.attachEvent("onsave", saveCommissions); Xrm.Page.data.entity.addOnSave(saveCommissions) Remove function onSave N/A Xrm.Page.data.entity.removeOnSave(savecommissions) Save crmForm.submit() Xrm.Page.data.entity.save() Xrm.Page.data.entity.save(“saveandclose”) Xrm.Page.data.entity.save(“saveandnew”)
28. Attributes methods (1) 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Property CRM 4.0 CRM 2011 Attaching on change event crmForm.all.telephone1.attachEvent("onchange", FormatPhone); crmForm.all. telephone1.FireOnChange() var attributeObj = Xrm.Page.data.entity.attributes.get(‘telephone1’); attributeObj.addOnChange(FormatPhone); attributeObj.fireOnChange(); attributeObj.removeOnChange(tempFunc); Attribute Type N/A attributeObj.getAttributeType() Attribute Format N/A attributeObj.getFormat() Default Value for Option set and Boolean attribute N/A attributeObj.getInitialValue() Is Dirty crmForm.all.telephone1.IsDirty attributeObj.getIsDirty() Maximum allowed value attributeObj.getMax() Minimum allowed value attributeObj.getMin() Maximum allowed length attributeObj.getMaxLength()
29. Attributes methods (2) 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Property CRM 4.0 CRM 2011 Logical Name attributeObj. getName() Get Option name attributeObj.getOption(value) Get options attributeObj.getOptions() Digits allowed to the right of the decimal point attributeObj.getPrecision() Get required level attributeObj.getRequiredLevel() Set Required Level attributes[i].setRequiredLevel("required"); Get selected Option var option = attributeObj.getSelectedOption() Option.text Option.value attributeObj.getText() Submit Mode attributeObj.getSubmitMode() Returns: Never, Always, Dirty
30. Attributes methods (3) 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Property CRM 4.0 CRM 2011 Set Submit mode attributeObj.setSubmitMode("always"); Get user privilege N/A var privileges = attribute.getUserPrivilege(); privileges.canRead privileges.canUpdate privileges.canCreate Get attribute value var value = attribute.getValue(); For Lookups value[i].entityType value[i].id value[i].name For Option sets Value
31. Page UI 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Property CRM 4.0 CRM 2011 Form Type CrmForm.FormType Xrm.Page.ui.getFormType() 0-Undefined 1-Create 2-Update 3 -Read Only 4-Disabled 5-Quick Create (Deprecated) 6-Bulk Edit Close the form crmForm.close() Xrm.Page.ui.close() Get current control which is having focus N/A Xrm.Page.ui.getCurrentControl() Refresh the ribbon N/A Xrm.Page.ui.refreshRibbon() Gets the controls on the page N/A Xrm.Page.ui.controls.get() Xrm.Page.ui.controls.forEach(delegate function(control, index)) Xrm.Page.ui.controls.getLength ()
32. Page UI Control methods 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Property CRM 4.0 CRM 2011 Add custom view for Lookup dialog N/A controlObj.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault) Add an Option to Option set N/A controlObj.addOption(option, [index]) Clear options N/A controlObj.clearOptions() Get the attribute that the control bound to N/A controlObj.getAttribute() Get the control type N/A controlObj.getControlType() Returns Standard, iframe, lookup, optionset, subgrid, webresource Gets default lookup view N/A controlObj.getDefaultView() Check the control is disabled N/A controlObj.getDisabled() Disable control N/A control.setDisabled(false) Gets the URL of Iframe N/A IFrameControlObj.getSrc()
33. Page UI Control methods 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Property CRM 4.0 CRM 2011 Making control visible or invisible N/A controlObj.getVisible() control.setVisible() Refresh sub grid crmForm.all.PosDataGrid.Refresh() controlObj.refresh()
34. Intellisence for Java Script 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani To enable Intellisence Visual Studio needs a reference to the file XrmPage-vsdoc.js /// <reference path="XrmPage-vsdoc.js" />
35. Image web resource 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani
36. Image web resource 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani
40. Late-bound 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Entity account = new Entity ( "account" ); account[ "name" ] = "Fourth Coffee" ; Entity is the base class which is part of the Xrm.Sdk namespace Microsoft.Crm.Sdk. DynamicEntity (CRM 4.0) Microsoft.Xrm.Sdk. Entity (CRM 2011)
41. Assemblies 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Assembly name Namespace name Microsoft.Crm.Sdk.Proxy.dll Microsoft.Crm.Sdk Microsoft.Crm.Sdk.Messages Microsoft.Xrm.Sdk.dll Microsoft.Xrm.Sdk Microsoft.Xrm.Sdk.Client Microsoft.Xrm.Sdk.Discovery Microsoft.Xrm.Sdk.Messages Microsoft.Xrm.Sdk.Metadata Microsoft.Xrm.Sdk.Query
42. Assemblies 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Assembly name Namespace name Microsoft.Xrm.Sdk.Workflow.dll Microsoft.Xrm.Sdk.Workflow Microsoft.Xrm.Sdk.Workflow.Activities Microsoft.Xrm.Sdk.Workflow.Designers Microsoft.Crm.Tools.EmailProviders.dll Microsoft.Crm.Tools.Email.Management Microsoft.Crm.Tools.Email.Providers Microsoft.Xrm.Sdk.Deployment.dll Microsoft.Xrm.Sdk.Deployment Microsoft.Xrm.Sdk.Deployment.Proxy
43. Type Mapping 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Attribute Type CRM 2011 Type CRM 4.0 Type AttributeTypeCode .Boolean bool or System.Boolean CrmBoolean AttributeType . CalendarRules EntityCollection DynamicEntity [] or calendarrule [] AttributeType. DateTime System.DateTime CrmDateTime AttributeType. Decimal decimal or System.Decimal CrmDecimal AttributeType. Double double or System.Double CrmFloat AttributeType. Integer int or System.Integer CrmNumber AttributeType. Lookup EntityReference Lookup AttributeType. Memo string or System.String System.String
44. Type Mapping 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Attribute Type CRM 2011 Type CRM 4.0 Type AttributeType. Money Money CrmMoney AttributeType. Owner EntityReference Owner AttributeType. PartyList EntityCollection or ActivityParty[] activityparty[] or DynamicEntity [] AttributeType. Picklist OptionSetValue Picklist AttributeType.PrimaryKey System.Guid Key AttributeType. String System.String System.String AttributeType. State OptionSetValue or enumeration generated for the entity state EntityNameStateInfo AttributeType. Status OptionSetValue or int Status AttributeType. Uniqueidentifier System.Guid UniqueIdentifier
45. New Entity Data Model 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Feature CRM 4.0 CRM 2011 Set property value to null account entity; entity.industrycode = null; Account entity; entity.Remove( "industrycode" ); Check for a NULL value account entity; if ( null != entity.industrycode && entity.industrycode.IsNull) { //This attribute is null } Account entity; if (entity.Contains( "industrycode" ) && null == entity[ "industrycode" ]) { //This attribute is null } Set NULL value Account entity; entity.IndustryCode = null ; account entity; entity.industrycode = new Picklist(); entity.industrycode.IsNull = true ; Entity and Attribute names contact newEntity = new contact(); newEntity.contactid = new Key(Guid.NewGuid()); newEntity.firstname = "Michael" ; Contact newEntity = new Contact(); newEntity.ContactId = Guid.NewGuid(); newEntity.FirstName = "Michael" ; Entity name and Type code int objectTypeCode = ( int )EntityName.account; string entityName = EntityName.account.ToString(); int objectTypeCode = Account.EntityTypeCode; string entityName = Account.EntityLogicalName;
46. Using CRM Services in Code 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani IDiscoveryService Web Service IOrganizationService Web Service using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Discovery; private DiscoveryServiceProxy _serviceProxy; //Initializing the object with global variables data from Config file using (_serviceProxy = new DiscoveryServiceProxy( DiscoveryUri, HomeRealmUri, Credentials, DeviceCredentials)) { IDiscoveryService service = _serviceProxy as IDiscoveryService; // Retrieve details about all organizations discoverable via the // Discovery service. RetrieveOrganizationsRequest orgsRequest = new RetrieveOrganizationsRequest() { AccessType = EndpointAccessType.Default, Release = OrganizationRelease.Current }; RetrieveOrganizationsResponse organizations = (RetrieveOrganizationsResponse)service.Execute(orgsRequest); }
47. Using CRM Services in Code 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani IOrganizationService Web Service using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; private IOrganizationService MyService; public IOrganizationService InitializeService() { System.ServiceModel.Description. ClientCredentials credentials = new System.ServiceModel.Description. ClientCredentials (); credentials.Windows.ClientCredential = System.Net. CredentialCache .DefaultNetworkCredentials; using ( OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy ( new Uri (_OrganizationService), null , credentials, null )) { _serviceProxy.Authenticate(); MyService = ( IOrganizationService )_serviceProxy; } return MyService; } public Guid Create( Entity Entity) { try { return MyService.Create(Entity); } catch ( Exception ex) { throw ex; } }
48. Create 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Entity primaryContact = new Entity ( "contact" ); primaryContact.Attributes[ "firstname" ] = txtFirstName.Text; primaryContact.Attributes[ "lastname" ] = txtLastName.Text; primaryContact.Attributes[ "address1_line1" ] = txtAddress.Text; primaryContact.Attributes[ "address1_city" ] = txtCity.Text; primaryContact.Attributes[ "address1_stateorprovince" ] = ddlState.SelectedItem.Text; primaryContact.Attributes[ "address1_postalcode" ] = txtZip.Text; primaryContact.Attributes[ "emailaddress1" ] = txtEmail.Text; primaryContact.Attributes[ "preferredcontactmethodcode" ] = new OptionSetValue ( Convert .ToInt32(ddlPrefrredContactMethod.SelectedValue)); primaryContact.Attributes[ "preferredappointmentdaycode" ] = new OptionSetValue ( Convert .ToInt32(ddlPreferredDay.SelectedValue)); primaryContact.Attributes[ "preferredappointmenttimecode" ] = new OptionSetValue ( Convert .ToInt32(ddlPreferredTime.SelectedValue)); Common common = new Common (); return common.Create(primaryContact); using Microsoft.Xrm.Sdk; Entity Opp = new Entity ( "opportunity" ); Opp.Attributes[ "name" ] = txtLastName.Text + " " + txtFirstName.Text; Opp.Attributes[ "customerid" ] = new EntityReference ( "contact" , PrimaryContactID); Common common = new Common (); return common.Create(Opp);
49. Retrieve 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Entity account = new Entity ( "account" ); account[ "name" ] = “Sample Account" ; _accountId = _service.Create(account); // Create a column set to define which attributes should be retrieved. ColumnSet attributes = new ColumnSet ( new string [] { "name" , "ownerid" }); // Retrieve the account and its name and ownerid attributes. account = _service.Retrieve(account.LogicalName, _accountId, attributes); using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query;
50. Update 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani // Retrieve the account containing several of its attributes. ColumnSet cols = new ColumnSet ( new String [] { "name" , "address1_postalcode" , "lastusedincampaign" }); Account retrievedAccount = ( Account )_service.Retrieve( "account" , _accountId, cols); Console .Write( "retrieved, " ); // Update the postal code attribute. retrievedAccount.Address1_PostalCode = "98052" ; // The address 2 postal code was set accidentally, so set it to null. retrievedAccount.Address2_PostalCode = null ; // Shows use of a Money value. retrievedAccount.Revenue = new Money (5000000); // Shows use of a boolean value. retrievedAccount.CreditOnHold = false ; // Update the account record. _service.Update(retrievedAccount); using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query;
51. Retrieve Multiple Query Expression 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani //Find all Opportunities with estimated close date in next 3 years query = new QueryExpression () { EntityName = Opportunity .EntityLogicalName, ColumnSet = new ColumnSet ( "name" ), Criteria = { Conditions = { new ConditionExpression ( "estimatedclosedate" , ConditionOperator .NextXYears, 3) } } }; entityCollection = _service.RetrieveMultiple(query).Entities; foreach ( Opportunity opportunity in entityCollection) { Console .WriteLine( "Opportunity ID: {0}" , opportunity.Id); Console .WriteLine( "Opportunity: {0}" , opportunity.Name); } using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query;
52. Retrieve Multiple Fetch XML 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani fetchXml= @"<fetch> <entity name='opportunity'> <attribute name='name'/> <filter type='and'> <condition attribute='estimatedclosedate' operator='next-x-years' value='3'/> </filter> </entity> </fetch>" ; // Build fetch request and obtain results. RetrieveMultipleRequest efr = new RetrieveMultipleRequest () { Query = new FetchExpression (fetchXml) }; entityResults = (( RetrieveMultipleResponse )_service.Execute(efr)).EntityCollection; foreach ( var e in entityResults.Entities) { Console .WriteLine( "Fetch Retrieved: {0}" , e.Attributes[ "name" ]); }; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query;
53. Retrieve Multiple using LINQ 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani OrganizationServiceContext context = new OrganizationServiceContext ( service ); var accounts = ( from a in context.CreateQuery ( "account" ) where (( string ) a [ "name" ]). StartsWith ( "Microsoft" ) select new { Id = ( Guid ) a [ "accountid" ], Name = ( string ) a [ "name" ] }).ToList(); using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query;
54. Compound Create or Update 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Entity accountToCreate = new Entity ( "account" ); accountToCreate[ "name" ] = "Example Account" ; //Define the IDs of the related letters we will create _letterIds = new [] { Guid .NewGuid(), Guid .NewGuid(), Guid .NewGuid() }; // Letter 1 for creation Entity letter1 = new Entity ( "letter" ); letter1[ "subject" ] = "Letter 1" ; letter1[ "activityid" ] = _letterIds[0]; // Letter 2 for creation Entity letter2 = new Entity ( "letter" ); letter2[ "subject" ] = "Letter 2" ; letter2[ "activityid" ] = _letterIds[1]; EntityCollection relatedLettersToCreate = new EntityCollection { EntityName = "letter" , Entities = { letter1, letter2} }; Relationship letterRelationship = new Relationship ( "Account_Letters" ); //Adds the letters to the account under the specified relationship accountToCreate.RelatedEntities.Add(letterRelationship, relatedLettersToCreate); //Passes the Account (which contains the letters) _accountId = _service.Create(accountToCreate); using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query;
56. Plug-in Development 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani // Microsoft Dynamics CRM namespace(s) using Microsoft.Xrm.Sdk; public class SamplePlugin : IPlugin { public void Execute( IServiceProvider serviceProvider) { ITracingService tracingService = ( ITracingService )serviceProvider.GetService( typeof ( ITracingService )); // Obtain the execution context from the service provider. Microsoft.Xrm.Sdk. IPluginExecutionContext context = (Microsoft.Xrm.Sdk. IPluginExecutionContext ) serviceProvider.GetService( typeof (Microsoft.Xrm.Sdk. IPluginExecutionContext )); // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains( "Target" ) && context.InputParameters[ "Target" ] is Entity ) { // Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = ( IOrganizationServiceFactory )serviceProvider.GetService( typeof ( IOrganizationServiceFactory )); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); //Plugin functionality goes here } } } public SamplePlugin() public SamplePlugin( string unsecure) public SamplePlugin( string unsecure, string secure)
57. Plug-in Development 09/08/11 Microsoft Dynamics CRM 2011 Techinical. Author – Anil Chelasani Syntax changes in Dynamics CRM 2011 plugins
<BoolParameter> (RibbonDiffXml) Specifies a value with a boolean data type that may be passed as a parameter. <Button> (RibbonDiffXml) Specifies a simple button used to perform an action. <CheckBox> (RibbonDiffXml) Specifies a check box used to select an option. <ComboBox> (RibbonDiffXml) Specifies a list used to select a value by clicking or typing. <Controls> (RibbonDiffXml) Contains a collection of controls. <CrmClientTypeRule> (RibbonDiffXml) A rule that detects the type of Microsoft Dynamics CRM client. <CrmParameter> (RibbonDiffXml) Represents data retrieved from the Microsoft Dynamics CRM application that may be passed as a parameter. <CustomAction> (RibbonDiffXml) Defines an action to add or replace items in the ribbon. <CustomRule> (RibbonDiffXml) A rule used with an EnableRule definition that is composed of a reference to a function within a JScript library. <DropDown> (RibbonDiffXml) Specifies a list used to select a value by clicking. <EnableRule> (RibbonDiffXml) Specifies either: 1. The definition of a rule used to enable or disable a ribbon element. 2. A reference to an EnableRule from a command definition. <HideCustomAction> (RibbonDiffXml) Removes an existing ribbon element from being processed for the ribbon. <IntParameter> (RibbonDiffXml) Specifies a value with a integer data type that may be passed as a parameter. <JavaScriptFunction> (RibbonDiffXml) An action that calls a function from a specific JScript library. <RecordPrivilegeRule> (RibbonDiffXml) A rule that detects a user’s privileges for a specific record in order to enable a ribbon element. <Url> (RibbonDiffXml) An action that opens a web page at a specified address.
Types: CustomAction HideCustomAction Steps: Export Account entity Locate Group where the new button needs to be inserted Extract and open Customizations.XML file Locate default RibbonDiffXml Edit XML to insert button xml