Search code examples
c#umbracoumbraco7

Is there a way of having editable shared content between pages on Umbraco?


I'm relatively new to Umbraco (but have read through the docs), so I apologise in advance.

I have a partial view on a website that I want to make editable (e.g. using page properties in Umbraco admin). The properties for this partial view will remain consistent on every page it's on. Is there a way I can edit those properties in one place or is there a concept of a shared component? At the moment the alternative is going through every single page and setting the properties wherever the partial view is displayed, which isn't a great solution for the number of pages it will appear on.

I have looked around, but I haven't found anything that points me in the right direction. I'm using Umbraco 7.6.6, so I'm a little restricted in what I can do.


Solution

  • The Umbraco.com website has some great documentation on getting started and setting up Umbraco. Umbraco TV is great for getting up to speed quickly too.

    This is a HUGE topic, which requires a conceptual explanation of how Umbraco works, and the documentation is the best place to start, but I'll try to summarise the relevant elements.

    What you're looking for is a Document Type. Document Types represent the data used by Umbraco pages and are stored in the database.

    You create a Document Type for your page with an accompanying Razor Template from inside Umbraco. When the page is rendered by Umbraco on the front-end the Razor view is loaded, it references the Document Type (Model) and inserts the data. The Razor views can be created from inside Umbraco or using Visual Studio. Naturally you can include partial views inside the Razor Template.

    Once you've created your page(s) in the Umbraco back-end and accompanying Document Type(s), which includes Data Types (inputs - media pickers, URL pickers, text inputs, Etc.), these can reference what is called a Composite Document type which can be included\shared in more than one page, a Composite Document Type is where you'd include properties which need to appear in more than one page but which have different data, such as SEO properties.

    For Global properties I tend to add these to the root or landing page, then reference it in the relevant Razor page using LinkQ, inserting the properties where and when I need them in the page. An example of this would be including something like the Google Analytics ID in the master page. A Composite Document Type wouldn't be need for that.

    The Umbraco documentation on querying gives this example of getting all the Name (default properties) of each page found under the root node, which may help as a starting point for referencing Document Type properties.

    // Get the children of the first content item found in the root
    @foreach (var child in Umbraco.ContentAtRoot().First().Children) {
        <a href="@child.Url">@child.Name</a>
    }