Sunday, August 6, 2017

Analyze your #Sitecore Media Library by MediaLibraryAnalyzer Module



Have you analyzed your Sitecore media library?

 
Not yet? This is the time to look your media library size, unused item etc.

Media library consumes more space in term of size and increase the volume of Items in sitecore tree, and over the period of time, it might have not used any more, thus we should clean up these Media library Items as these Items are not being used/referenced in any other content item in the Sitecore.

I got the same assignment, I had to clean up the media library in my project, so I analyzed the current media library folder and below is the summary:



I also wanted to capture the additional information when I ran the custom utility for Analysis my media library items,

  1. Media Item ID
  2. Media Item Name
  3. Media Item path
  4. Total Media Library Item Count
  5. Unused Media Library Count
  6. Uploaded Date
  7. Size of Individual Media Item

This information is really helping to my business user to analyzing the Media library data.

And also on the basis of this report, they can decide which Media item need to be delete or not?

In my case, below are the conditions for deleting the media library items
  • Media item must be unused means not referenced with any content item
  • Uploaded date should be older than 3 month 
  • Size of the media item should be more than 1MB


So,I have created one #Sitecore module called "MediaLibraryAnalyzer" which is responsible for the analyzing and cleanup of the media library.

Below are the functionalities of the MediaLibraryAnalyzer Module

  • Provides the user experience UI, where we can select the multiple input parameters, for example database name, Media Library Folder Path, Log File Path.
  • Provides the summary of the media library folder like Total media Item Count, Total Media Size, Total Unused Media Count, Total Unused Media Size, Total used Media Count, Total used Media Size
  • Provides the list of unused media item which contains various attributes like ItemName, Path, Media Type, Item Size, Created date, Modified date.
  • Filter the records based on created date to and from, media type, size 
  • Capability of archiving the unused Media Item, we can select the unused item and can move to a specific folder
  • Searching and paging the Media Item.


Below are the some Screen shot of the MediaLibraryAnalyzer Module

Summary of the media library




Filter the result based on several parameters



Move the un-used items to other folder:



Download the module:


How to use this Module:

  • Enter the URL your Sitecore instance name /Admin/MediaAnalyzer.aspx
  • Select the database where you need to run this utility
  • Enter the correct media folder path
  • Enter the Log file path for logging and make sure permission is being provided
  • Press on Go Button > 
  • All the un-used media item list will be listed
  • Click on Filter button >> further filter the result based on providing parameter like media type, size, created to and from the date.
  • Filter result will change in the text changed event
  • You can select multiple item and can perform 2 operation, whether delete it or move to another folder.
  • For delete the unused item, just select the items > press the Delete the selected item button


I hope this article will help you to analyze and cleanup the media library.

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

Happy Sitecoring





Friday, July 7, 2017

Create Your Rest/Web API Architecture with ASP .Net Core 1.1 from Scratch




Today I came up with Web API solution with .Net Core 1.1 from scratch, in this Article/POC we will focus more on architectural pattern of the solution means how to organise your solution architecture of the Web API with different layer, I try to follow some of the key design principles/Architecture Pattern/technology as below

  • Separate of concern
  • Repository pattern
  • Dependency Injection Containers (IOC)
  • N-Tier Project Architecture
  • Entity Framework Core
  • .Net Core Web API




Below is the high-level architecture and data flow diagram of Web API.




I am going to use Visual Studio 2017 RC and IDE, but you can also use Visual Studio 2015 as IDE. If you don’t have the template for ASP.NET Core, you need to install this template
Let’s take an example of ABCSoftware Company which need to expose the API to provides the related software Product information like ProductName, Price, details etc.

   
I quickly created sample database and table using below script

create database ABCSoftware
use ABCSoftware


CREATE TABLE PRODUCT(PRODUCTID INTEGER NOT NULL PRIMARY KEY,CODE VARCHAR(5),NAME VARCHAR(100),UNITPRICE NUMERIC(6,2) NOT NULL);


INSERT INTO PRODUCT VALUES(10,'AB123','ORACLE',1000);
INSERT INTO PRODUCT VALUES(20,'AB456','E-commerce',200.25);
INSERT INTO PRODUCT VALUES(30,'AB789','Online marketing',250.60);
INSERT INTO PRODUCT VALUES(40,'PQ123','School management',399);
INSERT INTO PRODUCT VALUES(50,'PQ456','OfficeManagement Software',1050);
INSERT INTO PRODUCT VALUES(60,'PQ789','Inventory Management Softare',2250.99);






Create Visual Studio Project using .NetCore Web API

Open VS2017 > create .Net core Web Application using (.NetCore),
 NOTE: we can either create a .NET Core Web Application which targets .NET Core or a .NET Core Web Application which targets the full .Net Framework.
If you target .NET Core, then you can run the app anywhere that supports .NET Core (Linux, Mac, and Windows).
You will also have to stick to third party libraries that support Core and you won’t be able to use some stuff that exists in the full framework and hasn’t been included in Core.
Alternatively, you can target the full .Net Framework. You’ll be limited to Windows (for hosting) but can use any third party library and everything that exists in the framework.
So I am creating .NET Core Web Application which targets .NETCore




Then select the webAPI template using ASP.NET Core 1.1  as below



Rename the Project name from ABCSoftware to ABCSoftware.API as below



Add the Different  Project Layer in the solution
Go to the solution > Add the New Project > Class Library(.Net Core)



We need to create the below Project layers ( .NetCore Class Library projects) in the solution with above steps:

ABCSoftware.Data: This is Data layer which is responsible for all database related tasks. The purpose of this layer is the direct access to the database. It’s the only layer responsible for communicating with the database. If some other layer wants to access the database, then this will be done through some of the classes (repositories) we will define in this project. Also, we will create the Repository/ DBContext in this layer only.

ABCSoftware.Model: We will keep all of our domain objects in this library.

ABCSoftware.Service: This Layer is responsible for all the operation that need to expose in the MVC controller, all the Business Logic will go in this layer. This layer will directly be communicated to the Controller.

ABCSoftware.Configuration: This library is where we will keep all the configuration and the settings of the project.

After adding all the above the above library in the solution, our project will look like below:



ADD Model in the ABCSoftware.Model Project

Add a new class in the ABCSoftware.Model project named Product as below:



class Product
    {
        [Key]
        public int PRODUCTID { get; set; }
        public string CODE { get; set; }
        public string NAME { get; set; }
        public decimal UNITPRICE { get; set; }
    }


Preparing DATA layer

Adding connection string and setting up DbContext
Got to ABCSoftware.API project > appsetting.json and add the connection string named DBConnection here as below:





Now, Let's add base context class which will inherit from DbContext class
Go to the DemoSoftware.Data project > Add one class called DatabaseContext as below:



Oops, DbContext is not resolved, Yes, Now Time to install Entity Framework.

ADD Entity Framework Core reference from Nuget in Data Library Project.
To install the package, just right click on the project ABCSoftware.Data and then, select "Manage NuGet package". The below dialog of Nuget Package Manager will pop up. In the Browse tab, there is a search box. Type “Microsoft.EntityFrameworkCore.SqlServer” and just click on "Install" button to install.

Note: make sure you select latest stable version, 1.1.2 is currently compatible with ASP.NET core 1.1



Now check the DatabaseContext Class, and resolve the DbContext with using Microsoft.EntityFrameworkCore;





Add the below code in the DatabaseContext Class

public class DatabaseContext : DbContext
    {
        public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
        {

        }

        public DbSet<Product> Product { get; set; }
    }


           
Adding Repositories in The Data Layer

Create one Folder in the DemoSoftware.Data project called Repositories
Add one class in the Repositories folder called ProductRespository and one interface called IProductRespository in the Repositories folder by Right click > Add > New item > Class




Go to ABCSoftware.Data project >  Add the project reference of ABCSoftware.Model



Go to IProductRepository Interface and add the below code
List<Product> GetProductList();

Go to ProductRepository implementation class and add the below code:
class ProductRepository : IProductRepository
    {

        private DatabaseContext _DbContext;

        public ProductRepository(DatabaseContext Dbcontext)
        {
            _DbContext = Dbcontext;
        }
        public IEnumerable<Product> GetProductList()
        {
           return _DbContext.Product;
        }
    }





Now our data layer is completed, repositories have been created, let's create the service layer.

