Copied to clipboard

Flag this post as spam?

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


  • Felipe Gubert 9 posts 89 karma points
    1 week ago
    Felipe Gubert
    0

    Shared Resources between Sites

    Hello, i need to put 3 different sites in the same umbraco install. Thats ok, its easy, no problem at all. The problem is that i want to share resources between them.

    Example:

    I want to have a Products section, where i can register all products for all the sites one time, and select (using a checkbox list), in where sites it should appear.

    I saw some videos of shared resources, but wasnt able to find a way to List products in each site, depending on the product should appear or not on that site. Any ideas?

    Thanks.

  • Jamie Freeman 36 posts 149 karma points
    1 week ago
    Jamie Freeman
    0

    What about moving the products outside of the site structure and using a set of Taxonomies - the checkbox list (applied to a product) to determine which product is displayed on which site.

    Then filter the iPublishedConent using a SurfaceController and Linq.

  • Felipe Gubert 9 posts 89 karma points
    1 week ago
    Felipe Gubert
    0

    Nice, is there any example or documentation of this working?

  • Jamie Freeman 36 posts 149 karma points
    1 week ago
    Jamie Freeman
    1

    Bear with me - working on this now.

  • Jamie Freeman 36 posts 149 karma points
    1 week ago
    Jamie Freeman
    100

    N.B For Future Reference - There is a better solution below involving Tags

    You need a DataType of Product - Checkbox list - which is a checkbox list with you website names within them.

    DataType

    And your Product Document Type must have the datatype.

    Product

    Here is an example Content Tree (I have placed the product in a Container DocumentType name Product Repository

    Content Tree

    N.B Product2 does not have Website1 selected

    The Website DocumentTypes contain the following code

    <h1>Products Avaialable on whidh Site (@Model.Content.Name)</h1>
    <a id="getProducts" href="javascript:void(0);">Get Products</a>
    <div id="productList">
        <span>see below</span>
        <ul></ul>
    </div>
    
    
    
    
    <script type="text/javascript">
        $("#getProducts").click(function () {
            GetProducts();
        });
    
        function GetProducts() {
            $.ajax({
                url: '/umbraco/Surface/ProductSurface/ByWebsite?websiteName=' + encodeURIComponent('@Model.Content.Name'),
                error: function (data, textStatus, jqXHR) {
                    alert(data.responseText);
                },
                success: function (data, textStatus, jqXHR) {
                    for(var i=0;i < data.products.length; i++) {
                        $('#productList ul').append('<li>' + data.products[i] + '</li>');
                    }
                }
            });
        }
    </script>
    

    and the SurfaceController is here

    public class ProductSurfaceController : SurfaceController
    {
        public ActionResult ByWebsite(string websiteName)
        {
            //Get ContentService
            var contentService = ApplicationContext.Services.ContentService;
            //Get DataTypeService
            var dataTypeService = ApplicationContext.Services.DataTypeService;
            //Get UmbracoHelper
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            //Get Root Items
            var rootContent = contentService.GetRootContent();
            //predicate - reusable - Not Name = "Product Repository" - you could pass this in
            bool predicate(IContent c) => c.Name.Equals("Product Repository", StringComparison.OrdinalIgnoreCase);
            if (rootContent.Any(predicate))
            {
                //Get Product Repository and Render Template
                var repo = rootContent?.Where(predicate).FirstOrDefault();
                var products = repo.Children().Where(c =>
                {
                    //Get Product Taxonomies Property
                    var properties = c.Properties.Where(p => p.Alias.Equals("productTaxonomies", StringComparison.OrdinalIgnoreCase));
                    if (properties.Any())
                    {
                        //If Exists Get Datatype
                        var property = properties.FirstOrDefault();
                        var dataType = dataTypeService.GetDataTypeDefinitionById(property.PropertyType.DataTypeDefinitionId);
                        //Get Pre Values of Datatype
                        var preValues = dataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id).FormatAsDictionary().Values;
                        var values = property.Value.ToString().Split(',').ToList();
                        //Compare Selected Valued Within Content Item and Website Name and Return Item If True
                        return preValues.Where(pv => values.Contains(pv.Id.ToString()) && pv.Value.Equals(websiteName, StringComparison.OrdinalIgnoreCase)).Any();
                    }
                    return false;
                }).Select(p => umbracoHelper.RenderTemplate(p.Id, p.Template.Id).ToString()).ToList();                
                return Json(new { products }, JsonRequestBehavior.AllowGet);
            }
            return new EmptyResult();
        }
    }
    

    which gives me the following output

    preview

  • Felipe Gubert 9 posts 89 karma points
    1 week ago
    Felipe Gubert
    0

    Tried this, i can see the controller is getting all the right items, but i got this error: Error

    Tried to associate a template with the Document type, but the error persists. What im missing?

    Thanks.

  • Jamie Freeman 36 posts 149 karma points
    1 week ago
    Jamie Freeman
    0

    Do the products not have their own template?

  • Felipe Gubert 9 posts 89 karma points
    1 week ago
    Felipe Gubert
    0

    Sorry, i have created in the first time without template. Recreated and now the error is gone, but the json object comes empty. What should i put in the template to return the full object?

    And i can see in the code that there is items to be returned, if i put @Umbraco.Field("infos") in the template, i got a string in return, but i wanted the full object.

    Thanks.

  • Felipe Gubert 9 posts 89 karma points
    6 days ago
    Felipe Gubert
    0

    Hey, still not able to return the full json object. What should i put on the template? Thanks

  • Jamie Freeman 36 posts 149 karma points
    1 week ago
    Jamie Freeman
    0

    Let me know how you get on - I saw an email about formatting, but cannot see the comment. Do you need further help ?

  • Felipe Gubert 9 posts 89 karma points
    1 week ago
    Felipe Gubert
    0

    The forum removed the format, but when i reloaded the page, it was ok. Im gonna test this solution, thanks very much.

    I just dont know where to create the SurfaceController ?

  • Jamie Freeman 36 posts 149 karma points
    1 week ago
    Jamie Freeman
    0

    Felipe, are you developing this with and IDE like Visual Studio or just the web interface ?

  • Felipe Gubert 9 posts 89 karma points
    1 week ago
    Felipe Gubert
    0

    Hey, im using visual studio community 2017.

  • Jamie Freeman 36 posts 149 karma points
    1 week ago
    Jamie Freeman
    0

    Great - So it either goes in the Controller folder within the website or within a referenced project in a folder location that makes sense to you.

    For Example

    Surface Controller Location

  • Jamie Freeman 36 posts 149 karma points
    1 week ago
    Jamie Freeman
    0

    And I'm an idiot - So I was looking into this last night and I have just found the whole Tag functionality in umbraco - much cleaner and simpler.

    Get Content By Tag

    Display Content By Tag

    So Add a Peoperty to a Product - in Available Editors select Tags, give it a Name like "Product Tags" and change the Tag Group to "products".

    Add this to you page

    <ul id="tagList" class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
        @{var tagList = Umbraco.TagQuery.GetAllContentTags("products").OrderBy(t => t.Text);}
        @foreach (var tag in tagList)
        {
            <li><a href="javascript:void(0)" data-tag="@tag.Text">@tag.Text</a></li>
        }
    </ul>
    
    <div id="taggedProductList">
        <span>see below</span>
        <ul></ul>
    </div>
    <script type="text/javascript">
        $("#tagList li a").click(function (e) {
            GetTaggedProducts($(this).data('tag'));
        });
    
        function GetTaggedProducts(tag) {
            $.ajax({
                url: '/umbraco/Surface/ProductSurface/ByTag?tag=' + encodeURIComponent(tag),
                error: function (data, textStatus, jqXHR) {
                    alert(data.responseText);
                },
                success: function (data, textStatus, jqXHR) {
                    $('#taggedProductList ul').empty();
                    for(var i=0;i < data.products.length; i++) {
                        $('#taggedProductList ul').append('<li>' + data.products[i] + '</li>');
                    }
                }
            });
        }
    </script>
    

    In your Surface Controller

    public ActionResult ByTag(string tag)
    {
        var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
        var publishedContent = Umbraco.TagQuery.GetContentByTag(tag);
        if (publishedContent.Count() > 0)
        {
            var products = publishedContent.OrderByDescending(i => i.CreateDate).Select(p => umbracoHelper.RenderTemplate(p.Id, p.TemplateId).ToString()).ToList();
            return Json(new { products }, JsonRequestBehavior.AllowGet);
        }
        return new EmptyResult();
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft