Mass Load New Product into database
This may be a simple questions. Just starting with sitefinity. How do you mass load product information including product pictures into the database. I have a list of about 4,400 products that need to be loaded and I do not want to do each product one at a time through the front end. Is there a convenient tool to use for this. Your help is appreciated. At 4,400 products at 3 minutes each would take about 13,200 minutes. This is roughly 220 hours of work.
Hi Crad,
You can use the below snippets to do product import, we do not have a utility tool or anything like that, but we do have a very simple to use API. Below snippets use our API to do the import. Let us know if you have any other questions -
private
Product CreateProduct(CatalogManager manager,
string
productTypeName,
string
title,
string
description,
string
sku,
decimal
price,
DateTime? expirationDate,
int
bestSelling,
bool
featured,
bool
isShippable,
bool
IsUSCanadaTaxable,
bool
IsVatTaxable,
double
rating,
bool
isOnSale, DateTime? saleEndDate, DateTime? saleStartDate,
decimal
? salePrice,
double
? weight,
string
imagePath,
string
imageAltText,
string
[] departmentTitles,
string
[] tags,
bool
isActive)
if
(manager.GetProducts().Where( x => x.Title == title).SingleOrDefault() !=
null
)
return
null
;
// Product already exists
productTypeName = productTypeName.ToLower();
ProductType productType = manager.GetProductTypes().Where(x => x.Title.ToLower() == productTypeName).SingleOrDefault();
var p = manager.CreateProduct(productType.ClrType);
p.Title = title;
p.AssociateBuyerWithRole = Guid.Empty;
p.BestSelling = bestSelling;
p.DateCreated = DateTime.Now;
p.Description = description;
p.ExpirationDate = expirationDate;
p.Featured = featured;
p.IsOnSale = isOnSale;
p.IsShippable = isShippable;
//p.IsUSCanadaTaxable = IsUSCanadaTaxable;
p.IsVatTaxable = IsVatTaxable;
p.Price = price;
p.IsActive = isActive;
p.Rating = rating;
p.SaleEndDate = saleEndDate;
p.SaleStartDate = saleStartDate;
p.SalePrice = salePrice;
p.Sku = sku;
p.UrlName = title.ToLower().Replace(
" "
,
"_"
);
//p.VatTax = new Tax();
//p.VatTaxId = Guid.Empty;
p.Visible =
true
;
p.Weight = weight;
if
(departmentTitles !=
null
)
TaxonomyManager tx = TaxonomyManager.GetManager();
HierarchicalTaxonomy departments = tx.GetTaxonomies<HierarchicalTaxonomy>().Where(x => x.Name ==
"Departments"
).Single();
foreach
(
string
departmentTitle
in
departmentTitles)
Taxon taxon = departments.Taxa.Where(x => x.Title == departmentTitle).SingleOrDefault();
p.Organizer.AddTaxa(
"Department"
, taxon.Id);
if
(tags !=
null
)
TaxonomyManager tx = TaxonomyManager.GetManager();
HierarchicalTaxonomy tagTaxonomies = tx.GetTaxonomies<HierarchicalTaxonomy>().Where(x => x.Name ==
"Tags"
).Single();
foreach
(
string
tag
in
tags)
Taxon taxon = tagTaxonomies.Taxa.Where(x => x.Title == tag).SingleOrDefault();
p.Organizer.AddTaxa(
"Tag"
, taxon.Id);
manager.Provider.RecompileItemUrls(p);
manager.SaveChanges();
if
(
string
.IsNullOrWhiteSpace(imagePath) ==
true
)
return
p;
FileInfo file =
new
FileInfo(imagePath);
if
(file ==
null
)
return
p;
string
path = file.DirectoryName;
string
filename = file.Name;
LibrariesManager lm = LibrariesManager.GetManager();
Album album = lm.GetAlbum(LibrariesModule.DefaultImagesLibraryId);
Telerik.Sitefinity.Libraries.Model.Image img = lm.CreateImage();
img.AlternativeText = imageAltText;
//img.Description = "description";
var extension = file.Extension;
var imageTitle = file.Name;
if
(extension.Length > 0)
imageTitle = imageTitle.Substring(0, imageTitle.Length - extension.Length);
img.Parent = album;
img.Title = imageTitle;
img.UrlName = imageTitle.ToLower().Replace(
' '
,
'-'
);
lm.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(img);
using
(var fileStream = file.OpenRead())
lm.Upload(img, fileStream, file.Extension);
lm.Publish(img);
lm.SaveChanges();
ProductImage pi =
new
ProductImage();
pi.AlbumId = album.Id;
pi.Album = album.Title.Value;
pi.Id = img.Id;
pi.Width = img.Width;
pi.Height = img.Height;
pi.Title = imageTitle;
pi.Url = img.Url;
pi.AlternativeText = img.AlternativeText;
pi.FileName = img.FilePath;
pi.FileSize = file.Length.ToString();
p.Images.Add(pi);
manager.SaveChanges();
ContentLinksManager contentLinksManager = ContentLinksManager.GetManager();
IEnumerable<ContentLink> contentLinks = contentLinksManager.GetContentLinks()
.Where(cl => cl.ParentItemId == p.Id && cl.ComponentPropertyName ==
"ProductImage"
).ToList();
IEnumerable<Guid> persistedIds = contentLinks.Select(cl => cl.ChildItemId);
List<ProductImage> imagesToAdd = p.Images.Where(i => !persistedIds.Contains(i.Id)).ToList();
var createdContentLinks =
new
List<ContentLink>();
foreach
(ProductImage productImage
in
imagesToAdd)
Telerik.Sitefinity.Libraries.Model.Image img2 = lm.GetImage(productImage.Id);
ContentLink contentLink = contentLinksManager.CreateContentLink(
"ProductImage"
, p, img2);
createdContentLinks.Add(contentLink);
contentLinksManager.SaveChanges();
return
p;
Hi,
Hello Victor,
Please set the image's approval workflow state to published using the code snippet below.
img.ApprovalWorkflowState =
"Published"
Hi All,
I am new in Sitefinity, where should i put this code please?
Thanks in advance.
Hello,
Please refer to this blog post on importing products.
http://www.sitefinity.com/blogs/stevemiller/posts/12-05-10/sitefinity_ecommerce_ndash_importing_products_into_sitefinity_ecommerce.aspx