ADD Service layer

Go to the ABCSoftware.Service Project and Add Folder Called Services > Under this folder create one class called ProductService and IProductService Interface as below:



Add data and Model project reference in the service project as below:



Add the below code in the IProductService Interface as below:

   public  interface IProductService
    {
        IEnumerable<Product> GetAllProduct();
    }

And add the below code in the ProductService implementation class:
class ProductService : IProductService
    {
        private IProductRepository _productRepository;

        public ProductService(IProductRepository productRepository)
        {
            _productRepository = productRepository;
        }
        public IEnumerable<Product> GetAllProduct()
        {
            return _productRepository.GetProductList();
        }
    }



This Service layer will call the repository class in the data layer as the above code.

And IproductService exposed only one method called GetAllProduct() and will call by controller class.

ADD Controller method
Now go to the web project >  Right Click > and add one controller class called ProductController:









Add Minimal Dependency
Add API Controller:








Go to Productcontoller > and Add the below Code:
public class ProductController : Controller
    {


        private IProductService _productService;

        public ProductController(IProductService productService)
        {
            _productService = productService;
        }

        [HttpGet]
        public IEnumerable<Product> GetProductData()
        {
            return _productService.GetAllProduct();
        }
    }



Now, everything is done!! Let’s Execute the API to test..
OOPS, we didn’t resolve the Repository and service layer dependency, let's resolve this first:

Add Dependency Injection in the Solution

Go to ABCSoftware.Configuration Project > Add one class file called DependencyResolver

Add Microsoft.EntityFrameworkCore.SqlServer reference in the Project
Add Service and Repository Project reference in the Project



Below is the code of the class

public static class DependencyResolver
    {
       
        public static void RegisterDependency(IServiceCollection _ServiceCollection,string connectionString)
        {
            //adding DB Dependecy Injection
            _ServiceCollection.AddDbContext<DatabaseContext>(options => options.UseSqlServer(connectionString));

            //Adding repository DI
            _ServiceCollection.AddTransient<IProductRepository, ProductRepository>();

            //Adding Service DI
            _ServiceCollection.AddTransient<IProductService, ProductService>();
        }
    }



Now go to ABCSoftware.API Project > Add Project Reference > ABCSoftware.Configuration

Go to the Startup.cs class and add the below code in the ConfigureServices() Method
      var con = Configuration.GetConnectionString("DBConnection");
            DependencyResolver.RegisterDependency(services,con);





Now all dependency will resolve through the common function called  RegisterDependency from DependencyResolver Class, This is a Just example of SOC(Separation of Concern) and also no need to get reference of Data Project directly in the ABCSoftware.API Project.

Now Build the solution and Run the Site using local IIS Express( using F5)
Check the result localhost/api/Product
If you want to host this API on Windows Server IIS, follow the below link



Testing ASP.net core Web API (HTTP request) Using Postman.
Download the POSTMAN APP, https//www.getpostman.com/ 
After download and install the Postman, Just check the HTTP Request as below:



I hope this article will help you to understand the Web API concepts using .Net Core.
If you have any questions or concerns, please get in touch with me on Twitter @sitecore_ashish or on Slack).


Friday, June 2, 2017

My #Sitecore #SUGCON 2017 - Amsterdam Experience




I got an opportunity to visit the Amsterdam city, one of the most beautiful place, especially for the nature lover like me, to attend the biggest Sitecore event #SUGCON 2017.

This event was an opportunity for me to meet all my virtual Sitecore Friends, colleagues, MVPs in Person.

#SUGCON started on 18th May 2017 at Postillion Convention Centre Amsterdam, starting time was 12 PM for registration.

I took the metro from Amsterdam centraal to Metro station Overamstel to attend this event, if I am not wrong the metro train number was 53.



I arrived there at 11.30AM from the back gate of the center, on the way of main hall stage, where #SUGCON organizers were busy in the preparation of the event, and Guess to whom I met First person in the event ???

Yes Mr. Akshay SURA, the most active Sitecore Community member. J
Then time came for #SUGCON Registration and we were all excited for the first session, the room was full and I attended the first session with UNA(we finally Meet)




