Monday, May 7, 2018

Set up and initialize Firebase for Cloud Functions

Hello World,

Serverless Computing is setting it's way up for about 2 years now. Now we have Azure Functions, AWS Lambda and Google Cloud Functions.

This post is on simple steps to create Google Cloud Function setup.

Cloud functions is a serverless provided by Google Firebase. Developers can upload their back-end code in functions to the cloud and the cloud automatically executes the corresponding logic based on event triggers and HTTP requests.

For more details you can refer https://firebase.google.com/docs/functions/get-started

We will create two functions here as,
                addMessage()
                makeUppercase()

1.  Install Node.js. Firebase CLI requires Node.js and npm.

2.  Install the Firebase CLI using Node Command Prompt in admin mode.
     "npm install -g firebase-tools"

3.  Authenticate the firebase tool
             Run "firebase login"

4.  Go to https://console.firebase.google.com/ an create your project there. Go to your Firebase                 project directory.
             Run "firebase init functions"

5.  Select the firebase project or create a new one. Install dependencies with npm if needed.

6.  Select the language.

After these commands complete successfully, your project structure looks like this:


myproject
+- .firebaserc # Hidden file that helps you quickly switch between
| # projects with `firebase use`
|
+- firebase.json # Describes properties for your project
|
+- functions/ # Directory containing all your functions code
|
+- .eslintrc.json # Optional file containing rules for JavaScript linting.
|
+- package.json # npm package file describing your Cloud Functions code
|
+- index.js # main source file for your Cloud Functions code
|
+- node_modules/ # directory where your dependencies (declared in # package.json) are installed


7. Open the index.js file and add your functions


For the addMessage() function, add these lines to index.js:

// Take the text parameter passed to this HTTP endpoint and insert it into the // Realtime Database under the path /messages/:pushId/original exports.addMessage = functions.https.onRequest((req, res) => {   // Grab the text parameter.   const original = req.query.text;   // Push the new message into the Realtime Database using the Firebase Admin SDK.   return admin.database().ref('/messages').push({original: original}).then((snapshot) => {     // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.     return res.redirect(303, snapshot.ref.toString());   }); });
For the makeUppercase() function, add these lines to index.js:
// Listens for new messages added to /messages/:pushId/original and creates an // uppercase version of the message to /messages/:pushId/uppercase exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')     .onCreate((snapshot, context) => {       // Grab the current value of what was written to the Realtime Database.       const original = snapshot.val();       console.log('Uppercasing', context.params.pushId, original);       const uppercase = original.toUpperCase();       // You must return a Promise when performing asynchronous tasks inside a Functions such as       // writing to the Firebase Realtime Database.       // Setting an "uppercase" sibling in the Realtime Database returns a Promise.       return snapshot.ref.parent.child('uppercase').set(uppercase);     });


8.  Run this command to deploy your functions: 

          "firebase deploy --only functions"



9.   Now go and check your https://console.firebase.google.com/ project you will see the functions  available there. Use the URL provided by CLI which will be available in firebase console function as well. It will be similar to below with your project name in it.
https://us-central1-MY_PROJECT.cloudfunctions.net/addMessage?text=uppercasemetoo
10. Now use this url in a browser and you will be navigated to firebase database where you can see the funtion results.


Windows PowerShell to deploy packages Dynamics 365 V9


Hello Troubled World,

I used Powershell to deploy some configuration data along with solutions into Dynamics 365 following https://msdn.microsoft.com/en-us/library/dn688182.aspx. It worked well for V8.2 and bellow.

Then with the V9 update I followed https://technet.microsoft.com/en-us/library/dn647420.aspx instructions and yet came across in below error w
hen running the RegisterXRMPackageDeployment.ps1 in powershell 3.0 as administrator, I get "Cannot verify the Microsoft .NET Framework version 4.5.2 because it is not included in the list of permitted versions" message and it continued exporting cmdlets.

The reason is Dynamics 365 V9 requires the .NET Framework 4.5.2 and we have to use TLS12 Security Protocol to use powershell in .net 4.5.2. 
When accessing services authenticated via AAD (Azure Active Directory) it  requires TLS 1.2 protocol. Somehow power-shell doesn't get the machine's setup for TLS 1.2 and hence we have to manually give the configurations.

by adding below line before accessing CRM will provide the required permissions.

[System.Net.ServicePointManager]::SecurityProtocol = 
[System.Net.SecurityProtocolType]::Tls12

eg:

Add-PSSnapin Microsoft.Xrm.Tooling.Connector
Add-PSSnapin Microsoft.Xrm.Tooling.PackageDeployment 
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$CRMConn = Get-CrmConnection -InteractiveMode
Import-CrmPackage -CrmConnection $CRMConn -PackageDirectory D:\test\ -PackageName testPkg.dll -Timeout 1:00:00:00 -Verbose





Thursday, February 26, 2015

Backup a dll saved to database in the Plugin Registration Tool for Microsoft CRM 2011


Hello World,

When an assembly in the Plugin Registration Tool that is saved to the database to take a backup of it has to be done through CRM.

Have to export it as a solution:

