Benutzer:MovGP0/CRM/Entities

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


Entity Customization

[Bearbeiten | Quelltext bearbeiten]

JavaScript Events

[Bearbeiten | Quelltext bearbeiten]

Xrm.Page is used for customization:

  • Solutions
  • ComponentsEntities → … → Forms
  • select Form
  • More Actions ▼
  • Edit
  • Form Properties
  • Events
Example
function setupEvents()
{
   Xrm.Page.getAttribute("myns_height").addOnChange(doCalc);
   Xrm.Page.getAttribute("myns_width").addOnChange(doCalc);
}

function doCalc()
{
   var height = Xrm.Page.getAttribute("myns_height").getValue();
   var width = Xrm.Page.getAttribute("myns_width").getValue();
   var area = height * width;
   Xrm.Page.getAttribute("myns_area").setValue(area);
}
using System;
using System.Collection.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;

namespace ActionsPlugin
{
   public class ActionsSample : IPlugin
   {
      public void Execute(IServiceProvider serviceProvider)
      {
         var context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
         var caseRecord = context.InputParameters["Target"] as EntityReference;
         var excalatedBy = context.InputParameters["ExcalatedBy"] as EntityReference;
         var priority = context.OutputParameters["Priority"].ToString();
      }
   }
}
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
  </configSections>
  <connectionStrings>
    <add name="Xrm" connectionString="Server=http://crm/contoso; Domain=CONTOSO; Username=Administrator; Password=pass@word1"/>
  </connectionStrings>
  <microsoft.xrm.client>
    <contexts default="Xrm">
      <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" connectionStringName="Xrm"/>
    </contexts>
  </microsoft.xrm.client>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

Entity is already created in the CRM.

Generates the Xrm-class.[1]

crmsvcutil /server:"http://crm-server-name/Contoso" /namespace:"Contoso.Xrm" /dataContextPrefix:"Contoso" /dataContextClassName:"ContosoXrmContext" /classNameFormat:"{0}Entity" /out:"Xrm.cs" /useDisplayNames
crmsvcutil /connectionString:"Authentication Type=SPLA; Server= http://crm-server-name/Contoso; User ID=user-name; Password=user-password" ...

XrmServiceContext

[Bearbeiten | Quelltext bearbeiten]
using Contoso.Xrm;

class Programm
{
   private static void Main()
   {
      using(var context = new ContosoXrmContext("Xrm")
      {
         Query(Xrm);
         Command(Xrm);
      }
   }
   
   private static void Query(ContosoXrmContext context)
   {
      var contacts = xrm.ContactSet.Where(c => c.EmailAddress1.EndsWith("@example.com"));
      foreach(var contact in contacts)
      {
         Console.WriteLine(contact.FullName);
      }
   }
   
   private static void Command(ContosoContext context)
   {
      var contact = new Xrm.Contact{ ... };
      xrm.AddObject(contact);
      xrm.SaveChanges();
   }
}

Entity is not yet available at build-time. Use Microsoft.Xrm.Entity-Class.

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;

namespace Microsoft.Crm.Sdk.Samples
{
    public class CRUDOperations
    {
        public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete)
        {
            try
            {
                using (var serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
                {
                    var service = (IOrganizationService) serviceProxy;

                    // CREATE
                    var account = new Entity("account");
                    account["name"] = "Fourth Coffee";
                    Guid accountId = service.Create(account);
                    Console.Write("{0} {1} created, ", account.LogicalName, account.Attributes["name"]);

                    // READ
                    var attributes = new ColumnSet(new string[] { "name", "ownerid" });
                    account = _service.Retrieve(account.LogicalName, _accountId, attributes);
                    
                    // UPDATE
                    account["address1_postalcode"] = "98052";
                    account["address2_postalcode"] = null;
                    account["revenue"] = new Money(5000000);
                    account["creditonhold"] = false;
                    service.Update(account);
                    
                    // DELETE
                    bool deleteRecords = true;
                    service.Delete("account", _accountId);
                }
            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            {
                throw;
            }
        }

        static public void Main(string[] args)
        {
            try
            {
                var serverConnect = new ServerConnection();
                ServerConnection.Configuration config = serverConnect.GetServerConfiguration();

                CRUDOperations app = new CRUDOperations();
                app.Run(config, true);
            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
                Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
                Console.WriteLine("Message: {0}", ex.Detail.Message);
                Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText);
                Console.WriteLine("Inner Fault: {0}", ex.Detail.InnerFault == null ? "No Inner Fault" : "Has Inner Fault");
            }
            catch (TimeoutException ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine("Message: {0}", ex.Message);
                Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
                Console.WriteLine("Inner Fault: {0}", ex.InnerException.Message == null ? "No Inner Fault" : ex.InnerException.Message);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine(ex.Message);

                if (ex.InnerException != null)
                {
                    Console.WriteLine(ex.InnerException.Message);

                    FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                    if (fe != null)
                    {
                        Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
                        Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
                        Console.WriteLine("Message: {0}", fe.Detail.Message);
                        Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText);
                        Console.WriteLine("Inner Fault: {0}", fe.Detail.InnerFault == null ? "No Inner Fault" : "Has Inner Fault");
                    }
                }
            }
            // Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
            // SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.
            finally
            {
                Console.WriteLine("Press <Enter> to exit.");
                Console.ReadLine();
            }
        }
    }
}
var service = context as IOrganizationService;

var relatedEntities = new EntityReferenceCollection();
relatedEntities.Add(new EntityReference("account", account1Id));
relatedEntities.Add(new EntityReference("account", account2Id));
relatedEntities.Add(new EntityReference("account", account3Id)); 

// Create an object that defines the relationship between the contact and account.
var relationship = new Relationship("account_primary_contact");  

// Associate the contact with the accounts.
service.Associate("contact", contactId, relationship, relatedEntities); 

// Disassociate the records.
service.Disassociate("contact", contactId, relationship, relatedEntities);
  1. Code Generation Using the CrmSvcUtil Tool. In: MSDN. Microsoft, abgerufen am 24. Juni 2014.