Monday, January 21, 2013

What is REST/SOAP?

What Is REST?

REST represents Representational State Transfer. REST is an architectural style in which every resource is addressed by using a unique URI. In Microsoft Dynamics CRM, a resource can be an entity collection or a record.
REST works the way the Internet works. You interact with resources by using HTTP verbs such as GET , POST , MERGE , and DELETE . Various libraries can be used to process the HTTP requests and responses. REST provides a standard interface that you can use with any programming language. REST allows for either synchronous or asynchronous processing of operations. The capability to perform asynchronous operations makes REST well suited for AJAX and Silverlight clients.
OData sends and receives data by using either ATOM or JavaScript Object Notation (JSON). ATOM is an XML-based format usually used for RSS feeds. JSON is a text format that allows for serialization of JavaScript objects.

Limitations

The REST endpoint provides an alternative to the WCF SOAP endpoint, but there are currently some limitations.
Only Create, Retrieve, Update, and Delete actions can be performed on entity records.
a.Messages that require the Execute method cannot be performed.
b.Associate and disassociate actions can be performed by using navigation properties.
Authentication is only possible within the application.
a.Use of the REST endpoint is limited to JScript libraries or Silverlight web resources.

What Is SOAP?

Unlike the REST endpoint for web resources, the SOAP endpoint uses the Organization service. This is the same service used when writing applications that exist outside of the Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online application. The differences are:
a.Requests are sent to a different URL: <organization URL>/XRMServices/2011/Organization.svc/web.b.Authentication is provided by the application.
Because authentication is provided by the application, you must create web resources in the application before the code that uses the service can operate.

Comparison of Programming Methods

You can use the SOAP endpoint for Web Resources with JScript libraries or by using Microsoft Silverlight. The process for using this endpoint is very different, depending on the technology used.

Using the SOAP Endpoint with JScript

With JScript, you will be using XmlHttpRequest to POST requests to the service. The body of the request must contain the XML appropriate for the message you are using. You must also parse the XML returned in a response. With XmlHttpRequest , it is possible to make synchronous requests. However it is highly recommended to always use asynchronous requests. Because manually configuring each request is very time consuming, it is expected that you will reuse existing libraries or create your own. Microsoft Dynamics CRM 2011 does not provide a comprehensive set of JScript libraries. The specific syntax used when calling JScript libraries depends on how they are designed. Several sample JScript libraries are provided with the Microsoft Dynamics CRM SDK, but these are not intended to represent the only or best library design. The content in the Microsoft Dynamics CRM SDK focuses on helping you create your own libraries.

What is meant by sandbox? How it is differentiate between standard environments?

 a. Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online support the execution of plug-ins in an isolated environment. In this isolated environment, also known as a sandbox.
b. Use of the full power of the Microsoft Dynamics CRM SDK to access the web services. Access to the file system, system event log, network, and more is prevented in the sandbox.
c. It is more secure, supports run-time monitoring and statistics reporting, and is supported on all Microsoft Dynamics CRM deployments.
d. In registration Sandbox, known as partial trust:
e. Outside sandbox is full trust: Full trust is supported for on-premise and Internet- Facing Microsoft Dynamics CRM deployments.
f. Microsoft Dynamics CRM Online deployment, plug-ins must be registered in the sandbox (partial trust) because no full trust run-time environment is supported. Partial trusts are also supported for on-premise deployments.
g. Sandboxed plug-ins can access the network through the HTTP and HTTPS protocols.
h. Access to localhost (loopback) is not permitted.
i. IP addresses cannot be used. Plug-ins must use a named web address that requires DNS name resolution.
j. Anonymous authentication is supported and recommended.
k. Full trust is supported for on-premises and internet facing Microsoft Dynamics CRM deployments. For a Microsoft Dynamics CRM Online deployment, plug-ins must be registered in the sandbox (partial trust) where they are isolated as previously described.

IOrganizationService Methods in CRM 2011

NameDescription
public methodAssociateCreates a link between records.
public methodCreateCreates a record.
public methodDeleteDeletes a record.
public methodDisassociateDeletes a link between records.
public methodExecuteExecutes a message in the form of a request, and returns a response.
public methodRetrieveRetrieves a record.
public methodRetrieveMultipleRetrieves a collection of records.
public methodUpdateUpdates an existing record.