1)      Create a new solution named 'Plugin Backup' or something (Settings->Solutions->New)
2)      Add the plugin (Plugin Assemblies -> Add Existing)
3)      Export the solution as an Unmanaged solution (Export Solution)
This will create a solution zip that contains your dll, which you can then re-import using 'Import Solution' if you need to restore to a previous version.


This is for CRM 2011 On-Premise

Monday, December 8, 2014

Get list of Entities in CRM query by Display Name (CRM bulk update)

Hello World,


MetadataSchema.Entity does not have the Display Name of the Entity in it. To get Display Name of a Entity we have to use MetadataSchema.LocalizedLabel. 
In the bellow query I get the all Entities where Entity Display Name starts from 'Z'.

select distinct e.Name, e.LogicalName, e.OriginalLocalizedName, OptionSetValue.Label
from MetadataSchema.Entity as e inner join
MetadataSchema.LocalizedLabel as OptionSetValue on (e.EntityId = OptionSetValue.ObjectId and
OptionSetValue.ObjectColumnName = 'LocalizedName')
where OptionSetValue.Label like 'Z%'
order by OptionSetValue.Label

With this we can easily get a list of entities and can do other necessary implementations in bulk. 


For example this is a good input to the query in below link,
http://matheeid.blogspot.com/2014/12/bulk-hide-entities-from-advance-find-in.html


Bulk Hide Entities from Advance Find in MS Dynamics CRM


Hello World,

If you are in trouble with unnecessary Entities in your Advance Find (that you created while ago and no longer needed) but you don't want to remove them. And you want to hide them from your customers. And you are too lazy and you want one query that works for all. :)
I was that lazy.

EntityMetadata.IsValidForAdvancedFind Property 

     - Gets or sets whether the entity will be shown in Advanced Find

if IsValidForAdvancedFind = 1 
then referring entity will be shown in Advance Find.


Therefore to hide a entity from Advance Find we can write - 

         update MetadataSchema.Entity
         set IsValidForAdvancedFind = 0
         where  Name = 'EntityName' 

Above will hide the contact  entity for all users  on the advanced.


Then to hide the Entity been displayed under related Entities we can write,

update MetadataSchema.Relationship 
set IsValidForAdvancedFind = 0
where ReferencingEntityId = (select MetadataSchema.Entity.EntityId 
    from MetadataSchema.Entity 
    where Name = 'EntityName') and IsValidForAdvancedFind  = 1)


If you have multiple Entities to update you can easily change the '=' to 'IN' and write all the Entities as a array.

update MetadataSchema.Entity 
set IsValidForAdvancedFind = 0 
where Name IN ('EntityName1', 'EntityName2', ......)


Hope this would be useful
Mathee

Thursday, October 23, 2014


When Run CRM  report if you don't get exact page count as below,


You can changes this by changing CRM  report viewer aspx file.

go to 
CRM/CRMWeb/CRMReports/rsviewer/reportviewer.aspx file

and change the below highlighted report control properties by adding PageCountMode="Actual" property.

In default report viwer pageCountMode is Estimate.


</style>
</head>
<body style="<%if (doNotShowBorder) { %> border-style:none;<% } %>">
<div style="height:100%; width:100%; position:absolute">
<form id="form1" runat="server" style="height:100%">
<asp:ScriptManager id="scriptManager"  runat="server" />
<rsweb:ReportViewer id="reportViewer"  runat="server" width="100%" height="100%"/>
</form>
</div>

</body>
</html>




<rsweb:ReportViewer id="reportViewer" PageCountMode="Actual"   runat="server" width="100%" height="100%"/>

Thursday, June 5, 2014

Zip files in ASP.Net, .net framework 4.0 with Telerik

For .Net Framework 4.0 and bellow there is no inbuilt zipping functionality. 3rd party libraries like DotNetZip, SharpZipLib are playing a great role in this scenario. But if you already use Telerik custom controls for your other developments on a project then you can easily use Telerik ZipPackage to crate zip  files.

using Telerik.Web.Zip;
using System.IO;
.....
...
..
Private void GeneratePdfFiles()
{
...//report parameters and data source credentials

    MemoryStream memoryStream = new MemoryStream();
    ZipPackage Package = ZipPackage.Create(memoryStream);
    Stream stream = default(Stream);

    foreach(reportParameters  in reportParameterCollection)
   {
    byte[] reportByte= RenderReport(reportServerUrl, reportParameters, reportPath, dataSourceCredentials, networkCredential, "pdf");
    stream = new MemoryStream(reportByte);
    Package.AddStream(stream, string.Format("ReportName.pdf"), Telerik.Web.Zip.CompressionType.Default);
    }
SendZipToClient(memoryStream , Package);
}

private void SendZipToClient(MemoryStream memStream, ZipPackage Package)
{  
    Package.Close(false);
    memStream.Position = 0;
    if (memStream != null && memStream.Length > 0)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=FormB.zip");
        Response.ContentType = "application/zip";
        Response.BufferOutput = false;   // to prevent buffering
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
                while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    Response.OutputStream.Write(buffer, 0, bytesRead);
                }
                Response.End();
    }
}