0

I am trying to insert a new country item under the path /sitecore/content/Corporate/MasterSite/MasterSite Dictionary/Country programmatically.

Is there any way to achieve the above requirement? enter image description here

1
  • It is possible 'programmatically' to create items. But you need to elaborate on your requirement like .Net or powershell code, one time import or periodic, where is this data coming from etc... Nov 28, 2023 at 14:54

3 Answers 3

0

To create it using Powershell script you can follow this code.

Dictionary Folder:

# Define the path where you want to create the dictionary folder
$dictionaryFolderPath = "/sitecore/system/Settings/Dictionary"

# Define the name for the dictionary folder
$folderName = "YourDictionaryFolder"

# Create the dictionary folder
$dictionaryFolder = New-Item -Path $dictionaryFolderPath -Name $folderName -ItemType "Dictionary Folder"
$dictionaryFolder.Editing.BeginEdit()
$dictionaryFolder["Key"] = $folderName
$dictionaryFolder.Editing.EndEdit()

Write-Host "Dictionary folder created: $folderName"

Dictionary Entry:

# Define the path where you want to create the dictionary entry
$dictionaryPath = "/sitecore/system/Settings/Dictionary"

# Define the key and value for the dictionary entry
$key = "YourDictionaryKey"
$value = "YourDictionaryValue"

# Create the dictionary entry
$dictionaryEntry = New-Item -Path $dictionaryPath -Name $key -ItemType "Dictionary Entry"
$dictionaryEntry.Editing.BeginEdit()
$dictionaryEntry["Key"] = $key
$dictionaryEntry["Phrase"] = $value
$dictionaryEntry.Editing.EndEdit()

Write-Host "Dictionary entry created: $key"

Hope this helps.

0

Creating an item programatically is pretty simple with the following code:

Sitecore.Data.Database masterDB =   Sitecore.Configuration.Factory.GetDatabase("master");

Item parentItem = masterDb.GetItem("<add the path to your parent item here>");

var template = masterDb.GetTemplate("/sitecore/templates/System/Dictionary/Dictionary entry");     

try
{
    Item newItem = parentItem.Add("CountryName", template);
    if (newItem!=null)
    {
        newItem.Editing.BeginEdit();
        newItem["Key"] = "YourKey";
        newItem["Phrase"] = "YourPhrase";
        newItem.Editing.EndEdit();
    }
}
catch
{
    newItem.Editing.CancelEdit();
}

Here we are getting the master db, getting the correct template for a dictionary entry. Then we get the parent item via the path and add a new item with the correct template.

This is just one way of doing it via C# code. You can also do this with PowerShell or the Item API. It will very much depend on where you need this code to run and on what action.

0

Here is a sample code,

using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Globalization;

public class DictionaryFolderCreator
{
    public void CreateDictionaryFolder(string dictionaryDomain, string folderName)
    {
        // Get the dictionary domain item
        Item dictionaryDomainItem = GetDictionaryDomainItem(dictionaryDomain);

        if (dictionaryDomainItem != null)
        {
            // Create a new folder under the dictionary domain
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                TemplateItem folderTemplate = GetFolderTemplate(); // Get the folder template (You can use your folder template here)

                // Create the folder under the dictionary domain
                Item folder = dictionaryDomainItem.Add(folderName, folderTemplate);
                if (folder != null)
                {
                    folder.Editing.BeginEdit();
                    folder.Fields["Title"].Value = folderName; // Set the title if needed
                    folder.Editing.EndEdit();
                }
            }
        }
    }

    private Item GetDictionaryDomainItem(string dictionaryDomain)
    {
        Database masterDb = Factory.GetDatabase("master"); // Change the database as needed
        Item dictionaryItem = masterDb.GetItem("/sitecore/system/Settings/Dictionary/" + dictionaryDomain);

        return dictionaryItem;
    }

    private TemplateItem GetFolderTemplate()
    {
        Database masterDb = Factory.GetDatabase("master"); // Change the database as needed
        Item folderTemplateItem = masterDb.GetItem("/sitecore/templates/System/Dictionary/Dictionary entry folder");

        return folderTemplateItem?.Template;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.