Sunday, April 30, 2017

Dependency- External DLLs Management in #Sitecore with NuGet



Dependency, Dependency, Dependency!!


There is the number of Library/framework/External DLLs reference uses in the most large .Net/Sitecore Projects. 

These dependencies have their own dependencies and those dependencies further depend on some other library and so on. So it’s may be more complex
Dependencies >> Dependencies >> Dependencies >> Dependencies 
The problem of dependency management is neither new nor original, it exists in all development platforms, and .NET is no different.

A very big question: Where to store external DLL files in your Sitecore based solution?

The first thing came into the mind is Lib folder in the solution, create the Lib folder and put all the required DLLs into this folder and check-in the folder in the source control.

Keeping dependencies in the source control that’s a very popular solution, and for a reason. The benefits are obvious like:

  • No setup. You already have your source control in place (hm, I hope you do!). Add \bin directory, and you are fine.
  • No learning curve. Developers are used to work with source control.
  • Shared. The whole team gets changes and updates from the server as they occur.

·        
Sounds good, doesn’t it? So, what’s wrong with it? Source control software is designed to control sources code well. As such they aren’t so great in controlling binaries. It’s not a good practice to store third party DLLs (binaries) into the source control.

The biggest problem with this approach is version upgrade, manually we need to upgrade the project reference version, let’s take one example of the popular IoC framework Ninject.

Between Jan 1, 2011 and Apr 5, 2011, there have been four different versions (2.0.1, 2.1.0.76, 2.2.0.0, 2.2.1.0). Not too many, but multiply that against a project with six or seven dependencies, and then add in the cross dependencies, and it has a nightmare.

No worries now NuGet is here.

“NuGet is a package management system for .NET. The goal of NuGet is to make the process of incorporating third party libraries into your solutions as simple as possible.”

Let’s look the great features of NuGet which makes the developer’s addicted to using this.

  • Manages packages, not dlls.
  • Provides NuGet Gallery – almost 70K packages are at your disposal for all your development needs.
  • Supports binary versioning.
  • Integrates with Visual Studio.
  • Integrates with your build.
  • Integrates with your build server

And the good thing is Sitecore also supports the NuGet package management system for distributing Sitecore package, that’s really icing on the cake for us.
Now you can manage all your third party dependencies from Nuget Package Along with Sitecore DLLs as well.


So it’s always advisable to add the Nuget package from the solution and then select the projects which required a specific package.

On a large project, you often wind up adding new projects over time, often weeks or months apart. If you are always installing packages one project at a time, and a package author happens to release an update in between your two installs, you may wind up with two projects in your solution referencing different versions of a package.

That is one of the reasons I personally preferred NuGet package from the solution only.

How to Add Third party DLLs Reference in your solution using NuGet

  1. Select the solution in solution explorer
  2. Right Click and select Manage NuGet Packages for solution…
  3. Select the  packages
  4. Now simply locate your new projects and tick the checkbox next
  5. Click Install, sit back and relax


Let’s take the example of Newtonsoft.Json Dlls that need to be included in the project reference:

Open the solution:

  1.          Right click on the solution
  2.          Select the Manage Nuget Packages for Solution Option > Click
 





Once you install this> check the output >






The newly added package has been installed and also reference has been added in the project as well.

Now we can add this as a namespace as below:



How to use Sitecore Assembly Reference in your solution using NuGet

Now we can also add the Sitecore dependency DLLs by Nuget,
Below are the steps:

Open the solution:
  1. Right click on the solution
  2. Select the Manage Nuget Packages for Solution Option > Click




Go to the package source

Click on property
Add the new Source by click on Add Icon in green as highlighted



Enter the name and Source Url:
Name: Sitecore



I added the new package source called Sitecore as below:



Now, I am going to add the reference of Sitecore. Kernel DLL in my project:
  1.           Select the Sitecore as source
  2.           Search the Kernel DLL
  3.           Select the Sitecore.Kernel DLL
  4.           Select the project, on which you need to add the reference
  5.           Click on install







Oops, There is Error for framework compatibility

“Could not install package 'Sitecore.Kernel 8.2.170407'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.”

Make sure your project framework and package framework must be compatible, check the dependency of the package before installing.





And check my project framework version:


I just added another Sitecore reference > Sitecore.Analytics.Sql.NoReferences.
This DLL has no dependency, so I can quickly add this just for the demo.



Sitecore reference has been added in the project,



In the same way, we can also add the Sitecore reference which has dependencies for example Sitecore.Kernel



 Now check the refrence in the code.


  





