0

I'm trying to add languages versions to an existing sitecore item i've created:

//creation of my item

Item item = Utils.CreatePage(cp.CreationFolder.Split('&')[0] + "/" + year, month, cp.PageTemplate);



 public static Item CreatePage(string creationFolder, int val, string pageTemplate)
    {
        using (new SecurityDisabler())
        {
            Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
            Item parentItem = masterDb.Items[creationFolder];
            TemplateItem template = masterDb.GetTemplate(pageTemplate);
            Item itemCreated = parentItem.Add(val.ToString(), template);
            Item item = masterDb.GetItem(itemCreated.ID);
            return item;
        }
    }

Then, after the call of this function is done, i call this function to add new versions and fill some fields:

Utils.EnableSubItemsSorting(item, "Reverse", cp.SubItemsSortingFolder, lstStrLanguages);

       public static void EnableSubItemsSorting(Item item, string sort, string SubItemsSortingFolder, List<string> lst_lg)
        {
            Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
            foreach (var lg in lst_lg)
            {
                using (new LanguageSwitcher(lg))
                {
                    var sitecoreItem = masterDb.GetItem(item.ID, Sitecore.Data.Managers.LanguageManager.GetLanguage(lg));
                    sitecoreItem.Editing.BeginEdit();
                    try
                    {
                        var sortItem = Factory.GetDatabase("master").GetItem(SubItemsSortingFolder).Children
                                       .FirstOrDefault(w => w.Name.Equals(sort));
                        sitecoreItem.Fields["__Subitems Sorting"].Value = sortItem.ID.ToString();
                    }
                    catch (Exception)
                    {
                        sitecoreItem.Editing.CancelEdit();
                    }
                    finally
                    {
                        sitecoreItem.Editing.EndEdit();
                        sitecoreItem = null;
                    }
                }
            }
        }

In sitecore i only see the item created in english and not in french for example, even if i'm absolutely sure that the lstStrLanguages contains both values ("fr" and "en").

Can anyone explain me what's wrong in my code ? Thank you for your help!

  • 1
    Have you tried wrapping the method call into using(new SecurityDisabler)? – grg Oct 11 '18 at 11:38
  • You mean the content of my EnableSubItemsSorting method ? – Slrg Oct 11 '18 at 11:48
  • Yes, that would work. Or a using statement around Utils.EnableSubItemsSorting() call. – grg Oct 11 '18 at 12:11
  • Great! I promoted the comment into an answer. Would you mind accepting it? – grg Oct 11 '18 at 12:58
  • 1
    i just did it ;) – Slrg Oct 11 '18 at 13:51
1

It's most likely related to missing permissions that prevent the code from creating item language versions. You could use SecurityDisabler just as you did in your first code sample:

using (new SecurityDisabler())
{
    Utils.EnableSubItemsSorting(item, "Reverse", cp.SubItemsSortingFolder, lstStrLanguages);
}

SecurityDisabler causes a segment of code to run in the context of a user with administrative rights, regardless of the context user. The code within the using statement block has full control of the entire system, and can take any action on any item or field.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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