Sitecore Publishing an Item in Multiple Languages

We have a multi-lingual Sitecore site with an inRiver PIM connector. The connector is responsible for adding and updating the PIM entities (products, images, pdfs, category nodes, etc.) in the Sitecore content tree. It does not publish products or images or category nodes after importing data from the PIM by default and relies on scheduled publishing to the Web Database. But the client wanted an automatic publish in certain scenarios so we created a Sitecore publishing helper class that the connector can call when it needs to publish.

I borrowed some code from Brian Pedersen to do this. I had to adapt it a bit to handle multiple languages but after my changes it did not work as I expected. I was trying to create the Publisher and PublishOptions once and change the Language in a loop. But it didn’t work as I expected. It wasn’t publishing my secondary languages. I had to move the Publisher and PublishOptions instantiations within the loop. I wonder if the original Language is somehow held on to by the PublishOptions even after I explicitly set it.

TLDR: premature optimization is the root of all evil

Not Working Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public static class SitecoreHelper
    {
        public static void PublishItem(Item item, bool clearCache = false)
        {
            // The publishOptions determine the source and target database,
            // the publish mode and language, and the publish date
            var webDb = Database.GetDatabase("web");
           
            PublishOptions publishOptions = new PublishOptions(item.Database,
                                                                  webDb,
                                                                  PublishMode.SingleItem,
                                                                  item.Language,
                                                                  System.DateTime.Now);  // Create a publisher with the publishoptions

            Publisher publisher = new Publisher(publishOptions);

            // Choose where to publish from
            publisher.Options.RootItem = item;
       
            // Publish children as well?
            publisher.Options.Deep = true;

            // Publish mode
            publisher.Options.Mode = PublishMode.Full;

            foreach (var lang in item.Languages)
            {
                //set the language
                publisher.Options.Langauge = lang;

                // Do the publish!
                publisher.Publish();
                Utils.LogAction(string.Format("Published item {0} ({1}) to web in {2}", item.ID.Guid.ToString("B"), item.Name, lang.GetDisplayName()), ApiType.Sitecore, "SitecoreHelper.PublishItem");
            }

            if(clearCache)
            {
                ClearSitecoreDatabaseCache(item.Database);
            }
        }

        private static void ClearSitecoreDatabaseCache(Database db)
        {
            db.Caches.ItemCache.Clear();
            db.Caches.DataCache.Clear();

            //Clear prefetch cache
            foreach (var cache in CacheManager.GetAllCaches())
            {
                if (cache.Name.Contains(string.Format("Prefetch data({0})", db.Name)))
                {
                    cache.Clear();
                }
            }
        }

Working Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public static class SitecoreHelper
    {
        public static void PublishItem(Item item, bool clearCache = false)
        {
            // The publishOptions determine the source and target database,
            // the publish mode and language, and the publish date
            var webDb = Database.GetDatabase("web");
           
            foreach (var lang in item.Languages)
            {
                PublishOptions publishOptions = new PublishOptions(item.Database,
                                                                  webDb,
                                                                  PublishMode.SingleItem,
                                                                  lang,
                                                                  System.DateTime.Now);  // Create a publisher with the publishoptions

                Publisher publisher = new Publisher(publishOptions);

                // Choose where to publish from
                publisher.Options.RootItem = item;

                // Publish children as well?
                publisher.Options.Deep = true;

                publisher.Options.Mode = PublishMode.Full;
                publisher.Options.Language = lang;

                // Do the publish!
                publisher.Publish();
                Utils.LogAction(string.Format("Published item {0} ({1}) to web in {2}", item.ID.Guid.ToString("B"), item.Name, lang.GetDisplayName()), ApiType.Sitecore, "SitecoreHelper.PublishItem");
            }
            if(clearCache)
            {
                ClearSitecoreDatabaseCache(item.Database);
            }
        }

        private static void ClearSitecoreDatabaseCache(Database db)
        {
            // clear html cache
           
            db.Caches.ItemCache.Clear();
            db.Caches.DataCache.Clear();

            //Clear prefetch cache
            foreach (var cache in CacheManager.GetAllCaches())
            {
                if (cache.Name.Contains(string.Format("Prefetch data({0})", db.Name)))
                {
                    cache.Clear();
                }
            }
        }