I hope this article will help you for Sitecore DLLs references management using nuget

If you have any questions or concerns, please get in touch with me on twitter @sitecore_ashish or on Slack).

Happy Sitecoring

Friday, April 21, 2017

Sitecore Habitat Project Installation Tips and Errors




Have you installed Sitecore habitat? Not yet, just try it, it will not take more than an hour.

If you are lucky it will go in the first shot, but in real life we see lots of issues come at the execution time or run time, so here is the complexity of the Sitecore habitat project with GULP, NPM, Nodejs, Unicorn, configurations, your company network security etc.. Etc..

You have to be very careful at the time of Sitecore habitat installation, below are some tips regarding the Sitecore habitat installation:
  1. Installed the right version of Sitecore and WFFM that support the Sitecore Habitat Project, for example, currently, Sitecore 8.2 update 1 is compatible with habitat
  2. Change the configuration properly as per your Sitecore instance name, directory and source code path in gulpfile.js, gulp-config.js, z.Habitat.DevSettings.config File.
  3. Provide the required permission to your Sitecore instance > website folder
  4. Chose your project source code location at the time of node.js installation instead of the default location.
  5. Make sure you have installed the correct WFFM module with post-installation steps, try to avoid this installation using SIM.
  6. MongoDb service should be running in order to install WFFM. Otherwise, you will have an infinite loop in WFFM installation. 
  7. Disable xDB configuration if you are not using XDB.


I have invested a good amount of time in R&D in order to figure out the solutions of these silly problems/Errors. So don’t waste your time in these errors, it’s a very basic just be focused at the time of installation and follow the steps which are mentioned in the installation guide 


Here I am describing the 2 errors with a solution that I have faced recently during the Sitecore habitat installation:

1st error: Habitat Gulp Task > 03-Publish-All-Project Failed 

Build Error

 cmd: '"C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\MSBuild.exe" "C:\\projects\\Habitat\\src\\src\\Feature\\Person\\code\\Sitecore.Feature.Person.csproj" "/target:Build" /verbosity:minimal /toolsversion:14.0 /nologo /maxcpucount /property:Configuration="Debug" /property:DeployOnBuild="true" /property:DeployDefaultTarget="WebPublish" /property:WebPublishMethod="FileSystem" /property:DeleteExistingFiles="false" /property:publishUrl="C:\\inetpub\\wwwroot\\Habitat.dev.local\\Website" /property:_FindDependencies="false"' }[17:07:13] Build failed![17:07:13] 'Publish-Feature-Projects' errored after 16 s[17:07:13] Error: Command failed: "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" "C:\projects\Habitat\src\src\Feature\Person\code\Sitecore.Feature.Person.csproj" "/target:Build" /verbosity:minimal /toolsversion:14.0 /nologo /maxcpucount /property:Configuration="Debug" /property:DeployOnBuild="true" /property:DeployDefaultTarget="WebPublish" /property:WebPublishMethod="FileSystem" /property:DeleteExistingFiles="false" /property:publishUrl="C:\inetpub\wwwroot\Habitat.dev.local\Website" /property:_FindDependencies="false"    at ChildProcess.exithandler (child_process.js:204:12)    at emitTwo (events.js:106:13)    at ChildProcess.emit (events.js:191:7)    at maybeClose (internal/child_process.js:886:16)    at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)[17:07:13] '03-Publish-All-Projects' errored after 36 s[17:07:13] Error in plugin 'run-sequence(Publish-Feature-Projects)'Message:    Command failed: "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" "C:\projects\Habitat\src\src\Feature\Person\code\Sitecore.Feature.Person.csproj" "/target:Build" /verbosity:minimal /toolsversion:14.0 /nologo /maxcpucount /property:Configuration="Debug" /property:DeployOnBuild="true" /property:DeployDefaultTarget="WebPublish" /property:WebPublishMethod="FileSystem" /property:DeleteExistingFiles="false" /property:publishUrl="C:\inetpub\wwwroot\Habitat.dev.local\Website" /property:_FindDependencies="false"Details:    killed: false    code: 1    signal: null    cmd: "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" "C:\projects\Habitat\src\src\Feature\Person\code\Sitecore.Feature.Person.csproj" "/target:Build" /verbosity:minimal /toolsversion:14.0 /nologo /maxcpucount /property:Configuration="Debug" /property:DeployOnBuild="true" /property:DeployDefaultTarget="WebPublish" /property:WebPublishMethod="FileSystem" /property:DeleteExistingFiles="false" /property:publishUrl="C:\inetpub\wwwroot\Habitat.dev.local\Website" /property:_FindDependencies="false"Stack:Error: Command failed: "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" 

Are you facing the same error, you have downloaded the habitat project from the github and trying to set up locally on your machine?

Solution

The problem is the permission issue of C:\inetpub\wwwroot\habitat.dev.local\Website\packages.config File. Your system treating this file as unprotected 

Just right click >Unblock this file and try again, it will work.

“The file has come from another system and might be blocked by some application running on the current system“. This type of problem is apparent with the Windows Vista and Windows XP SP2 running with IE 7 where the zone related data is maintained in the system with NTFS partition”


2nd Error : '05-Sync-Unicorn’ Invoke-WebRequest Error - 'Sitecore.ContentSearch.IIndexOperationsEx'

Starting '05-Sync-Unicorn'
Sync-Unicorn: Preparing authorization for http://habitat.dev.local/unicorn.aspx?verb=Sync&configuration=
Error: Invoke-WebRequest :
Error:
 Could not load type 'Sitecore.ContentSearch.IIndexOperationsEx' from assembly 'Sitecore.ContentSearch, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null'.


Are you facing this error – Sitecore.contentSearch.IIndexoperationsEx?

I also faced the same error, I tried to figure the problem and found the solution

Solution

I installed the higher version of Sitecore 8.2 update 3, which is not compatible with habitat solution yet. Then I used the version mentioned in the wiki for Sitecore habitat (Sitecore 8.2 update 1)

Make sure you have installed the correct version of Sitecore and WFFM as below:
  1. Sitecore Experience Platform 8.2 Update 1 in Experience Platform or Experience Management mode
  2. Webforms for Marketers 8.2 Update 1 module. It should be installed prior to running Sync Unicorn gulp task.

Also, you may need to have running mongo instance when installing WFFM module, otherwise it may lead to never ending installation dialogue window.


I hope this article will help you during the installation of habitat project.

If you have any questions or concerns, please get in touch with me on twitter @sitecore_ashish or on Slack).

Happy Sitecoring and Habitat




Saturday, April 8, 2017

SETTING UP YOUR FIRST SITECORE HELIX APPLICATION FROM SCRATCH (BLANK SOLUTION)




A quick walk through of Sitecore helix on how to setup a Sitecore website based on Helix Architecture from scratch/blank solution

There is two solution to start with Helix:
  1. First to download the habitat demo site, cleanup and rename the project as per the requirement of the Project, and start working on the top up of the habitat.
  2. Second, create the solution from scratch means from blank solution.
I always preferred the second approach, because we can have full control of the solution and no need to understand the existing site logic and complexity.

So with the consideration of approach 2, here we are going to setup the site from scratch using Helix principle.

Hope you already have gone through the Sitecore helix principles, habitat documents, and training.

There was a lot of confusion regarding the Helix and Habitat terms, is it architecture? Principle? , design? Sitecore accelerator, what exactly is it?? It was a big question mark in the Sitecore community when Sitecore introduced this in the September 2016.

                                  




Helix is a set of overall design principles and conventions for Sitecore development.

Habitat is a real Sitecore project implemented on the Sitecore Experience Platform using Helix. It is an example that allows developers to see how Helix is applied and lets developers experience a project based on these principles.
I am not going to explaining in detail what is helix and habitat?

Refer the below Sitecore helix documentary link for more details:

Helix is all about the structure, structure, and structure, Helix says to split your solution into the logical and physical structure. Everything thing is a component here.

“A Visual Studio solution can host a number of different project types, such as Web Application projects, unit test projects, Team Development for Sitecore (TDS) projects, behavioural testing projects, Xamarin projects, etc., but modules are always grouped by their logical connection to a module – and never by type.”
   
On the very high level, implementation point of view, I divided the solution into 2 Main categories as below:
  1. Physical Structure: Create file system structure (physical structure)
  2. Logical/Virtual Structure: Create Visual Studio Solution structure (logical Structure)

Logical structure- Visual Studio Solution file Structure

For maximum discoverability, the structure of the Visual Studio solution must represent the layer and module structure, i.e. have solution folders for Project, Feature and Foundation layers and separate solution folders for each module. If there are any additional grouping of modules, these can have their own solution folders.

Then I set up the folder structure inside Visual Studio, adding the Project, Feature and Foundation folders. Solution folders are virtual, not physical.

Habitat example



NOTE: it is very important to sync your visual studio folder structure with file system structure, Need to follow some discipline to maintain the structure.

