I'm creating a website using Tea Commerce and I need to add products from a database.
In essence, we have a products master database that is updated every hour, I need to scrape this database, find any new products and add these to the website. I also need to check if any products have sold, if so then set the stock level to 0 meaning the product cannot be purchased.
Tea Commerce seems to work in the way where a product is actually the same as a general node, meaning it should be as simple as adding a new page (node) and just defining where on the website I want this page to sit.
For example, Adding a Rolex watch under the page Rolex which is found under watches.
this.Services.ContentService.CreateContentWithIdentity("Page name here", this.Services.ContentService.GetById(1115), "productWatch");
So this creates a page with no content in which is a good start, I now need a way to add content to this page i have just created (would be amazing if I could create the page and add the content in one go)
Could someone point me to which service I need to add content to a node programmatically?
The class model you need is IContent. Then to set your properties use the method SetValue() and as a final you save the content object using the Services.ContentService.Save() method.
Cracking! It's adding a page I have changed the .Save to .SaveAndPublishWithStatus but im getting an error in my logs of
2018-04-17 15:50:22,145 [P115440/D2/T11] INFO Umbraco.Core.Services.ContentService - Content 'new rolex' with Id '0' could not be published because of invalid properties.
Is this because there are required properties that do not have a value?
yes, looking into the source code and I see it does do checks on the properties values. So yes if you have mandatory properties you need to fill them in.
So I have the media item being created now. After creating the image in the media section how could i get this items id in order to pass this into the page publish?
The Id im getting from the item.id is returning an id that is different to the item that was uploaded!
Below is the code im using to successfully upload but return the wrong id!!
--
Model:
public class ImageModel
{
public string Title { get; set; }
public string Url { get; set; }
public int ParentId { get; set; }
}
--
Controller
var Media = new ImageModel();
Media.Title = PageTitle + Reference + "main";
Media.ParentId = GetParentMappedImage("Rolex");
Media.Url = "https://i.pinimg.com/originals/c5/63/19/c56319f33466a4304f41fc1ad810ebb1.jpg";
//running task to upload image.
var mediaId = UploadImageToMedia(Media.Title, Media.ParentId, "Image", Media.Url);
//mediaId is the Id I need to pass into the content for the image.
//Content is then uploaded using this mediaId as the ID for the image.
public int UploadImageToMedia(string MediaItemTitle, int ParentId, string MediaTypeAlias, string ImageUrl)
{
var mediaId = 0;
var mediaService = Services.MediaService;
var request = WebRequest.Create(ImageUrl);
var webResponse = request.GetResponse();
var responseStream = webResponse.GetResponseStream();
if(responseStream != null)
{
var originalImage = new Bitmap(responseStream);
var path = Server.MapPath("/Media-Auto/" + MediaItemTitle + ".jpg");
originalImage.Save(path, ImageFormat.Jpeg);
FileStream fileStream = new FileStream(path, FileMode.Open);
var fileName = fileStream.Name;
var mediaImage = mediaService.CreateMedia(MediaItemTitle, ParentId, "Image");
mediaImage.SetValue("umbracoFile", fileName, fileStream);
mediaId = mediaImage.Id;
mediaService.Save(mediaImage);
responseStream.Dispose();
webResponse.Dispose();
originalImage.Dispose();
}
return mediaId;
//this is returning an id that doesnt match the Id of the image that was uploaded....
}
Thanks,
Lewis
EDIT:
GetParentMappedImage gets the id for the parent where the image wants to be placed. Just a simple switch that gets the Id based on the title passed in.
Might not be the answer you are looking for. But CMSImport PRO has teacommerce support out of the box, can import media and can be scheduled. https://soetemansoftware.nl/cmsimport
Might be worth to check out? I am happy to generate a trial license for you (mail [email protected] in case you are interested to try)
Add page using SQL
Hi all,
I'm creating a website using Tea Commerce and I need to add products from a database.
In essence, we have a products master database that is updated every hour, I need to scrape this database, find any new products and add these to the website. I also need to check if any products have sold, if so then set the stock level to 0 meaning the product cannot be purchased.
Tea Commerce seems to work in the way where a product is actually the same as a general node, meaning it should be as simple as adding a new page (node) and just defining where on the website I want this page to sit.
For example, Adding a Rolex watch under the page Rolex which is found under watches.
Any help would be greatly appreciated!
Thanks, Lewis
So i have found https://our.umbraco.org/documentation/reference/management/services/contentservice which I have used to add a page under the category of 'Rolex'
So this creates a page with no content in which is a good start, I now need a way to add content to this page i have just created (would be amazing if I could create the page and add the content in one go)
Could someone point me to which service I need to add content to a node programmatically?
Thanks, Lewis
Hi Lewis,
take a look at this example code:
The class model you need is
IContent
. Then to set your properties use the methodSetValue()
and as a final you save the content object using theServices.ContentService.Save()
method.Hope this helps!
/Michaël
Hi Michael,
Cracking! It's adding a page I have changed the
.Save
to.SaveAndPublishWithStatus
but im getting an error in my logs ofIs this because there are required properties that do not have a value?
Thanks, Lewis
Hi Lewis,
yes, looking into the source code and I see it does do checks on the properties values. So yes if you have mandatory properties you need to fill them in.
Hope this helps!
/Michaël
Thanks again. In regards to attaching an image so .SetValue() with the image being set, is this expecting the image URL, Id etc etc etc?
thanks, Lewis
Hi Lewis,
no problem, glad to help here.
Yes you will first need to create a Media object and save this in order to have an id which you then fill in into the property of your content.
Have a look at this thread here:
https://our.umbraco.org/forum/developers/api-questions/47792-Create-Media-Programmatically-in-V7
Api docs about the media service:
https://our.umbraco.org/apidocs/csharp/api/Umbraco.Core.Services.MediaService.html
The method you need is the
CreateMedia()
Hope this helps.
/Michaël
Hi Michael,
So I have the media item being created now. After creating the image in the media section how could i get this items id in order to pass this into the page publish?
Thanks, Lewis
Hi Lewis,
The return type of
CreateMedia
is anIMedia
object. So you can do just something like:Hope this helps!
/Michaël
Hi Michael,
The Id im getting from the item.id is returning an id that is different to the item that was uploaded!
Below is the code im using to successfully upload but return the wrong id!!
--
Model:
--
Controller
Thanks, Lewis
EDIT:
GetParentMappedImage
gets the id for the parent where the image wants to be placed. Just a simple switch that gets the Id based on the title passed in.Hi Lewis,
Might not be the answer you are looking for. But CMSImport PRO has teacommerce support out of the box, can import media and can be scheduled. https://soetemansoftware.nl/cmsimport
Might be worth to check out? I am happy to generate a trial license for you (mail [email protected] in case you are interested to try)
Best,
Richard
Hi Richard,
I will have a look into this and consider it.
Thanks, Lewis
is working on a reply...