ASP.NET, CMS

Sitecore Custom Publisher

In Sitecore, both developers and content authors often comes across the situation where they need to publish multiple items which do not belongs to same parent. But unfortunately, Sitecore does not provide option for this.

Let’s consider following scenario.

 

Node A

                    Child a1

                    Child a2

                    Child a3

                   Child a4

Node B

                  Child b1

                  Child b2

                 Child b3

 

Here, I want to publish Node A, child a1, child a2, child a3 and child b2. To do this, we need to manually select and publish each item one after the other.

 

To overcome this, I created a custom publisher(simple aspx page), using which Sitecore user can select and publish multiple items from the Sitecore tree in a single go.

First create a simple aspx page with empty divs to hold Sitecore tree and languages list.

Aspx:

Add reference to jstree js and css files.

    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jstree/3.3.8/themes/default/style.min.css" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
    <script src="//cdnjs.cloudflare.com/ajax/libs/jstree/3.3.8/jstree.min.js"></script>

Html:

  <div class="container">
        <h1 style="text-align: center;">Sitecore Custom Publisher </h1>
        <div class="row">
            <div class="col-md-8 border">
                <h3>Select items to publish </h3>
                <div class="row">
                    <div class="col-md-6">
                        <div id="jsTreeTest">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <button id="toggle" type="button" class="btn btn-primary">Toggle</button>
                        <div class="form-group">
                            <label for="name">Enter Item Ids in comma separated format</label>
                            <textarea class="form-control" rows="3" id="itemdIdstextarea" placeholder="Item Ids"></textarea>

                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4 border">
                <h3>Select language(s)</h3>
                <div id="jsLanguageTree">
                </div>
                <button id="publish" type="button" class="btn btn-primary" style="margin: 20px;">Publish</button>


            </div>
        </div>
    </div>

 

 

Then create the webmethods to get Sitecore tree, list of available languages and to publish the selected items on clicking the publish button

 [WebMethod]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public static List<JsTree> GetSitecoreTree()
 {
     Database _datase = Sitecore.Configuration.Factory.GetDatabase("master");
     List<JsTree> items;

     try
     {
          var rootItem = _datase.GetItem("/sitecore/content");
          // code to get list of items to build sitecore tree
        
      }
     catch (Exception ex)
     {
          Sitecore.Diagnostics.Log.Error("Error while generating tree node ", ex.Message);
          return null;
      }
 }

 

Get list of languages available in Sitecore:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<JsTree> GetLanguages()
{
      Database _datase = Sitecore.Configuration.Factory.GetDatabase("master");

      var languages = LanguageManager.GetLanguages(_datase).ToList();

      return languages.Select(x => new JsTree
      {
          id = x.CultureInfo.Name,
          text = x.Name,
          icon = x.GetItem(_datase).GetIconSrc(),
          children = null,
       }).ToList();
}

 

Publish items on clicking publish button:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static void PublishItems(List<string> ids, List<string> cultureInfos)
{

       Database[] targetDatabases = { Database.GetDatabase("web") };

       // get languages
       var sourceLanguages = new List<Language>();

       foreach (var cultureinfo in cultureInfos)
       {
           sourceLanguages.Add(LanguageManager.GetLanguage(cultureinfo));
       }

      
      //Publish your items here
}

 

Now lets render the Sitecore tree and languages using ajax calls.

Get Sitecore tree on page load:

        $("#jsTreeTest").jstree({
            "core": {
                "data": {
                    "url": "CustomPublisher.aspx/GetSitecoreTree",
                    "type": 'POST',
                    "dataType": 'JSON',
                    "contentType": 'application/json;',
                    "data": function (node) { return {}; }
                }
            },
            "plugins": ["checkbox", "selected"]

        });

 

Get languages list on page load:

        $("#jsLanguageTree").jstree({
            "core": {
                "data": {
                    "url": "CustomPublisher.aspx/GetAvailableLanguages",
                    "type": 'POST',
                    "dataType": 'JSON',
                    "contentType": 'application/json;',
                    "data": function (node) { return {}; }
                }
            },
            "plugins": ["checkbox"]
        });

 

 

Publish items on clicking publish button:

        $('#publish').on("click", function () {
            var selectedItems = $('#jsTreeTest li[aria-selected="' + true + '"]');
            var selectedLanguages = $('#jsLanguageTree li[aria-selected="' + true + '"]');
            var itemlist = [];
            var languagelist = [];
            for (var i = 0; i < selectedItems.length; i++) {
                itemlist.push(selectedItems[i].id);
            }

            for (var i = 0; i < selectedLanguages.length; i++) {
                languagelist.push(selectedLanguages[i].id);
            }
			
			var itemIdstextarea = document.getElementById("itemdIdstextarea").value.split(',');
			
			itemlist = itemlist.concat(itemIdstextarea);
			
			
            $.ajax({
                type: "POST",
                url: "CustomPublisher.aspx/PublishItems",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify({ ids: itemlist, cultureInfos: languagelist }),
                success: function (msg) {
                    alert("success");
                }
            });

        });
    });

 

 

Using this custom publisher user can either select the items from the sitecore tree on the left or enter comma(,) separated item ids in Items Ids text area.

Once the items are selected, select language to publish from Select language(s) list on the right side of the screen and click Publish

All the items that are selected in the Sitecore tree and the Items id textarea will be published.

 

References:

JS Tree: https://www.jstree.com/

 

 

About The Author