Tuesday 17 September 2013

Monday 16 September 2013

MSCRM 2011 Paging Queries

As u all Know CRM "RetireveMultiple" fetches all the records . In  a Scenario where you want to restrict it to retrieve the count of records according to the business rule, the below query would be used to restrict the count

public Guid retrievesite(int count)
{
       

        QueryExpression qe = new QueryExpression("new_scans");
        qe.PageInfo = new PagingInfo();
        qe.PageInfo.Count = count;// pass number to which it should be restricted
        qe.ColumnSet = new ColumnSet(true);
        qe.Criteria.AddCondition("statuscode", ConditionOperator.Equal,100000003);
        EntityCollection scans = _xrm.RetrieveMultiple(qe);
        if (scans.Entities.Count > 0)
  {
   foreach (Entity site in scans.Entities)
    {
     string  _sitename = site.Attributes["new_name"].ToString();
    }
  }

}

Thanks

Friday 13 September 2013

To Create and Remove Connections in MSCRM 2011 Programatically

Hi CRM Dynamites,


In Microsoft Dynamics CRM 2011, a new feature called Connections adds a layer to records organization, allowing users to connect records in an intuitive manner. Connections gives users insight into their company’s records in Dynamics CRM 2011 at a glance. A user can open any record and quickly identify relationships or a hierarchy by looking at that record’s Connections with other records.

Scenario: Suppose u have a Account entity and Contact Entity and u want to create a connection for Account with Contact Entity.

To Create this Connections Programmatically below code can be used :


public void Create_Accc_Contact_Connection(Guid accountid, int objectTypeCode) //Account Entity Obeject Typecode
        {
          
                Guid Contact = ContactGUID;
 Connection newConnection = new Connection
                    {
                        Record1Id = new EntityReference(ContactEntityLogicalName,
                                                Contact),
                        Record1RoleId = new EntityReference(ConnectionRole.EntityLogicalName,
                                                ContactConnectionRoleId),
                        Record2RoleId = new EntityReference(ConnectionRole.EntityLogicalName,
                                                AccountConnectionRoleId),
                        Record2Id = new EntityReference(AccountEntityLogicalName,
                                                accountid)
                    };

                    service.Execute(newConnection);
              
      
          
        }





To Remove Connection Programmatically between entity below code can be used :


 
public void Remove_Accc_Contact_Connection(Guid accountid, int objectTypeCode) //Account Entity Obeject Typecode
        {
          
                Guid Contact = ContactGUID;
                QueryExpression query = new QueryExpression
                {
                    EntityName = Connection.EntityLogicalName,
                    Criteria =
                    {
                        Conditions =
                        {
                            new ConditionExpression("record1id", ConditionOperator.Equal, accountid),
                            new ConditionExpression("record2objecttypecode", ConditionOperator.Equal, objectTypeCode),
                            new ConditionExpression("record2roleid", ConditionOperator.Equal, Contact)
                        }
                    },
                };
                    service.Delete(Connection.EntityLogicalName, ent.Id);
              
      
          
        }



Hope this post helps for your requirement

Thanks

 

Associate Request MSCRM 2011

Scenario: I have 2 Entity "Entity A" and Entity B", and there is a relationship 1:N between "Entity A"and "Entity B".Suppose you have create a new record for "Entity A"and since there is a relationship between "Entity A"and "Entity B", so you would like to relate this "Entity A" record to a specific or many records of "Entity B". I will use a class called “Associate Request”  which links records to each user assuming there is a relationship between the entities which records belong to . Below is the code snippet to achieve:



IOrganizationSericce service;  // instance of the organization serivce
AssociateRequest  contactToAccount = new AssociateRequest
 {

//Entity A Record
Target = new EntityReference(Entity A LogicalName, Entity A.GUID),
RelatedEntities = new EntityReferenceCollection
 {
 new EntityReference(Entity B LogicalName, Entity B.GUID)
 },

Relationship = new Relationship(“name of the relationship already existing”)
 };

              

Thanks.. :)
 

Bulk import functionality using Execcute Multiple Request UR12 -Not OOB feature

Hi folks, after long time adding something to my blog

This is a small feature which was requested by my client where they wanted to have bulk import functionality in there Web application but they never wanted to use OOB Bulk Import functionality in MSCRM 2011.
Scenario: Customer would upload a excel file from web application which consists (1000 Min) rows , for which the record should be created in MSCRM 2011

We Used normal create call but the time consumed to created record was very High . In a scenario time taken to create 25 records is (90 Sec Approx.) . Later we achieved the same using the new class in UR12 "ExecuteMultipleRequest" class. using which 25 records got created in (12 sec Approx.).Below code gives the demo of how to create a series of record using the new class.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.ServiceModel;
using MS.GetOnline.SiteInfo.code;
using MS.GetOnline.SiteInfo.CrmOrgServiceLib;
using Microsoft.Xrm.Sdk;
using System.Globalization;
using AjaxControlToolkit;
using System.Runtime.InteropServices;
using System.Collections;
using System.Text.RegularExpressions;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Crm.Sdk.Messages;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using System.Configuration;
using System.Data;
using System.Diagnostics;
namespace MS.GetOnline.SiteInfo
{
    public partial class Bulk_Import : System.Web.UI.Page
   
{
 public void bulkimport(List<SiteInformation> objSiteInformation, string status)
        {
            try
            {
//Object which we create using the data imported in Excel sheet Count.
                int noOfRecs = objSiteInformation.Count();
                List<Guid> _createdSiteGuids = new List<Guid>();
                #region Site with ExecuteMultipleRequest

                ExecuteMultipleRequest SiteCreation = new ExecuteMultipleRequest()
                {
                    // Assign settings that define execution behavior: continue on error, return responses.
                    Settings = new ExecuteMultipleSettings()
                    {
                        ContinueOnError = true,
                        ReturnResponses = true
                    },
                    // Create an empty organization request collection.
                    Requests = new OrganizationRequestCollection()
                };
                for (int i = 0; i < noOfRecs; i++)
                {
                    Entity site = new Entity("Entity Logical Name");
                    site["new_name"] = objSiteInformation[i].SiteUrl;
                    Guid PrimaryContactIdStr = new Guid(Crm.Data.GetContactIdByFullName(objSiteInformation[i].PrimaryContactName));
                    site["new_primarycontactid"] = new EntityReference("contact", PrimaryContactIdStr);
                    site["new_username"] = objSiteInformation[i].UserID;
                    site["new_password"] = objSiteInformation[i].Password;
                
                    if (objSiteInformation[i].dataClass == "LBI")
                    {
                        site["new_dataclassification"] = new OptionSetValue(100000000);
                    }
                    else
                        if (objSiteInformation[i].dataClass == "MBI")
                        {
                            site["new_dataclassification"] = new OptionSetValue(100000001);
                        }
                        else
                        {
                            site["new_dataclassification"] = new OptionSetValue(100000002);
                        }
                   //  Create New request For ExecuteMutlipleRequest                  
                    CreateRequest createReq = new CreateRequest { Target = site };
                    SiteCreation.Requests.Add(createReq);
                }
                // Execute all the requests in the request collection using a single web method call.
                ExecuteMultipleResponse siteResults = (ExecuteMultipleResponse)_xrm.Execute(SiteCreation);
                // Display the results returned in the responses.
                foreach (var responseItem in siteResults.Responses)
                {
                    // A valid response.
                    if (responseItem.Response != null)
                    {
                        _createdSiteGuids.Add(new Guid(responseItem.Response.Results["id"].ToString()));
                    }
                }
               }


}
}


Hope this helps you in your scenario.....