Skip navigation

Sharing content between projects

When managing multiple projects under one subscription, you might want to share some of the information from one project with another. Learn how you can leverage what Kentico Kontent has to offer to make your projects cooperate smoothly.

Table of contents

    Having a set of information that is shared across multiple projects has its advantages. For one, you can avoid replication of the data and save yourself the hassle of duplicating your content. Another benefit would be not adding more complexity when managing such information in separate projects. 

    For example, imagine you have multiple websites under your subscription and each of them utilizes the same data for localization strings. In this case, it would be easier to have this type of data in one shared project and pull that data whenever needed.

    Decisions, decisions

    It's always good to think about your particular use case before you start implementing a custom solution. When you have a set of content items from one project that you want to reuse in another project, you have a couple of options.

    You might want to keep the related content items separate and only link them in your new project. In this case, using a custom element would be the preferred choice. This element lets you link published content items from a different project directly in the UI.

    Still, sometimes it's better to have all the information in the same project even if it means duplicating content. This is especially true if you know you'll make changes to the related content and want to keep it at hand. Or if the project you're reusing data from is somehow restricted and you don't want content contributors to make changes to it. In situations like these, it might be helpful to clone an existing project that already contains the required data and then tweak the project to fit your needs.

    If you have specific requirements for the format of your shared data or would like to pull content from multiple projects at once, it's best to do so programmatically. While taking advantage of the Kentico Kontent Delivery SDKs, you can retrieve content from multiple Delivery Clients.

    As you can see, it all comes down to how you want to work with the content later on. This article looks closely at two different options – one using a custom element in the UI and the other one using SDKs to pull content from multiple projects.

    Linking content from a different project

    If you need to link content items from a different project right in the UI, you can leverage a custom element designed specifically for this use case.

    To add a custom element:

    1. From the app menu, choose .
    2. Open the content type that should hold the custom element.
    3. Drag in a Custom element element from the right and give it a name.
    4. Add the following hosted code URL:  https://kentico.github.io/kontent-custom-element-samples/Item-selector/item-selector.html
    5. Fill in the JSON parameters.
      • Specify the project containing the items you want to link by adding its projectid.
      • (Optional) Filter content items you want to retrieve using the filter parameter.
    6. Click Save changes.

    When using this custom element, there are a few things to keep in mind:

    • You can only link published content items.
    • If you plan on using the sample element in your own production project, we recommend you clone the repository. By doing so, you will not be affected by possible changes made to the custom element in the future.

    Once configured, the custom element could look similar to this one:

    Example of a custom element used in a content item

    For more details on how to work with custom elements, see Integrating your content editing features.

    Getting content from multiple Delivery clients

    Whether you're using the Kentico Kontent Delivery .NET SDK, Delivery JavaScript SDK, or another SDK, the principle of pulling the data from two projects stays the same. You first create two individual instances of DeliveryClient, supplying each with the corresponding project ID. Then you retrieve content items from both clients and get the response. In the code samples below, the response is an array containing items from both projects. But you can always change the format depending on how you want to work with the items.

    If you don't want to get all the content items from the projects, you can always filter the results by, for example, specifying a content type. Learn more about filtering your content.

    • Java
    // Tip: Find more about Java/JavaRx SDKs at https://docs.kontent.ai/java import com.github.kentico.kontent.delivery; // Initializes the content delivery clients DeliveryClient client1 = new DeliveryClient("975bf280-fd91-488c-994c-2f04416e5ee3"); DeliveryClient client2 = new DeliveryClient("368d5707-fb05-0146-fc0a-f85c5d1a264e"); List<NameValuePair> params = DeliveryParameterBuilder.params() .build(); // Gets content items from both projects ContentItemsListingResponse listingResponse1 = client1.getItems(); ContentItemsListingResponse listingResponse2 = client2.getItems(); //Pass the length parameter and combine arrays Object[] combined = new Object[listingResponse1.length + listingResponse2.length]; System.arraycopy(listingResponse1, 0, combined, 0, listingResponse1.length); System.arraycopy(listingResponse2, 0, combined, listingResponse1.length, listingResponse2.length);
    // Tip: Find more about Java/JavaRx SDKs at https://docs.kontent.ai/java import com.github.kentico.kontent.delivery; // Initializes the content delivery clients DeliveryClient client1 = new DeliveryClient("975bf280-fd91-488c-994c-2f04416e5ee3"); DeliveryClient client2 = new DeliveryClient("368d5707-fb05-0146-fc0a-f85c5d1a264e"); List<NameValuePair> params = DeliveryParameterBuilder.params() .build(); // Gets content items from both projects ContentItemsListingResponse listingResponse1 = client1.getItems(); ContentItemsListingResponse listingResponse2 = client2.getItems(); //Pass the length parameter and combine arrays Object[] combined = new Object[listingResponse1.length + listingResponse2.length]; System.arraycopy(listingResponse1, 0, combined, 0, listingResponse1.length); System.arraycopy(listingResponse2, 0, combined, listingResponse1.length, listingResponse2.length);
    • JavaScript
    // Tip: Find more about JS/TS SDKs at https://docs.kontent.ai/javascript const KontentDelivery = require("@kentico/kontent-delivery"); const deliveryClient1 = new KontentDelivery.DeliveryClient({ projectId: "975bf280-fd91-488c-994c-2f04416e5ee3" }); const deliveryClient2 = new KontentDelivery.DeliveryClient({ projectId: "368d5707-fb05-0146-fc0a-f85c5d1a264e" }); const allContentItems: ContentItem[] = []; deliveryClient1.items() .toObservable() .subscribe(response => { // Add items to shared array allContentItems.push(...response.items); }); deliveryClient2.items() .toObservable() .subscribe(response => { // Add items to shared array allContentItems.push(...response.items); });
    // Tip: Find more about JS/TS SDKs at https://docs.kontent.ai/javascript const KontentDelivery = require("@kentico/kontent-delivery"); const deliveryClient1 = new KontentDelivery.DeliveryClient({ projectId: "975bf280-fd91-488c-994c-2f04416e5ee3" }); const deliveryClient2 = new KontentDelivery.DeliveryClient({ projectId: "368d5707-fb05-0146-fc0a-f85c5d1a264e" }); const allContentItems: ContentItem[] = []; deliveryClient1.items() .toObservable() .subscribe(response => { // Add items to shared array allContentItems.push(...response.items); }); deliveryClient2.items() .toObservable() .subscribe(response => { // Add items to shared array allContentItems.push(...response.items); });
    • C#
    // Tip: Find more about .NET SDKs at https://docs.kontent.ai/net using Kentico.Kontent.Delivery; // Initializes the first content delivery client IDeliveryClient client1 = DeliveryClientBuilder .WithProjectId("368d5707-fb05-0146-fc0a-f85c5d1a264e") .Build(); // Initializes the second content delivery client IDeliveryClient client2 = DeliveryClientBuilder .WithProjectId("975bf280-fd91-488c-994c-2f04416e5ee3") .Build(); // Gets content items from both projects // Note: Using the <object> generic parameter produces strongly typed objects, based on "system.type" DeliveryItemListingResponse<object> client1Response = await client1.GetItemsAsync<object>(); DeliveryItemListingResponse<object> client2Response = await client2.GetItemsAsync<object>(); // Concatenate the results IReadOnlyList<ContentItem> items = client1Response.Items.Concat(client2Response.Items).ToArray();
    // Tip: Find more about .NET SDKs at https://docs.kontent.ai/net using Kentico.Kontent.Delivery; // Initializes the first content delivery client IDeliveryClient client1 = DeliveryClientBuilder .WithProjectId("368d5707-fb05-0146-fc0a-f85c5d1a264e") .Build(); // Initializes the second content delivery client IDeliveryClient client2 = DeliveryClientBuilder .WithProjectId("975bf280-fd91-488c-994c-2f04416e5ee3") .Build(); // Gets content items from both projects // Note: Using the <object> generic parameter produces strongly typed objects, based on "system.type" DeliveryItemListingResponse<object> client1Response = await client1.GetItemsAsync<object>(); DeliveryItemListingResponse<object> client2Response = await client2.GetItemsAsync<object>(); // Concatenate the results IReadOnlyList<ContentItem> items = client1Response.Items.Concat(client2Response.Items).ToArray();
    • PHP
    <?php // Tip: Find more about PHP SDKs at https://docs.kontent.ai/php // Defined by Composer to include required libraries require __DIR__ . '/vendor/autoload.php'; use Kentico\Kontent\Delivery\DeliveryClient; // Initializes the content delivery clients $client1 = new DeliveryClient("975bf280-fd91-488c-994c-2f04416e5ee3"); $client2 = new DeliveryClient("368d5707-fb05-0146-fc0a-f85c5d1a264e"); // Gets content items from both projects $items1 = $client1->getItems(); $items2 = $client2->getItems(); $items = array_merge($items1, $items2);
    <?php // Tip: Find more about PHP SDKs at https://docs.kontent.ai/php // Defined by Composer to include required libraries require __DIR__ . '/vendor/autoload.php'; use Kentico\Kontent\Delivery\DeliveryClient; // Initializes the content delivery clients $client1 = new DeliveryClient("975bf280-fd91-488c-994c-2f04416e5ee3"); $client2 = new DeliveryClient("368d5707-fb05-0146-fc0a-f85c5d1a264e"); // Gets content items from both projects $items1 = $client1->getItems(); $items2 = $client2->getItems(); $items = array_merge($items1, $items2);
    • TypeScript
    // Tip: Find more about JS/TS SDKs at https://docs.kontent.ai/javascript import { ContentItem, DeliveryClient } from "@kentico/kontent-delivery"; const deliveryClient1 = new DeliveryClient({ projectId: "975bf280-fd91-488c-994c-2f04416e5ee3" }); const deliveryClient2 = new DeliveryClient({ projectId: "368d5707-fb05-0146-fc0a-f85c5d1a264e" }); const allContentItems: ContentItem[] = []; // Note: Using the <ContentItem> parameter produces strongly typed objects deliveryClient1.items<ContentItem>() .toObservable() .subscribe(response => { // Add items to shared array allContentItems.push(...response.items); }); deliveryClient2.items<ContentItem>() .toObservable() .subscribe(response => { // Add items to shared array allContentItems.push(...response.items); });
    // Tip: Find more about JS/TS SDKs at https://docs.kontent.ai/javascript import { ContentItem, DeliveryClient } from "@kentico/kontent-delivery"; const deliveryClient1 = new DeliveryClient({ projectId: "975bf280-fd91-488c-994c-2f04416e5ee3" }); const deliveryClient2 = new DeliveryClient({ projectId: "368d5707-fb05-0146-fc0a-f85c5d1a264e" }); const allContentItems: ContentItem[] = []; // Note: Using the <ContentItem> parameter produces strongly typed objects deliveryClient1.items<ContentItem>() .toObservable() .subscribe(response => { // Add items to shared array allContentItems.push(...response.items); }); deliveryClient2.items<ContentItem>() .toObservable() .subscribe(response => { // Add items to shared array allContentItems.push(...response.items); });

    Getting content from three or more different projects is fairly similar to getting content from two projects. You can apply the principles described above to suit your needs.

    What's next?

    In this tutorial, you've learned about the different approaches you can take when sharing content between projects.