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



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

Saturday, May 7, 2016

Sitecore Command Line tool without using web service.

Its been there for ages and It is somewhere stuck somewhere in world wide web. Most of us must be knowing about this. I was fascinated by this one as it made my development stuff so easy. I can do lot of stuff with this one. I explored powershell extension as well. It is master piece but having said about this, my challenge was installing it in my local dev box which was causing bit of issue with power shell extension for sitecore however i need time to fix that one. To my rescue was Alen Pelin's blog of command line from visual studio without web service to hook sitecore database.
Check this out,

http://blog.alen.pw/2013/05/access-sitecore-data-api-directly.html

and source code repository.
Bit bucket source code to more

https://bitbucket.org/alenreg/sitecore-command-line-tool/src/1a9a1717733e7329a26abf398f44f810fc004cc4?at=master


Sunday, May 1, 2016

Json Data format issue /Date() in Asp.net MVC

I struggled bit with getting over to this Json date format issue. I got the lead with Hanselman . Scott tried his best to point to right direction but all he was intended towards web api 2. I was having a issue with asp.net mvc underlying Json.net.


After working on this for an hour I started getting lot of solution via stackoverflow and many other blog post.


We can handle this date issue at client side but I personally want to handle everything at server side.


I liked Bipin Joshi Solution, which is kind of straight forward or straight cut solution.




http://www.developer.com/net/dealing-with-json-dates-in-asp.net-mvc.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace JsonNetExtension
{
    public class JsonNetResult: JsonResult
    {
        public new object Data { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = "application/json";
            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;
            if (Data != null)
            {
                JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Newtonsoft.Json.Formatting.Indented };
                JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings());
                serializer.Serialize(writer, Data);
                writer.Flush();
            }
        }
    }
}