CustomAdminPages/Reading CSVorJSON/ImportingContent in Sitecore:
Recently I got a requirement in project to create sitecore admin pages as well as to import content in sitecore using csv and json files .
As i was much familiar with powershell script importing content using powershell was easy for me .But this was i have to achieve using c# .
So let’s see how i have achieved this step by step :
Step 1- First step of the problem to create sitecore admin page
As we all know all the sitecore admin pages are stored under our sitecore instance sitecore/admin folder and all are aspx pages .
In my project solution i have created a project under foundation as per helix and added aspx page under the folder structure sitecore/admin/BuildingsContentImport.aspx
Important thing to keep in mind while creating admin page is -
in aspx page it should be like :
in code behind file where we have to write our logic to read json or csv file it should inherit from Sitecore.sitecore.admin.AdminPage and your file will look like as follows :
As you see CheckSecurity(fasle) i have passed false as my requirement was only logged in user can see this admin page .
Step 2 : Uploading csv or Json file to sitecore media upload folder which is present in webroot of sitecore instance.
Added the file upload control in aspx file and in code behind wrote the logic to upload file in sitecore upload folder present in webroot .
Step 3: Here comes the step where i was little nervous , As a c# developer it was easy for me but while seeing the structure of csv file i was little confused
so here is the magic nuget package which helped to me read my csv file without any issues CsvHelper by JoshClose .
Added the reference using nuget in my project and Written a method to read csv file and converted this in to my c# model object .
In above screenshot Buildings is my model class name which is having all the headers of csv file.
Step 4 : Import Content in sitecore using csv file this was the final goal of my task .So i created all the content in sitecore programmatically using c# .
In Screenshot below i have commented each step of creating sitecore item starting from getting the master db , template name , location of item to be created , new Sitecore.SecurityModel.SecurityDisabler() and Editing.BeginEdit() and Editing.EndEdit();
Step 5 : As the requirement was to read json file as well so written a method to read json file as well.
purpose of this post is to giving back to the community and help someone who is looking similar kind of task so instead of getting information in scattered way , collectively it is available in single post .
Happy Learning…