Tag Cloud

CRM 2011 (161) CRM 4.0 (144) C# (116) JScript (109) Plugin (92) Registry (90) Techpedia (77) PyS60 (68) WScript (43) Plugin Message (31) Exploit (27) ShellCode (26) FAQ (22) JavaScript (21) Killer Codes (21) Hax (18) VB 6.0 (17) Commands (16) VBScript (16) Quotes (15) Turbo C++ (13) WMI (13) Security (11) 1337 (10) Tutorials (10) Asp.Net (9) Safe Boot (9) Python (8) Interview Questions (6) video (6) Ajax (5) VC++ (5) WebService (5) Workflow (5) Bat (4) Dorks (4) Sql Server (4) Aptitude (3) Picklist (3) Tweak (3) WCF (3) regex (3) Config (2) LINQ (2) PHP (2) Shell (2) Silverlight (2) TSql (2) flowchart (2) serialize (2) ASHX (1) CRM 4.0 Videos (1) Debug (1) FetchXml (1) GAC (1) General (1) Generics (1) HttpWebRequest (1) InputParameters (1) Lookup (1) Offline Plug-ins (1) OutputParameters (1) Plug-in Constructor (1) Protocol (1) RIA (1) Sharepoint (1) Walkthrough (1) Web.config (1) design patterns (1) generic (1) iframe (1) secure config (1) unsecure config (1) url (1)

Pages

Friday, May 10, 2013

CRM 2011 Solutions

Solutions - Data Modeling


Solutions - Introduction


Solutions - Managed & UnManaged


Solutions - Publishing

CrmService.Fetch Method

[Applies to: Microsoft Dynamics CRM 4.0]

Retrieves entity instances in XML format based on the specified query expressed in the FetchXML query language.

Syntax

 public string Fetch(
  string  fetchXml
 );



Parameters

fetchXml

Specifies a String that contains the fetch query string to be executed.

Return Value

Returns an XML String type that contains the results of the query.

Remarks

Use this method to execute a query expressed in the FetchXML query language.

To perform this action, the caller must have the Read privilege to the entity types being retrieved and access rights on the entity instances retrieved.

Example

The following example demonstrates the use of the Fetch method.

   1:  //# [CrmService.Fetch Method]
   2:  // Set up the CRM Service.
   3:  CrmAuthenticationToken token = new CrmAuthenticationToken();
   4:  // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory Authentication.
   5:  token.AuthenticationType = 0; 
   6:  token.OrganizationName = "AdventureWorksCycle";
   7:   
   8:  CrmService service = new CrmService();
   9:  service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
  10:  service.CrmAuthenticationTokenValue = token;
  11:  service.Credentials = System.Net.CredentialCache.DefaultCredentials;
  12:   
  13:  // Retrieve all attributes for all accounts.
  14:  // Be aware that using all-attributes may adversely affect
  15:  // performance and cause unwanted cascading in subsequent 
  16:  // updates. A best practice is to retrieve the least amount of 
  17:  // data required.
  18:  string fetch1 = @"   <fetch mapping=""logical"">
  19:                    <entity name=""account"">
  20:                       <all-attributes/>
  21:                    </entity>
  22:                 </fetch>";
  23:   
  24:  // Fetch the results.
  25:  String result1 = service.Fetch(fetch1);
  26:   
  27:  // Retrieve the name and account ID for all accounts where
  28:  // the account owner's last name is not Cannon.
  29:  string fetch2 = @"<fetch mapping=""logical"">
  30:                    <entity name=""account"">
  31:                       <attribute name=""accountid""/>
  32:                       <attribute name=""name""/>
  33:                       <link-entity name=""systemuser"" to=""owninguser"">
  34:                          <filter type=""and"">
  35:                             <condition attribute=""lastname"" operator=""ne"" value=""Cannon""/>
  36:                          </filter>
  37:                       </link-entity>
  38:                    </entity>
  39:                 </fetch>";
  40:   
  41:  // Fetch the results.
  42:  String result2 = service.Fetch(fetch2);

How to Retrieve a List of Messages and Entities that Support Plug-ins

The following code displays a list of messages and entities that support plug-ins. This sample code can be found in the following file in the SDK download:



   1:  //# [How to Retrieve a List of Messages and Entities that Support Plug-ins ]

   2:  using System;

   3:  using System.Collections;

   4:  using CrmSdk;

   5:  using MetadataServiceSdk;

   6:  using Microsoft.Crm.Sdk.Utility;

   7:   

   8:  namespace Microsoft.Crm.Sdk.HowTo

   9:  {

  10:     public class RetrieveSupportedMessages

  11:     {

  12:        public static bool Run(string crmServerUrl, string orgName)

  13:        {

  14:           bool success = true;

  15:           

  16:           // Store all Create, Retrieve, Update, and Delete sdk messages supported by the account entity for verification.

  17:           ArrayList crudMessagesForVerification = new ArrayList();

  18:   

  19:           try

  20:           {

  21:              // Set up the CRM Services.  

  22:              CrmService service = 

  23:                  Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(

  24:                  crmServerUrl, orgName);

  25:              service.PreAuthenticate = true;

  26:   

  27:              MetadataService metadataService =

  28:                  Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetMetadataService(

  29:                  crmServerUrl, orgName);

  30:              metadataService.PreAuthenticate = true;

  31:   

  32:              // Retrieve a list of all entities.

  33:              RetrieveAllEntitiesRequest allEntitiesRequest = 

  34:                  new RetrieveAllEntitiesRequest();

  35:              allEntitiesRequest.RetrieveAsIfPublished = true;

  36:              allEntitiesRequest.MetadataItems = MetadataItems.EntitiesOnly;

  37:   

  38:              // Execute the request.

  39:              RetrieveAllEntitiesResponse allEntitiesResponse =

  40:              (RetrieveAllEntitiesResponse)metadataService.Execute(allEntitiesRequest);

  41:   

  42:              // Create a query to get all related sdk messages. 

  43:              // An example SQL query that will be built for every entity:

  44:              // SELECT sdkmessage.name

  45:              // FROM   sdkmessage

  46:              // INNER JOIN sdkmessagefilter ON sdkmessagefilter.skdmessageid = sdkmessage.skdmessageid

  47:              // WHERE  sdkmessagefilter.primaryobjecttypecode = entity.LogicalName;

  48:              QueryExpression supportedMessagesQuery = new QueryExpression();

  49:              

  50:              // Iterate through the retrieved entities.

  51:              foreach (EntityMetadata entity in allEntitiesResponse.CrmMetadata)

  52:              {

  53:                 // Retrieve the supported message name for this entity.

  54:                 ColumnSet sdkMessageColumns = new ColumnSet();

  55:                 sdkMessageColumns.Attributes = new string[] { "name" };

  56:                 

  57:                 // Build the WHERE clause condition.

  58:                 ConditionExpression schemaNameCondition = new ConditionExpression();

  59:                 schemaNameCondition.AttributeName = "primaryobjecttypecode";

  60:                 schemaNameCondition.Operator = ConditionOperator.Equal;

  61:                 schemaNameCondition.Values = new object[1];

  62:                 schemaNameCondition.Values[0] = entity.LogicalName;

  63:                 

  64:                 // Create the WHERE clause filter.

  65:                 FilterExpression whereExpression = new FilterExpression();

  66:                 whereExpression.Conditions = 

  67:                     new ConditionExpression[] { schemaNameCondition };

  68:                 

  69:                 // Create the inner join link.

  70:                 LinkEntity innerJoinAccount = new LinkEntity();

  71:                 innerJoinAccount.JoinOperator = JoinOperator.Inner;

  72:                 innerJoinAccount.LinkCriteria = whereExpression;

  73:                 innerJoinAccount.LinkFromAttributeName = "sdkmessageid";

  74:                 innerJoinAccount.LinkFromEntityName =

  75:                     EntityName.sdkmessage.ToString();

  76:                 innerJoinAccount.LinkToAttributeName = "sdkmessageid";

  77:                 innerJoinAccount.LinkToEntityName =

  78:                     EntityName.sdkmessagefilter.ToString();

  79:   

  80:                 // Set the query properties.

  81:                 supportedMessagesQuery.EntityName = EntityName.sdkmessage.ToString();

  82:                 supportedMessagesQuery.ColumnSet = sdkMessageColumns;

  83:                 supportedMessagesQuery.LinkEntities = 

  84:                     new LinkEntity[] { innerJoinAccount };

  85:   

  86:                 // Retrieve all sdkmessage names for this entity.

  87:                 BusinessEntityCollection coll =

  88:                     service.RetrieveMultiple(supportedMessagesQuery);

  89:                 

  90:                 // Output the supported messages for this entity

  91:                 Console.WriteLine("============================================================================");

  92:                 Console.WriteLine("Entity: " + entity.LogicalName);

  93:                 if (coll.BusinessEntities.Length > 0)

  94:                 {

  95:                    Console.WriteLine("Supported Messages:");

  96:                 }

  97:                 else

  98:                 {

  99:                    Console.WriteLine("No Messages Supported.");

 100:                 }

 101:                 string sdkMessageName = string.Empty;

 102:                 foreach(BusinessEntity anSdkMessage in coll.BusinessEntities)

 103:                 {

 104:                    sdkMessageName = ((sdkmessage)anSdkMessage).name;

 105:                    Console.WriteLine("\t\t" + sdkMessageName);

 106:                    

 107:                    // Verify that the account entity supports create, retrieve, update, delete.

 108:                    if (entity.LogicalName == EntityName.account.ToString())

 109:                    {

 110:                       // Store all Create, Retrieve, Update, and Delete messages.

 111:                       if (sdkMessageName == "Create" || sdkMessageName == "Retrieve" ||

 112:                          sdkMessageName == "Update" || sdkMessageName == "Delete")

 113:                       {

 114:                          crudMessagesForVerification.Add(sdkMessageName);

 115:                       }

 116:                    }

 117:                 }

 118:              }

 119:   

 120:              #region check success

 121:   

 122:              // Validate that the 4 Create, Retrieve, Update, and Delete messages were found.

 123:              if (crudMessagesForVerification.Count != 4)

 124:              {

 125:                 success = false;

 126:              }

 127:   

 128:              #endregion

 129:           }

 130:           catch (System.Web.Services.Protocols.SoapException)

 131:           {

 132:              // Perform error handling here.

 133:              throw;

 134:           }

 135:           catch (Exception)

 136:           {

 137:              throw;

 138:           }

 139:   

 140:           return success;

 141:        }

 142:     }

 143:  }