Then Pieter Brinkmen started the very first session, he Invited Lars Nielsen for opening keynote, and he addressed the future technology of Sitecore: SXA, Robotics with Sitecore, Publishing Service, artificial intelligency with Sitecore etc.



It is very Good start with Artificial intelligence in Sitecore.
Then Bas Lijten and Rob Habrakens came on stage, talked about “A Futuristic showcase of the omnichannel capabilities of Sitecore XP” And introduced the first robotic system with Sitecore called ROBBIE, We all welcomed ROBBIE as a new member of the Sitecore family.



Then finally, I got the chance to meet with Lars Nielsen, Senior VP Technical Marketing and Cofounder of Sitecore and Pieter Brinkman, Director of Technical Marketing at Sitecore along with Bas and Rob.




Check what Lars and Peter think about the #SUGUAE( Sitecore user group UAE-Dubai),






Then I attended some interesting session as below:

DATA MINING, PREDICTION AND MACHINE LEARNING WITH THE XDB by Amanda Shiga

Download the complete slide from here


USING SPECFLOW TO DRIVE YOUR TEST AUTOMATION by Nathanael Mann
Download the complete slide from here

SITECORE XDB IN-DEPTH: HARNESSING CONTACTS, SESSIONS AND CLUSTERS by DMYTRO SHEVCHENKO



I attended this session with Alex van Wolferen



Then the time came for Sitecore Hackathon Award host by Akshay Sura.


Then, TAMAS VARGA hosted the most awaited Sitecore Award ceremony “MVP Award ceremony”



This was the great moments for me, as I received my First Sitecore MVP Award in #SUGCON.


 

This is how we completed the first day of #SUGCON, along with Networking and Dinner.



The second day of #SUGCON started with the STEPHEN POPE Presentation on WHAT’S NEW IN PUBLISHING SERVICE V2.0

“I can say this was one of the Best presentation in the #SUGCON 2017”
The hall was full of Sitecore Enthusiasts, and Stephen was explaining the amazing feature of Sitecore Publishing Service and showed some case study, how company utilized the performance gain through Sitecore publishing service.



Then I attended below session:

GLASS MAPPER - THINGS THAT I REALLY SHOULD AVE DOCUMENTED by MIKE EDWARDSH

He explained the power of Glass mapper and practical technique to use Glass Mapper.

SITECORE ON MICROSOFT AZURE PAAS by Christof Claessens


DevOps with Sitecore in the cloud Nick Hills
it was also a very interested session by Nick, he explained the EasyJet case study, and explained how EasyJet transformed their Sitecore platform from on-premise to cloud




Then I attended the Session on Sitecore User Group by Peter, Tamas
It was More Open discussion session, where we can share our experience of the Sitecore user group.


               

SITECORE AND JAVASCRIPT - FINALLY IN LOVE!

Then, Alex Shyba and Adam Weber introduced the new  Sitecore JavaScript Services (JSS Framework), they showed the demo with React.JS, this is something new for the Sitecore community and very cool stuff for frontend developers.

The session was full of fun when Alex fired the T-shirt by AIR Gun, who asked the Question.


 

Then #SUGCON Day-2 Session’s wrapped up with “THE SITECORE PHILOSOPHY” By JEPPE GRUE

Jeppe addressed how important the Sitecore community is! for Sitecore and any Product.

He shared the PHILOSOPHY and roadmap of the Sitecore.



This session took more time as expected, everyone was waiting to finish up the session at the end :)

Then lastly Peter Brinkman delivered the closing note, and that is how #SUGCON Event END. !! And check the gift from Sitecore to the #SUGCON organizers.

Yes, Pack of Different Beers!!




I am with #SUGCON organizers,



Then we enjoyed friet yes means French Fries (yes @VirtualAffairs sponsored the free fries for #SUGCON participation)



Then we got invitation for #SUGCON after party (including Drinks and Snacks) by Mirabeau

Thanks Mirabeau for such a Nice arrangement, I really liked the Mirabeau Office ambiance.




Then time to say bye to the beautiful City Amsterdam, #SUGCON was the very successful Sitecore event with full of knowledge, Networking, FUN!!!



I personally enjoyed a lot in the #SUGCON Event and Amsterdam!! I am looking forward to the next year!!