locked
PLease help me on my first Plugin RRS feed

  • Question

  • Dear all,

    after several attempts, I have created my first plugin :D

     

    using System;
    using System . ServiceModel;
    using System . ServiceModel . Description;
    using System . Collections . Generic;
    
    // These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
    // located in the SDK\bin folder of the SDK download.
    using Microsoft . Xrm . Sdk;
    using Microsoft . Xrm . Sdk . Query;
    using Microsoft . Xrm . Sdk . Client;
    using Microsoft . Crm . Sdk;
    
    namespace Microsoft . Crm . Sdk . Samples
    {
     public class QueryByAttributeSample:IPlugin
     {
     public void Execute ( IServiceProvider serviceProvider)
     {
      try
      {
      IPluginExecutionContext context = ( IPluginExecutionContext ) serviceProvider . GetService ( typeof ( IPluginExecutionContext ) );
      // get reference to CRM Web Service
      IOrganizationServiceFactory serviceFactory = ( IOrganizationServiceFactory ) serviceProvider . GetService ( typeof ( IOrganizationServiceFactory ) );
      IOrganizationService service = serviceFactory . CreateOrganizationService ( context . UserId );
    
      //DevCrm . Xrm mycrmservice = new DevCrm . Xrm ( service);
    
      QueryByAttribute querybyattribute = new QueryByAttribute ( "account" );
      querybyattribute . ColumnSet = new ColumnSet ( "name");
    
      // Attribute to query.
      querybyattribute . Attributes . AddRange ( "address1_city" );
    
      // Value of queried attribute to return.
      querybyattribute . Values . AddRange ( "aaa" );
    
      // Query passed to service proxy.
      EntityCollection retrieved = service . RetrieveMultiple ( querybyattribute );
      
      System . Console . WriteLine ( "Query Using QueryByAttribute" );
      System . Console . WriteLine ( "===============================" );
    
      // Iterate through returned collection.
      foreach ( var c in retrieved . Entities )
      {
       System . Console . WriteLine ( "Name: " + c . Attributes [ "name" ] );
      }
      System . Console . WriteLine ( "===============================" );
    
      }
      catch
      {
      throw new InvalidPluginExecutionException ( );
      }
     }
     }
    }
    
    

     

    I've build it without any errors

    I've registerd it against the account entity on the update event and I was expecting to display the result of this query, I'm sure that the record with the address1 city = aaa exist but nothing happens

     

    please advise

     

    thanks in advance..

    Monday, June 13, 2011 2:04 PM

Answers

