Skip to main content

Posts

Showing posts from September, 2014

Read in a CSV file using C# Razor in Umbraco and Display the details:

@{
try {
var dataFile = Server.MapPath("~/App_Data/Contact-List.csv");
Array csvData = File.ReadAllLines(dataFile);

int dataStartsRow = 5;
int thisRow = 0;
<h2>Reading File Starting at 5th Row, because that is where the data Starts</h2>
foreach (string dataLine in csvData)
{
thisRow = thisRow  +1;
int columnInSpreadsheet = 0;
foreach (string dataItem in dataLine.Split(','))
{
if (thisRow >= dataStartsRow)
{
@dataItem <br />
}

}
}



}
catch{
<h2>Error:  Check to see if the file Contact-List.csv exists and is not open.</h2>
}

}






Umbraco Razor. Read Contents of A CSV File and Create nodes

The Script below reads the contents of a CSV Spreasheet in Razor, in Umbraco.

It's pretty specific for something I was working on, but there are bits of code that can be reused: Loading a CSV file in Razor, Reading the Contents of a CSV file in Razor, Creating Nodes in Umbraco from a CSV file in Razor, Deleting Nodes using Razor.


@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.cms.businesslogic.web

  <!-- This Script Reads from a CSV file in the Data App Folder -->
  <!-- It then finds Dates in the File and Creates New Event Nodes with the Date as part of the name-->
  <!-- There are 3 Types of Events, so it selects the type of event which is decided on by the column it is in-->
  <!-- See the CSV for examples-->


   <!-- The ETSR Mettings comes from a file "ETSRMeeting.csv" that should be stored in the App_Data folder.
    The file is read in and used in the Events Calander. -->
   <!-- Further Documentation at http://o…

Umbraco Razor: Check to see Whether the Date Field is Filled in and Only Display time if the Time is filled in.

Umbraco Razor: Check to see Whether the Date Field is Filled in and Only Display time if the Time is filled in.    and
Formating the Time in Umbraco using Razor

<p>Event Date: @Model.eventDate.ToString("dd MMMM yyyy")</p>

<!-- If the start time is filled in then write it out -->
@if(@Model.eventDate.ToString("H:mm") != "0:00")
{
<p>Start Time: @Model.eventDate.ToString("H:mm")</p>
}




<!-- If the End date is filled in write it out -->
@if (Model.endDate.ToString() != "")
{
<p>End Date: @Model.endDate.ToString("dd MMMM yyyy")</p>

}





<!-- If the end time is filled in then write it out -->
@if(@Model.endDate.ToString("H:mm") != "0:00" && Model.endDate.ToString() != "")
{
<p>End Time: @Model.endDate.ToString("H:mm")</p>
}