The IOrganizationService Web service provides a set of methods used to perform the most common operations on system and custom entities and on the metadata for your organization. These operations can also be performed by using the IOrganizationService.Execute method and the corresponding message.

Create

Use the IOrganizationService.Create method to create an instance (record) of any entity that supports the Create message, including custom entities.

Retrieve

Use the IOrganizationService.Retrieve method to retrieve an instance (record) of an entity..

RetrieveMultiple

Use the IOrganizationService.RetrieveMultiple method to retrieve a collection records. The query can be specified using a query expression or Fetch XML query. If the query includes an aggregate function, xxx.

Update

Use the IOrganizationService.Update method to update an existing record.

Delete

Use the IOrganizationService.Delete method to delete an existing record.

Associate

Use the IOrganizationService.Associate method to create a link between two records that participate in a relationship.

Disassociate

Use the IOrganizationService.Disassociate method to delete the link between two records.

Execute

Use the IOrganizationService.Execute method to execute a message. This includes common processing like create and delete of data records and metadata, or it can be specialized processing such as import or detect duplicates.

Friday, December 28, 2012

Read records synchronously using JSON in CRM 2011


function retrieveRecordsSynchronously() {
     var retrieveRecordsReq = new XMLHttpRequest();
     var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet";
     retrieveRecordsReq.open('GET', ODataPath, false);
     retrieveRecordsReq.setRequestHeader("Accept", "application/json");
     retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     retrieveRecordsReq.send(null);
     var records = JSON.parse(retrieveRecordsReq.responseText).d;
     //Read the first account name
     alert('First Account Name is: ' + records.results[0].Name);
 }

Automatical refreshing the form in crm 2011


function ForceRefreshForm() 

         if(Xrm.Page.data.entity.getIsDirty()) 
                 Xrm.Page.data.entity.save(); 
         else 
                 window.location.reload(true); 
}  

Set the CRM Form as Read Only


function setFormAsReadOnly() {
    // define the logic here
    disableFormFields(true);
}
function doesControlHaveAttribute(control) {
    var controlType = control.getControlType();
    return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid";
}
function disableFormFields(onOff) {
    Xrm.Page.ui.controls.forEach(function(control, index) {
        if (doesControlHaveAttribute(control)) {
            control.setDisabled(onOff);
        }
    });
}

Passing configuration data using Plug-in registration tool (Secured vs. Unsecured)


  • CRM plugin registration tool contain “Unsecure & Secure” configuration sections while registering a “Step”
  • We can pass configuration data and can be used in the plug-in logic.
Secured vs. Unsecured
Below are key differentiations between Secured and Unsecured data configuration
  • Access
    • Data passed through “Unsecure” section is PUBLIC (i.e., It can be read by any user in CRM).
    • Only users with “System Administrator” role have access to the data passed through “Secure” configuration section
  • Storage
    • “Unsecure” config data will be stored along with the Plugin ‘Step’ registration information (i.e., In SdkMessageProcessingStep entity)
    • “Secure” config data will be stored in a separate entity named “SdkMessageProcessingStepSecureConfig
      • Only “System Administrator” has Read access on this entity, hence only users with ‘Sys Admin’ role can access this data
    • Both “Secured & Unsecured” configuration data stored as “Plain text” in DB
  • Outlook Sync
    • “Unsecured” configuration data is downloaded to the user’s computer when they go offline making it Unsecure
    • “Secured” configuration data is NOT downloaded to User’s Computer when they go Offline
How to read Configuration data in Plug-in
In our plug-in class, we can define a constructor that passes two parameters (i.e., unsecure configuration and secure configuration)

public class AccountCreateHandler: IPlugin{
  public AccountCreateHandler(string unsecure, string secure){
    // Do something with the parameter strings.
  }
  public void Execute(IPluginExecutionContext context){
    // Do something here.
  }
}

 If you want to read “Secure” configuration in the plug-in code, either change the user context in plugin registration as “CRM administrator ” or Impersonate to “CRM Administrator” role user in the code