All replies

  • You cannot call System.Console.WriteLine from a CRM plugin

    Gonzalo | gonzaloruizcrm.blogspot.com

    Monday, June 13, 2011 2:06 PM
    Moderator
  • Oups....that was really silly :$

     

    so is there anyway to display the results of the query ??

     

    I'm trying to create a simple plugin sample to use in my future CRUD operations

     

    thanks in advance..

    Monday, June 13, 2011 2:11 PM
  • If you are debugging the plugin you can just follow these instructions:  http://msdn.microsoft.com/en-us/library/gg328574.aspx

    You cannot "display results" unless you throw an InvalidPluginExecutionException and you can pass some string as the constructor, then the message will be shown in the screen, but it is considered an error and will roll-back the operation. Read more here: http://technet.microsoft.com/en-us/library/microsoft.xrm.sdk.invalidpluginexecutionexception.invalidpluginexecutionexception.aspx

     

     


    Gonzalo | gonzaloruizcrm.blogspot.com

    Monday, June 13, 2011 2:15 PM
    Moderator
  • Dear Gonzalo,

     

    Would you please guide me on modifying the code to update another record with the retrieved value ??

     

    thanks in advance..

    Monday, June 13, 2011 2:22 PM
  •    Entity account = new Entity("account");
       account["description"] = "<your string here>";
       account["name"] = "Account name";
       service.Create(account);
    

     

    To update record:

    account["name"] = "New account name";
          service.Update(account);



    Gonzalo | gonzaloruizcrm.blogspot.com


    Monday, June 13, 2011 3:00 PM
    Moderator
  • Dear Gonzalo,

     

    thanks alot for your help, I didn't try your above code yet but I've tried to edit my code to create a new record based on the retrieved value as below:

    using System;
    using System . ServiceModel;
    using System . ServiceModel . Description;
    using System . Collections . Generic;
    
    // These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
    // located in the SDK\bin folder of the SDK download.
    using Microsoft . Xrm . Sdk;
    using Microsoft . Xrm . Sdk . Query;
    using Microsoft . Xrm . Sdk . Client;
    using Microsoft . Crm . Sdk;
    
    namespace Microsoft . Crm . Sdk . Samples
    {
      public class QueryByAttributeSample:IPlugin
      {
        private OrganizationServiceProxy _serviceProxy;
        private IOrganizationService _service;
        private List<Guid> _accountIds = new List<Guid> ( );
    
        string accName = "";
    
        public void Execute ( IServiceProvider serviceProvider)
        {
          try
          {
            //IPluginExecutionContext context = ( IPluginExecutionContext ) serviceProvider . GetService ( typeof ( IPluginExecutionContext ) );
            //// get reference to CRM Web Service
            //IOrganizationServiceFactory serviceFactory = ( IOrganizationServiceFactory ) serviceProvider . GetService ( typeof ( IOrganizationServiceFactory ) );
            //IOrganizationService service = serviceFactory . CreateOrganizationService ( context . UserId );
    
            //DevCrm . Xrm mycrmservice = new DevCrm . Xrm ( service);
    
            QueryByAttribute querybyattribute = new QueryByAttribute ( "account" );
            querybyattribute . ColumnSet = new ColumnSet ( "name");
    
            // Attribute to query.
            querybyattribute . Attributes . AddRange ( "address1_city" );
    
            // Value of queried attribute to return.
            querybyattribute . Values . AddRange ( "CITY" );
    
            // Query passed to service proxy.
            EntityCollection retrieved = _service . RetrieveMultiple ( querybyattribute );
    
            //// Iterate through returned collection.
            foreach ( var c in retrieved . Entities )
            {
              accName=c . Attributes [ "name" ].ToString()+" 2";
            }
    
            // Create two accounts.
            Account account = new Account
            {
              Name = accName,
              Address1_StateOrProvince = "Colorado",
              Address1_Telephone1 = "(206)555-5555",
              EMailAddress1 = "info@datum.com"
            };
            _accountIds.Add(_service.Create(account));
          }
          catch
          {
            throw new InvalidPluginExecutionException ( );
          }
        }
      }
    }
    
    


    but the code throws an exception in a logfile as below:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Exception of type 'Microsoft.Xrm.Sdk.InvalidPluginExecutionException' was thrown.Detail: 

    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">

      <ErrorCode>-2147220891</ErrorCode>

      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">

        <KeyValuePairOfstringanyType>

          <d2p1:key>OperationStatus</d2p1:key>

          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>

        </KeyValuePairOfstringanyType>

      </ErrorDetails>

      <Message>Exception of type 'Microsoft.Xrm.Sdk.InvalidPluginExecutionException' was thrown.</Message>

      <Timestamp>2011-06-13T14:59:19.37296Z</Timestamp>

      <InnerFault i:nil="true" />

      <TraceText>

     

    [Microsoft.Crm.Sdk.Samples: Microsoft.Crm.Sdk.Samples.QueryByAttributeSample]

    [ccf347a5-cb95-e011-ac8d-000c297ed18c: Microsoft.Crm.Sdk.Samples.QueryByAttributeSample: Update of account]

     

     

    </TraceText>

    </OrganizationServiceFault>

     please advise...and thanks alot for your efforts and for wasting your time to help me

    Monday, June 13, 2011 3:05 PM
  •  catch (Exception e)
       {
        throw new InvalidPluginExecutionException (e.Message, e );
       }
    

    To know what is going wrong just catch the exception and trace it:

     

     


    Gonzalo | gonzaloruizcrm.blogspot.com

    Monday, June 13, 2011 3:24 PM
    Moderator
  • Dear Gonzalo, I've used the above code to create a record with the name retrieved from the query

     

    but the below exception was thrown: Object reference not set to an instance of an object.

     

    please advise..

    Monday, June 13, 2011 3:46 PM
  • Dear Gonzalo,

     

    below is the last updated code:

    using System;
    using System . ServiceModel;
    using System . ServiceModel . Description;
    using System . Collections . Generic;
    
    // These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
    // located in the SDK\bin folder of the SDK download.
    
    using Microsoft . Xrm . Sdk;
    using Microsoft . Xrm . Sdk . Query;
    using Microsoft . Xrm . Sdk . Client;
    using Microsoft . Crm . Sdk;
    
    namespace Microsoft . Crm . Sdk . Samples
    {
      public class QueryByAttributeSample : IPlugin
      {
        private OrganizationServiceProxy _serviceProxy;
        private IOrganizationService _service;
        private List<Guid> _accountIds = new List<Guid> ( );
    
        string accName = "";
    
        public void Execute ( IServiceProvider serviceProvider )
        {
          try
          {
            //IPluginExecutionContext context = ( IPluginExecutionContext ) serviceProvider . GetService ( typeof ( IPluginExecutionContext ) );
            //// get reference to CRM Web Service
            //IOrganizationServiceFactory serviceFactory = ( IOrganizationServiceFactory ) serviceProvider . GetService ( typeof ( IOrganizationServiceFactory ) );
            //IOrganizationService service = serviceFactory . CreateOrganizationService ( context . UserId );
    
            //DevCrm . Xrm mycrmservice = new DevCrm . Xrm ( service);
    
            QueryByAttribute querybyattribute = new QueryByAttribute ( "account" );
            querybyattribute . ColumnSet = new ColumnSet ( "name" );
    
            // Attribute to query.
            querybyattribute . Attributes . AddRange ( "address1_city" );
    
            // Value of queried attribute to return.
            querybyattribute . Values . AddRange ( "CITY" );
    
            // Query passed to service proxy.
            EntityCollection retrieved = _service . RetrieveMultiple ( querybyattribute );
    
            //// Iterate through returned collection.
            foreach ( var c in retrieved . Entities )
            {
              accName = c . Attributes [ "name" ] . ToString ( ) + " 2";
            }
    
            // Create two accounts.
            Entity account = new Entity ( "account" );
            account [ "name" ] = accName;
            _service . Create ( account );
          }
          catch ( Exception e )
          {
            throw new InvalidPluginExecutionException ( e . Message , e );
          }
        }
      }
    }


    Monday, June 13, 2011 3:54 PM
  • You should debug your plugin using the information (link) I provided above, then you will be able to walk line-by-line with the debugger until you find your problem.


    Gonzalo | gonzaloruizcrm.blogspot.com

    Monday, June 13, 2011 4:03 PM
    Moderator