Physical Structure:

Helix solution has a fixed folder structure on disk.
The first thing is to set up the folder structure on disk. Helix recommend having everything in the folder named src. Habitat places the solution file outside this src folder. We will also follow the Habitats example

Folder structure will looks like:



Here I am starting with visual studio solution, I am taking one project reference let’s start with an Example called: DubaiRTX

Let’s do some workshop:

Create the file system folder called DubaiRTX anywhere in your filesystem, I created under C:/Websites/DubaiRTX

Create the blank solution named DubaiRTX and placed under folder named DubaiRTX


Then create the folder named src under DubaiRTX folder, we will place everything in this folder like below:

DubaiRTX
                Src
                                Project
                                Feature
                                Foundation


Create three folder under src, named Project, feature, Foundation.

  
                                




Now open the visual studio solution, and create the feature, Foundation, and project solution Folder in the solution.


NOTE: important point to sync the both structure (file system and visual studio solution) 






Now, we have three folder in the visual studio and same folder structure in the file system.
So, we have mapped the logical structure with physical structure as below:



Now create the multiple modules under: Foundation, Feature, and Project layer.



First we need to create the subfolder under each layer to categories in to modular format, means we should not create the project directly into the Project, feature, and Foundation layer as per the Helix principles.

Helix Principle based solution





So below is my requirement, I need to create some feature, foundation and main project website:

Project: Website
Feature: Category, navigation, RightTheme
Foundation: Bucket, Security

Let’s add projects into the solution

Add project into the Solution

  1. Go to visual studio solution
  2. Navigate to feature > Add solution folder named Navigation > Add new project under Navigation
  3. Select location of the project > click on Browse > select physical project feature path>
  4. Create folder named > navigation > and select that path to store the newly created project
  5. Named the project as Code.




Note: it’s very important to sync your project with proper file structure, that’s why we created folder named navigation in the feature folder and created the project by name called code, because we need to maintain all the project file under the code folder, and  it will create the folder directory into the >DubaiRTX>src>feature>Navigation>Code>project files.



Rename Project Name
  1. Rename the Project name from Code to DubaiRTX.Feature.Navigation as per Helix naming principles.
  2. Select Project > Right Click>Property>
  3. Also, rename the Assembly name and default namespace name as highlighted to DubaiRTX.Feature.Navigation and save the solution




Change the Webconfig setting

We need to make sure the Web.config file doesn’t get published, set the build action to ‘None’:
Go to the project > select the webconfig > right click > property > change build action to none




Do the same steps for all project you want to include in the solution:
I have done the same exercise using the above-mentioned steps for 6 projects.

As per the Helix principle

Projects are grouped together in a solution by the layer and module to which they belong.

Project Naming convention in Helix

A project, and assembly should be named in a namespace-like fashion with:

  1. The overall customer, partner or solution name
  2. The layer (optional for project layer modules)
  3. The logical module grouping (optionally)
  4. The module name

The logical Grouping of the project in the solution:



Feature Name: DubaiRTX.Feature.Navigation
This name is the combination of Company Name, layer module and logical module group as below:
Company Name: DubaiRTX
Layer: Feature
Logical Module: Navigation


Now check the Physical File Structure as well,

As per the Best practice, all code should be in src folder, then there are 3 layers named feature, Foundation, project.

In each layer, there is a module name, under module name, there is Code folder named where the actual code file is placed.

Below is the snapshot of the Physical structure of the helix based Sitecore project





Done!! oops Also need to add Test Project as well,

Let’s add test project in the solution as well,

Add test project in the Sitecore Helix solution
Add new project


Rename from Tests to DubaiRTX.Feature.Category.Tests







Change the property and assembly name:

                   


                          


Check the file structure of the Tests Project, everything should be under Tests folder 

                         





Now build the solution:




Yuppy, Build Successfully!!

What Next? Automation, Automation, Automation, all this step should be done by some script or other tools??? Yes, this is the wish of every developer :)



The very first thing came into my Mind for automation is PowerShell, yes I am the big fan of powershell, it is really very powerfull by its name as well.

Good news guys, Sitecore MVP, and the Most Active member of our Sitecore Community Akshay Sura already provided some solution for setting up helix from scratch using Powershell, here is the link:


I hope this article will help you to understand the basic concept’s to setup the Sitecore helix project from scratch.

If you have any questions or concerns, please get in touch with me on twitter @sitecore_ashish or on Slack).

Happy Sitecoring and Helix