Copied to clipboard

Flag this post as spam?

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


  • Glenn Smith 18 posts 99 karma points
    Dec 10, 2018 @ 21:27
    Glenn Smith
    0

    Creating instances of Document Types Dynamically

    I am evaluating Umbraco and would like to know if there are any limitation related to the dynamic creation of unpublished instances of document types.

    I'd like to be able to create new unpublished instances of a product document type in code so that as new products enter the market, we have a job which uses the Umbraco api to add them to the Umbraco content database so that when back office is opened the new products appear with the existing products.

    A content manager can then modify and publish the new products so they appear on the site. I would think this would be pretty straight forward but have not read of any examples.

    Any assistance would be appreciated.

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Dec 10, 2018 @ 22:14
    Søren Gregersen
    0

    Hi,

    I would suggest you create an endpoint in your website, that you can then post the new products to.

    The solution here would allow an outside service to post (json) data to yoursite.com/Umbraco/Products/Publish

    Example code (just a mockup):

    [PluginController("Products")]
    public class ProductsController : UmbracoApiController{
        [HttpPost]
        public void Publish(ProductDto product){
             var productListDocumentId = 1234;
             var content = Umbraco.UmbracoContext.Application.Services.ContentService;
             var p = content.CreateContent(product.Name, productListDocumentId, "ProductDetails");
             // p.SetValue for each property....
             content.Save(p);
        }
    }
    
    public class ProductDto {
        public string Sku{get;set;}
        public string Name{get;set;}
        public string Description{get;set;}
        public decimal Price{get;set;}
    }
    

    You would/should also add some security to this.

    hth :)

Please Sign in or register to post replies

Write your reply to:

Draft