Mass Load New Product into database

Posted by Community Admin on 05-Aug-2018 02:33

Mass Load New Product into database

All Replies

Posted by Community Admin on 07-Dec-2011 00:00

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.

Posted by Community Admin on 10-Dec-2011 00:00

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;
        

Regards,
Venkata Koppaka
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 09-Mar-2012 00:00

Hi,

This worked very well but..

All my images defaulted to "draft" status.  How can they be set to "publish"?

Posted by Community Admin on 13-Mar-2012 00:00

Hello Victor,

Please set the image's approval workflow state to published using the code snippet below.

img.ApprovalWorkflowState = "Published"

You can use the above line right after setting Image's alternative text.

Let us know if you run into any other issues.

Greetings,
Venkata Koppaka
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 23-Aug-2012 00:00

Hi All,

I am new in Sitefinity, where should i put this code please?

Thanks in advance.

Posted by Community Admin on 01-Sep-2012 00:00

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 


Greetings,
Steve
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed