Thursday, April 23, 2020

Create sitecore items from json


1. Create a new Web Form in VS solution
2. In the code file:

Keyword: create items in code

using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;

namespace Globus.Foundation.Extensions
{
    public partial class IconsImport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
            //get icon styles folder under Avalon Datasource
            Sitecore.Data.Items.Item parentItem = masterDB.GetItem("{F6B79270-AC40-4E38-B7FF-FC8F6A80A931}");
            var template = masterDB.GetTemplate("{F848A4AE-3A28-4E86-9268-D2262B853343}");

            JObject iconsObj = JObject.Parse(File.ReadAllText(@"c:\Transfers\fa-icon-list.json"));
         

            foreach(var group in iconsObj.Children())
            {
                string groupName = JObject.Parse("{" + group.ToString() + "}").Properties().First().Name;
                Sitecore.Data.Items.Item set = parentItem.Children.FirstOrDefault(x => x.Name == groupName);             
                JArray items = (JArray)group.Children().First();
                string prefix = "far fa-";


                if (groupName.ToString() == "brands")
                {
                    prefix = "fab fa-";
                }
                else if(groupName.ToString() == "solid")
                {
                    prefix = "fas fa-";
                }

                foreach (var iconStyle in items)
                {
                    string value = UppercaseWords(iconStyle.ToString().Replace("-", " "));

                    using (new Sitecore.SecurityModel.SecurityDisabler())
                    {
                        Sitecore.Data.Items.Item newItem = set.Add(value, template);
                        string temp = (prefix + value).ToLower();

                        try
                        {
                            if (newItem != null)
                            {
                                newItem.Editing.BeginEdit();
                                newItem["Value"] = prefix + iconStyle.ToString();
                                newItem.Editing.EndEdit();

                            }
                        }
                        catch
                        {
                            //newItem.Editing.CancelEdit();
                        }
                    }
                }
            }
        }

        static string UppercaseWords(string value)
        {
            char[] array = value.ToCharArray();
            // Handle the first letter in the string.
            if (array.Length >= 1)
            {
                if (char.IsLower(array[0]))
                {
                    array[0] = char.ToUpper(array[0]);
                }
            }
            // Scan through the letters, checking for spaces.
            // ... Uppercase the lowercase letters following spaces.
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i - 1] == ' ')
                {
                    if (char.IsLower(array[i]))
                    {
                        array[i] = char.ToUpper(array[i]);
                    }
                }
            }
            return new string(array);
        }
    }
}


And restrict multilist field to one selected item. In Validation field on a template:

^.{38,116}$” , here, “^.{N,X}$”, 

No comments:

Post a Comment