Skip to content

Instantly share code, notes, and snippets.

@esitefinity
Created May 10, 2012 22:52
Show Gist options
  • Save esitefinity/2656432 to your computer and use it in GitHub Desktop.
Save esitefinity/2656432 to your computer and use it in GitHub Desktop.
Sitefinity Ecommerce CSV Upload Widget
<%@ control language="C#" autoeventwireup="true" codebehind="CsvUploader.ascx.cs"
inherits="SitefinityWebApp.ProductsUpload.CsvUploader" %>
<h1 class="sfBreadCrumb" id="sfToMainContent">
<span>Product Upload:</span></h1>
<div class="sfMain sfClearfix">
<div class="sfContent">
<div class="sfAllToolsWrapper">
<div class="sfAllTools">
<%--<ul id="commandButtons" class="sfActions">
<li class="sfMainAction"></li>
</ul>--%>
</div>
</div>
<div class="sfWorkArea">
<div id="messageControl" runat="server" class="sfMessage sfGridViewMessage">
</div>
<div>
<h2>
Upload Csv File</h2>
<br />
<br />
<asp:fileupload id="FileUpload1" runat="server" />
<asp:requiredfieldvalidator id="rq4" runat="server" controltovalidate="FileUpload1"
errormessage="* Required" forecolor="Red" setfocusonerror="true" display="Dynamic"
validationgroup="UploadCSV"></asp:requiredfieldvalidator>
<asp:regularexpressionvalidator id="regexValidator" runat="server" controltovalidate="FileUpload1"
errormessage="Only CSV file is allowed" validationgroup="UploadCSV" validationexpression="(.*\.([Cc][Ss][Vv])$)">
</asp:regularexpressionvalidator>
<br />
<br />
<asp:button id="btnUpload" runat="server" validationgroup="UploadCSV" text="Submit"
cssclass="RadUploadSubmit" onclick="btnUpload_Click" />
</div>
</div>
</div>
</div>
// Sitefinity Ecommerce Import products from CSV file.
// make sure you have created a folder called upload in the root of your web site
// you must also be logged-in to the Sitefinity backend for this product importer to work.
// You must also have CSVHelper package installed. Install Nuget http://nuget.codeplex.com into Visual Studio as an extension
using System;
using System.Linq;
using System.Web.UI;
using Telerik.Sitefinity.Samples.Ecommerce.ProductUpload.Util;
using Telerik.Sitefinity.Samples.Ecommerce.ProductUpload;
namespace SitefinityWebApp.ProductsUpload
{
public partial class CsvUploader : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
UploadCsvFileAndTriggerProductImport();
}
private void UploadCsvFileAndTriggerProductImport()
{
string filePath = UploadFileAndGetFilePath();
UploadManager uploadManager = new UploadManager(new UploadConfig { NumberOfColumns = 11 });
uploadManager.ImportProductsFromCsvFile(filePath);
}
private string UploadFileAndGetFilePath()
{
string folderPath = Server.MapPath("~/Upload");
IoHelper.ValidateFolderExsistence(folderPath);
string filePath = folderPath + @"\" + FileUpload1.FileName.ToString();
IoHelper.ValidateFileExsistence(filePath);
FileUpload1.SaveAs(filePath);
return filePath;
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace SitefinityWebApp.ProductsUpload {
public partial class CsvUploader {
/// <summary>
/// messageControl control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlGenericControl messageControl;
/// <summary>
/// FileUpload1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.FileUpload FileUpload1;
/// <summary>
/// rq4 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.RequiredFieldValidator rq4;
/// <summary>
/// regexValidator control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.RegularExpressionValidator regexValidator;
/// <summary>
/// btnUpload control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btnUpload;
}
}
Title ProductTypeTitle Description Url Price Weight SKU ImageFilePath IsActive CustomProperty1 CustomProperty2
My Product Title CustomProductType This is my product description 999.99 2 IPN4 C:\demos\ProductUpload\Images\Image2.jpg|C:\demos\ProductUpload\Images\Image1.jpg FALSE CustomData1-Row2 CustomData2-Row2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment