Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lewis Smith 208 posts 617 karma points c-trib
    Apr 17, 2018 @ 14:19
    Lewis Smith
    0

    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

  • Lewis Smith 208 posts 617 karma points c-trib
    Apr 17, 2018 @ 14:35
    Lewis Smith
    0

    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'

    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?

    Thanks, Lewis

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 17, 2018 @ 14:44
    Michaël Vanbrabandt
    0

    Hi Lewis,

    take a look at this example code:

    IContent page = null;
    page = Services.ContentService.CreateContent("Page Name here", this.Services.ContentService.GetById(1115), "productWatch", 0);
    page.SetValue("propOneAlias", propOneValue);
    page.SetValue("propTwoAlias", propTwoValue);
    
    Services.ContentService.Save(page);
    

    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.

    Hope this helps!

    /Michaël

  • Lewis Smith 208 posts 617 karma points c-trib
    Apr 17, 2018 @ 14:52
    Lewis Smith
    0

    Hi Michael,

    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?

    Thanks, Lewis

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 17, 2018 @ 14:55
    Michaël Vanbrabandt
    1

    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

  • Lewis Smith 208 posts 617 karma points c-trib
    Apr 17, 2018 @ 14:58
    Lewis Smith
    0

    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

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 17, 2018 @ 15:00
    Michaël Vanbrabandt
    0

    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

  • Lewis Smith 208 posts 617 karma points c-trib
    Apr 17, 2018 @ 15:53
    Lewis Smith
    0

    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

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 17, 2018 @ 16:55
    Michaël Vanbrabandt
    1

    Hi Lewis,

    The return type of CreateMedia is an IMedia object. So you can do just something like:

     var media = MediaService.CreateMedia();
     int mediaId = media.Id;
    

    Hope this helps!

    /Michaël

  • Lewis Smith 208 posts 617 karma points c-trib
    Apr 18, 2018 @ 07:59
    Lewis Smith
    0

    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:

    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.

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Apr 18, 2018 @ 07:20
    Richard Soeteman
    0

    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

  • Lewis Smith 208 posts 617 karma points c-trib
    Apr 18, 2018 @ 08:32
    Lewis Smith
    0

    Hi Richard,

    I will have a look into this and consider it.

    Thanks, Lewis

Please Sign in or register to post replies

Write your reply to:

Draft