Friday, May 24, 2019

SDK: Create Email Using Template

        private static Guid CreateEmailMessageFromTemplate(IOrganizationService service, EntityReference contact, Guid templateId, EntityReference fromQueue)
            InstantiateTemplateRequest request = new InstantiateTemplateRequest()

                TemplateId = templateId,
                ObjectId = contact.Id,
                ObjectType = contact.LogicalName
            InstantiateTemplateResponse response = (InstantiateTemplateResponse)service.Execute(request);
            Entity email = response.EntityCollection[0];
            var toActivityParty = new Entity("activityparty");
            toActivityParty["partyid"] = new EntityReference("contact", contact.Id);
            EntityCollection toParty = new EntityCollection() { EntityName = "activityparty" };

            var fromActivityParty = new Entity("activityparty");
            fromActivityParty["partyid"] = fromQueue;
            EntityCollection fromParty = new EntityCollection() { EntityName = "activityparty" };

            email.Attributes["to"] = toParty;
            email.Attributes["from"] = fromParty;
            return service.Create(email);

Restrict Browser Back button

history.pushState(null, null, location.href);
    window.onpopstate = function () {

Use the above code to restrict going to the back page from the current page.

This piece of code comes very handy when you are working on dynamics portals and you have logic where you should not allow users to navigate to back page.

Tuesday, October 23, 2018

D365 - Retreive more than 5000 records using fetchxml

 private static List RetrieveAllRecords(IOrganizationService service, string fetch)  
       var moreRecords = false;  
       int page = 1;  
       var cookie = string.Empty;  
       List<Entity> Entities = new List();  
         var xml = string.Format(fetch, cookie);  
         var collection = service.RetrieveMultiple(new FetchExpression(xml));  
         if (collection.Entities.Count >= 0) Entities.AddRange(collection.Entities);  
         moreRecords = collection.MoreRecords;  
         if (moreRecords)  
           cookie = string.Format("paging-cookie='{0}' page='{1}'", System.Security.SecurityElement.Escape(collection.PagingCookie), page);  
       } while (moreRecords);  
       return Entities;  


  var fetch = "<fetch {0}>" +  
             "  <entity name='accounts' >" +  
             "    <attribute name='accountid' />" +  
             "    <attribute name='name' />" +  
             "  </entity>" +  
       var accountCol = RetrieveAllRecords(service, fetch);  

Monday, August 27, 2018

Dynamics Portals Caching issue

When you change any portal configuration in CRM it doesn't gets reflected on the portals unless the portal is restated from the admin portal. This is very painful when we tend to do some small changes as it takes at least 2-3 mins to restart the portal.

Portal provides about page which has the clear cache option which can be used in this senarions. to access the page append the Portal URL + /_services/about

Make sure the user accessing this page is logged into the portal and have "administrator" web role

Tuesday, March 13, 2018

Error Connecting to Dynamics 365 Online from Report Viewer in VS

After entering connection details the connection dialog prompts for the credentials multiple times and this is because of the recent changes by Microsoft to he overall platform

Its all because of the latest update in the Microsoft TSL(Transport Security Layer) Protocol in SDK assemblies..Microsoft allowed the TSL connection 1.0  and 1.1 for the browsers or client to connect the CRM org.Now Microsoft will support only TSL 1.2 or above going forward(Reference) . If you are connecting your org with the old version of plugin registration tool , then you may face this issue.

Uninstall Report Authoring Extension and install the latest.(Make sure Installed dll is the latest SDK 9.0). if this doesnt resolve the issue then follow the below step.

On the machine where VS is installed go to the start menu, then type run and then enter. Type in regedit and then OK.

Once the Registry Editor is open, go to: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319
Right click on the name of the folder (the v4.0.30319 folder) and select New, then DWORD. Give it the name of SchUseStrongCrypto and the Value of 1. Exit the Registry Editor, then restart your machine. 

Wednesday, February 14, 2018

SDK - Send Email Using Template with attachement

 InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest  
             TemplateId = templateId,  //template guid
             ObjectId = incidentId,   // template type id
             ObjectType = "incident"  
 InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);  
 Entity instTemplate = (Entity)instTemplateResp.EntityCollection.Entities[0];  
 Guid emailGuid = service.Create(instTemplate);  
 //Add attachement  
 Entity emailAttachment = new Entity("activitymimeattachment");  
 //attachement in byte[]  
 emailAttachment["body"] = Convert.ToBase64String(attachement);  
 emailAttachment["objectid"] = new EntityReference("email", emailGuid);  
 emailAttachment["objecttypecode"] = "email";  
 emailAttachment["filename"] = item.Name;  
 service.Create(emailAttachment);  //Creates attachement to email
 SendEmailRequest sendEmailReq = new SendEmailRequest()  
             EmailId = emailGuid,  
             IssueSend = true  
  SendEmailResponse sendEmailResp = (SendEmailResponse)service.Execute(sendEmailReq);  

Saturday, September 23, 2017

Retrieve EntityImage using SDK

In this example we will retrieve contact entity image

var Contact=Service.Retreive("contact","GUID",new ColumnSet(true));
byte[] entityimage=Contact["entityimage"] as byte[];

the above piece of code has no issues and ideally should Retreiveall the attributes of the contact as new ColumnSet(true) is specified but this will not get the entity image 

When you use RetrieveMultiple or Retrieve the EntityImage is not included when the ColumnSet.AllColumns property is set to true. Because of the potential size of data in this attribute, to return this attribute you must explicitly request it.
The binary data representing the image isn’t returned using the deprecated ExecuteFetchRequest class. You should use RetrieveMultipleRequestinstead.

Reference -