Sunday, June 12, 2016

Run Powershell script from Gulp.js

Installation Requirement

  1. If your Visual Studio is 2013 or order install Task Runner Explorer - Download and install- https://visualstudiogallery.msdn.microsoft.com/8e1b4368-4afb-467a-bc13-9650572db708
  2. Node.js- Recommended for most users-Check this download link-https://nodejs.org/en/
 
  • Run from the cmd command from this solution or class library location.
  • Ensure you keep the gulp.js in location where the actions /events are required.
  • Run command below from Class Library or website folder: Npm I gulp -g

Change Environment variable path

Gulp .js can be run from Task Runner VS2015

var gulp = require("gulp");
var childProcess = require('child_process');

gulp.task('deploy-static-files', function (callback) {
    console.log("---------Start--------------------------");
    return childProcess.exec('Powershell.exe  -executionpolicy remotesigned -File  DeployStaticFiles.ps1', function (err, stdout, stderr) {
        console.log(stdout);
        console.log(stderr);
        if (err) console.log(err);

        console.log("Finished");
    });
})

Powershell script :Deploy Static file code

####----Copy files from source to destination-----
$publishPathDestination      =   "Some Path"
$scriptpathSource            = Split-Path $MyInvocation.MyCommand.Path
Copy-Item  "$scriptpath"     $publishPathDestination   -Recurse -Force;


Get description Attributes from Enum.

using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
         //Get Description
            Console.WriteLine(GetEnumDescription<MyEnum>(2));

            //Get Value
            Console.WriteLine(MyEnum.Hear);

            //Get Name
            Console.WriteLine(Enum.GetName(typeof(MyEnum), 1));

            //Get Enum Descriptions values
            var myEnumDescriptions =
                Enum.GetValues(typeof(MyEnum))
                    .Cast<MyEnum>()
                    .Select(n => new { ID = (int)n, Name = Enum.GetName(typeof(MyEnum), n), Description = GetEnumDescription(n) });

            //Get Name, Description, Value
            foreach (var item in myEnumDescriptions)
            {
                Console.WriteLine(item.Name);
            }

            //Iterate and fetch Description
            foreach (int iValues in Enum.GetValues(typeof(MyEnum)))
            {
                Console.WriteLine(GetEnumDescription <MyEnum>(iValues));
            }
        }

        public enum MyEnum
        {
            [Description("You can Walk")]
            Walk = 1,
            [Description("You can Talk")]
            Talk = 2,
            [Description("You can Hear")]
            Hear = 3
        }

        public static string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                    typeof(DescriptionAttribute),
                    false);

            if (attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }

        public static string GetEnumDescription <T>(int value)
        {
            return GetEnumDescription((Enum)(object)((T)(object)value));
        }
    }


}



Tuesday, May 31, 2016

My Powershell Code Snippets


1. Copy files from source to destination

####----Copy files from source to destination-----

$publishPathDestination      =   "Some Path"
$scriptpathSource            = Split-Path $MyInvocation.MyCommand.Path
Copy-Item  "$scriptpath"     $publishPathDestination   -Recurse -Force;

2. Duplicate and copy files

 ##---Duplicate and copy files
 
 $files="F1.pdf","F2.pdf"

 Foreach ($file in $files)
 {
       Copy-Item C:\DummyFiles\dummy.pdf -Destination C:\DummyFiles\Transfer\$file
 }


3. Sitecore Powershell extensions to create daily backup



Thursday, May 26, 2016

Friday, May 13, 2016

Common Mistake :Asp.net MVC or Web Api and POST Json request

I'm sure most of the folks must be well aware of Post request that is send as Json Raw body request.
I come across very common and trivial mistake that I did when creating my web api request. I was working with my scripting guy and he asked me to form Json request in some format and end up spending an hour to figure out the problem related to my json which was not coming fine.
#Problem: Always check the Variable instead the main class or Model name. Otherwise you will end with JSON request coming in .net web.api as null values.
   
public class SOME_VIEW_MODEL
    {
        public DateTime? DOB { get; set; }
        public IList  childViewModel{ get; set; }
    }

public virtual JsonResult GetMeSomeResults(SOME_VIEW_MODEL someViewModel)
{

}
With respect to above data model design the json can be formed with two request body. #1 Request Body
   {
      "DOB": "",
      "childViewModel": [
        {
          "DOB": "1992-03-21"
        },
        {
          "DOB": "1992-03-21"
        }
      ]
    }
{
    "someViewModel":
    {
      "DOB": "",
      "childViewModel": [
        {
          "DOB": "1992-03-21"
        },
        {
          "DOB": "1992-03-21"
        }
      ]
    }
}
Mind it and double check always that you are using variable in request instead a class name. It is basic concept however we tend to make such mistake. In this case it has to be "someViewModel" instead SOME_VIEW_MODEL