2017년 11월 20일 월요일

Sitecore 9.0 Dynamic Placeholder 사용법

사이트코어는 9.0을 릴리즈하면서, DynamicPlaceholder라는 새로운 SitecoreHelper를 소개하였다.

기존의 @Html.Sitecore().Placeholder("key")와는 달리, @Html.Sitecore().DynamicPlaceholder("Key", optional parameters)는 새로운 Placehoder를 좀 더 효율적으로 생성 및 관리할수 있으며, Placeholder Key이름에 렌더링 아이템 아이디와 인덱스를 추가하여 똑같은 Placeholder key를 중보긍로 사용할수 있도록 하였다.

예: placeholderName-RenderingID-Count
content-{4C770DD9-F840-45AB-A22F-8CCAAC42F646}-0

먼저, 아래는 DynamicPlaceholder Method에서 사용가능 한 Parameter의 목록을 나열하였고, 각각 어떻게 사용되어지는지 알아보도록 하자.


DynamicPlaceholder(string placeholderName, int count = 1, int maxCount = 0, int seed = 0)
DynamicPlaceholder(string placeholderName, TagBuilder chrome, int count = 1, int maxCount = 0, int seed = 0)
DynamicPlaceholder(string placeholderName, Func<DynamicPlaceholderRenderContext, TagBuilder> chromeResolver, int count = 1, int maxCount = 0, int seed = 0)
DynamicPlaceholder(string placeholderName, Func<HtmlString, HtmlString> outputModifier, int count = 1, int maxCount = 0, int seed = 0)
DynamicPlaceholder(DynamicPlaceholderDefinition definition)


  1. DynamicPlaceholder(string placeholderName, int count = 1, int maxCount = 0, int seed = 0)

    기본 Placeholder Method처럼 @Html.Sitecore().DynamicPlaceholder("placeholder name") 처럼 사용할 수 있지만, 추가적으로 기본 선택적인 Parameter인 count, maxCount, 그리고 seed를 사용함으로써 해달 Placeholder를 제한적으로 사용할 수가 있다.

    @Html.Sitecore().DynamicPlaceholder("content", count:2)
    

    - count: 사용하고 싶은 Placeholder 수를 정함.
    - maxCount: 맥시멈 Placeholder 수를 정함. 예를들어, count가 "5"이고 maxCount를 "3"으로 정했다면, 페이지에서는 maxCount인 3개의 Placeholder만 나타남.
    - seed: Placeholder의 카운티은 "0"에서 시작되며, 특정하게 시작하는 수는 정하고 싶다면 seed를 사용하면 됨


  2. DynamicPlaceholder(string placeholderName, TagBuilder chrome, int count = 1, int maxCount = 0, int seed = 0)

    첫번째, 메소드의 형식과 달리 이번 메소드는 TagBuilder 오브젝트를 패스하여 새로운 HTML 마크업을 생성할수가 있다.

    @{
        TagBuilder newTag = new TagBuilder("div");
    
        newTag.GenerateId("unique-id-here");
        newTag.AddCssClass("custom-class-name-here anotheer-class-here");
        newTag.InnerHtml = "This DIV is built by TagBuilder";
    }
    
    @Html.Sitecore().DynamicPlaceholder("content", newTag, count: 2, seed: 5)
    
    // 또는,
    
    @Html.Sitecore().DynamicPlaceholder("content", 
                                        output =>
                                        {
                                            newTag = new TagBuilder("Div");
                                            newTag.GenerateId("unique-id-here");
                                            newTag.AddCssClass("custom-class-name-here another-class-here");
                                            return newTag;
                                        }, 
                                        count: 2,
                                        seed: 5)
    

    위의 예제처럼, 새로운 태크 생성을 위한 Argument를 메소드에 전달하였으며 seed를 "5"로 세팅함으로써 Placeholder 카운터가 "5"부터 시작한다.

    HTML Preview Output 
    <div class="custom-class-name-here another-class-here" id="unique-id-here"></div>
    <div class="custom-class-name-here another-class-here" id="unique-id-here"></div>
    

  3. DynamicPlaceholder(string placeholderName, Func<DynamicPlaceholderRenderContext, TagBuilder> chromeResolver, int count = 1, int maxCount = 0, int seed = 0)

    두번째 예제에서의 문제점은 똑같은 HTML 마크업을 생성할수는 있으나, 마크업의 Attribute (클래스 이름 또는 아이디 이름)등을 Unique하게 생성할수가 없다. 똑같은 클래스 이름이나 아이디를 사용하면 Javascript 이벤트를 불러오는데어서 Conflict 문제가 생길수가 있다. 사이트코어 Presentation 네임스페이스의 DynamicPlaceholderRenderContext 클래스를 사용하여, Placeholder의 속성값을 가져올수가 있다.

    // @View
    <div class="row">
        @functions
        {
            TagBuilder CreateColumn(DynamicPlaceholderRenderContext context)
            {
                var col = new TagBuilder("div");
                col.GenerateId("column-id-" + context.Index);
                col.AddCssClass("col-lg-" + 12/context.PlaceholdersCount));
                return col;
            }
        }
        @Html.Sitecore().DynamicPlaceholder("content", CreateColumn, count: 3, seed: 20, maxCount: 4)
    </div>
    

    위의 예제는 context.PlaceholderCount을 Argument에 세팅한 Count의 값 "3"의 불러와 새로 생성되는 "<div>" 마크업의 클래스이름 "col-lg-{count}"으로 정하도록 하였다. 또한, Unique한 ID를 생성하여 context.Index값을 차려로 나열하도록 만들었다.



    HTML Preview Output
    <div class="row">
        <div class="col-lg-4" id="column-id-0"></div>
        <div class="col-lg-4" id="column-id-1"></div>
        <div class="col-lg-4" id="column-id-2"></div>
    </div>
    

  4. DynamicPlaceholder(string placeholderName, Func<HtmlString, HtmlString> outputModifier, int count = 1, int maxCount = 0, int seed = 0)

    이번에도 3번째 예제와 비슷하게 Placeholder 속성값을 전달하여 새로운 HTML 마크업 또는 값을 생성하여, 원하고자하는 Placeholder의 마크업을 생성할수있다.

    @Html.Sitecore().DynamicPlaceholder("content", 
                                        (input, context) => new HtmlString(String.Format("<div class=\"custom-class-{1}\">{0}</div>", 
                                                            input, 
                                                            context.DynamicKey)
                                        ), 
                                        count: 50,
                                        maxCount: 3, 
                                        seed: 50)
    

    이번 예제에서는 해당 Placeholder의 DynamicKey의 값을 사용하였고, 값은 PlaceholderName-UniqueRenderingID-Count 값으로 HTML 마크업에 나타난다.

    <div class="custom-class-content-{4C770DD9-F840-45AB-A22F-8CCAAC42F646}-50"></div>
    <div class="custom-class-content-{4C770DD9-F840-45AB-A22F-8CCAAC42F646}-51"></div>
    <div class="custom-class-content-{4C770DD9-F840-45AB-A22F-8CCAAC42F646}-52"></div>
    

  5. DynamicPlaceholder(DynamicPlaceholderDefinition definition)

    마지막으로 DynamicPlaceholderDefinition 오브젝트는 지금까지 설명한 모든 옵션을 통합한 클래스이다. 새로운 오브젝트에 Placeholder 이름을 정하고, 필요한 인덱스값을 정해주며, Placeholder의 속성값을 HTML 마크업의 Unique한 값으로 정할수가 있다.

    @Html.Sitecore().DynamicPlaceholder(new DynamicPlaceholderDefinition("content")
    {
        Count = 5,
        MaxCount = 10,
        Seed = 100,
        OutputModifier = (input, context) => new HtmlString(String.Format("<div class=\"definition-{1}\">{0}</div>", input, context.Index))
    })
    


    <div class="definition-0">content-{4C770DD9-F840-45AB-A22F-8CCAAC42F646}-100</div>
    <div class="definition-1">content-{4C770DD9-F840-45AB-A22F-8CCAAC42F646}-101</div>
    <div class="definition-2">content-{4C770DD9-F840-45AB-A22F-8CCAAC42F646}-102</div>
    <div class="definition-3">content-{4C770DD9-F840-45AB-A22F-8CCAAC42F646}-103</div>
    <div class="definition-4">content-{4C770DD9-F840-45AB-A22F-8CCAAC42F646}-104</div>
    


2017년 11월 7일 화요일

Sitecore 9 Forms의 새로운 기능

이전 포스트에 소개한것처럼, 사이트코어 9 에서는 Forms 라는 모듈이 소개되었다.

기존에 사용하던 WFFM (Web Forms For Marketers)를 대체하는 모듈로써, WFFM보다 더 진보(?) 되어진 모듈이라고 할수있으며, Forms는 Web Form이 아닌 MVC 기반으로 만들어져 새로운 UI/UX를 통하여 Non-Technical 유저에게도 사용하기 편하도록 만들어졌다.

기본적으로 HTML Element를 쉽게 추가하고 목록을 변경할수 있도록 Drag & Drop 기능이 소개되었으며, 아래는 Forms 모듈의 기능을 나열하였다.


  • 웹사이트 Visitor들의 폼 페이지를 렌더링 수
  • 필드 검증절차를 통하여, 오류 빈도 및 소요한 시간 분석
  • 멀티 페이지 (예, 페이지1 -> 다음 -> 페이지2 -> 다음 -> 페이지 3 -> 등록) 등록 가능
  • Submit 버턴을 통하여 마케팅 캠페인과 호환
  • Javascript 및 CSS 등 추가 리소스 등록가능
  • CSS Class 필드를 통하여, 새로운 스타일 생성가능
  • Forms 템플릿을 생성하여 많은 페이지를 쉽게 생성할수있음
  • Ajax를 이용하여 페이지 로딩없이 페이지를 이동할수있음
  • 필요에 따라 추가적인 필드 검증과 액션버튼 이벤트 생성 가능

사이트코어 - Sitecore 9 Forms Tab

사이트코어 - Sitecore 9 Forms



* 사이트코어는 9.0까지 WFFM를 지원하지만, 버전 9.1부터는 WFFM Support를 중단할 예정이다.



2017년 10월 26일 목요일

Sitecore 9.0 새로운 기능 및 기술

저번 주에 라스베가스에서 열린 Sitecore Symposium 2017을 무사히 마치고 돌아왔며, 이번 Symposium에서 사이트코어는 Sitecore Experience Cloud 9.0 릴리즈 하였다.

개인적으로 이번 토론회는 처음 참석하는 것이므로 무엇보다 기대가 많았다. Sitecore MVP로써 Sitecore 9.0을 미리 시험할수 있었으며, MVP들의 피드백을 토대로 사이트코어는 새로운 SIF (Sitecore Installation Framework) 보완하는 계기도 되었다.

아래는 이번 Sitecore 9.0 에서 적용되는 새로운 중요 기술 및 기능들이며, 차후 각각 기술에 대한 상세한 설명을 포스트 할 예정이다.

xConnect

사이트코어 XP와 xDB 사이에 존재하는 새로운 Service Layer로써, xConnect는 사이트코어가 아닌 다른 플랫폼 및 디바이스와 연동 할수가 있다. xConnect는 Collection과 Search 서비스로 구성되어 있으며, 다른 플랫폼과 디바이스는 xConnect를 통하여 사이트코어 xDB의 데이타를 불러올 수 있으며, 또한 xConnect는 다른 플랫폼의 데이타를 xDB를 가져올수가 있다.

SQL Server

Sitecore 7.5부터 적용해오던 MongoDB 를 필수요소에서 제외하였다. SQL Server를 기본 Provider로 설정되어졌으며, MongoDB 는 선택적인 항목이 되었다. 사용자 및 개발자 환경에 따라 SQL Server, SQL Azure, CosmosDB, 또는 MongoDB를 사용할수있으며, 어떤 DB 타입 상관없이 xConnect는 이 모든 데이타베이스 서버와의 호환되며 작동한다.


Forms

사이트코어 8.2까지 WFFM (Web Form For Marketers) 모듈을 사용하여, 온라인 폼 페이지를 만들고, xMarketing과 컨넥하여 사용자 및 유저의 정보를 모아왔으나, 9.0부터는 새로운 Form 빌더를 Sitecore XP (Experience Platform)에 추가였으며, UI/UX 역시 더 세련(?)되어 만들어졌다.. 이로써, 새로운 폼 모듈을 추가적으로 설치 하지 않아도 된다. Sitecore 9.0까지는 계속 WFFM을 지원하고 서포트를 하지만, 9.1부터는 WFFM 모듈 및 서포트를 중단할 계획인다.

Marketing Automation

기존에 사용해오던, Engagement Plan를 보완한 기능이며, Drag/Drop을 통하여 쉽게 마케팅 로직을 생성할수있다. 손 쉬운 UI/UX 바탕으로 마케터는 쉽게 자동화된 마케팅 캠페인을 생성할 수 있다.

Search

Sitecore 8.2까지 기본 서치엔진이던 Lucene 사용을 중단하고, 기본 엔진으로 Solr 또는 Azure Search를 사용한다. Lucene를 파일을 기반으로한 서치엔진으로 모든 데이타가 파일형식으로 저장되는것으로 반해, Solr 은 웹어플리케이션으로써 Lucene 을 탑재하여 다양한 API를 제공하며, 서치엔진은 사용 용도에 맞게 구현할수가 있다. 
Sitecore 9.0는 OWIN authentication 미드웨어를 통하여 새로운 로그인 방식을 구현한다. 기존의 LDAP 모듈을 이용하여 Active Directory의 데이터를 Sitecore CMS와 통합하여 로그인을 구현하였지만, 이번 Federated Authentication을 사용함으로써 아래의 다양한 플랫폼과 연동시킬수 있다. 예를 들면, Sitecore의 로그인을 Azure AD와 통합을 할수가 있다.
  • OpenId Connect (AzureAD, identity server) 
  • Microsoft Account 
  • Google
  • Facebook 
  • Twitter 
  • WS-Trust (Web Services Trust Language)
  • OAuth 
  • SAML


Dynamic Placeholder

이전부터 많이 문제가 제기되어왔던 기존 Placeholder에 대한 기능은 많이 제약이 따랐다. 예를 들어 렌더링 아이템에는 같은 이름의 Placeholder를 사용하지 못하였으며, 이로인하여 각각 새로운 Placeholder Setting을 적용해야만 했다. 하지만, 이름처럼 이를 다이나믹하게 보안하여 하나의 Placeholder Setting 아이템을 다양하게 구현하도록 만들었다. 차후, 여기에 대한 자세한 설명을 포스트하도록 하겠다.





2017년 10월 4일 수요일

사이트코어 필드 아이템 Versioned, Unversioned 그리고 Shared 속성 적용하기

이번에는 사이트코어의 기본기능에 대하여 소개해보도록 하겠다.

모든 페이지 아이템은 탬플릿에 의하여 생성되며, 탬플릿을 생성할시 유저는 필드 이름과 필드 타입을 설정하여야 한다. 탬플릿을 생성하고 필드를 추가하면 해당 필드의 설정에 Unversioned 와 Shared라는 체크박스가 있다.



필드를 새로 성성하면, 자동적으로 두 체크박스를 선택되어지지 않으므로써 Versioned라는 세팅으로 필드를 생성한다.

먼저, 페이지 아이템에 2개의 언어가 설정되어있고, 각각 2개 및 3개의 페이지 아이템 버전이 있다고 생각하자.
필드 이름: "Test", 필드 기본값: "Test Version"
    • PageItem 버전1 : 영어
    • PageItem 버전2 : 영어
    • PageItem 버전3 : 영어
    • PageItem 버전1 : 일본어
    • PageItem 버전2 : 일본어


아래는 각각 설정이 어떻게 적용되어지는 알수가 있다.

  • Unversioned (적용 안함), Shared (적용 안함)
    필드에 새로운 값이 적용되면, 오직 해당 아이템 버전과 해당 언어에만 새로운 값이 적용된다. 예를 들어, "PageItem 버전 2: 영어" 아이템의 "Test" 필드 값을 "Test Version 2"로 설정할시,
    • PageItem 버전1 : 영어 - 필드값: "Test Version"
    • PageItem 버전2 : 영어 - 필드값: "Test Version 2"
    • PageItem 버전3 : 영어 - 필드값: "Test Version"
    • PageItem 버전1 : 일본어 - 필드값: "Test Version"
    • PageItem 버전2 : 일본어 - 필드값: "Test Version"

  • Unversioned (적용), Shared (적용 안함)
    필드에 새로운 값이 적용되면, 오직 해당 언어의 모든 아이템 버전에 새로운 값이 적용된다. 예를 들어, "PageItem 버전 2: 영어" 아이템의 "Test" 필드 값을 "Test Version 2"로 설정할시,
    • PageItem 버전1 : 영어 - 필드값: "Test Version 2"
    • PageItem 버전2 : 영어 - 필드값: "Test Version 2"
    • PageItem 버전3 : 영어 - 필드값: "Test Version 2"
    • PageItem 버전1 : 일본어 - 필드값: "Test Version"
    • PageItem 버전2 : 일본어 - 필드값: "Test Version"

  • Unversioned (적용 안함), Shared (적용)

    필드에 새로운 값이 적용되면, 모든 언어의 모든 아이템 버전에 새로운 값이 적용된다. 예를 들어, "PageItem 버전 2: 영어" 아이템의 "Test" 필드 값을 "Test Version 2"로 설정할시,
    • PageItem 버전1 : 영어 - 필드값: "Test Version 2"
    • PageItem 버전2 : 영어 - 필드값: "Test Version 2"
    • PageItem 버전3 : 영어 - 필드값: "Test Version 2"
    • PageItem 버전1 : 일본어 - 필드값: "Test Version 2"
    • PageItem 버전2 : 일본어 - 필드값: "Test Version 2"

2017년 8월 31일 목요일

사이트코어 크롬 익스텐션 (Chrome Extension)

사이트코어 CMS를 사용하다보면 UI 디자인부분 또는 자바스크립 기능에서 불편함 점이있다.

예를 들면, 데스크탑 모드에서 작업을하다보면 Maser 또는 Web DB를 이동해야하는 경우가 간혹(?) 발생하며, 또는 Content Editor에서 아이템 정보를 일괄적으로 늘려야 하는경우도 생긴다.

추가적으로 많은 기능을 필요로화 하지만, Siteocre에서는 모든것은 추가할수가 없다. 이것을 보완하기 위하여, 필자는 크롬 익스텐션에서 Sitecore Extension 이라는 플러그인을 사용한다.

플러그인에는 아주 많은 기능이 있어 모두 다 나열하지는 못하지만, 필자가 생각하는 가장 좋은 기능은 Ctrl+Space key를 사용하여 Shortcut 기능을 수행할수가 있다.

Install at Chrome: Sitecore Extension





 

2017년 8월 30일 수요일

Placeholder 컨트롤 수 제한하기

xEditor를 사용하다보면, 사이트코어 사용자의 권한 설정에 따라 특정한 Placeholder에 하나 이상의 컴포넌트를 제한해야하는 경우가 있다.

또한, 특정한 필드가 각각의 페이지가 아닌, 템플릿 __Standard Values에서 일괄적으로 변경을 해야하는 경우가 있는데, 이럴경우 사이트코어의 GetChromeDataArgs 클래스 파이프라인을 통하여 제한설정을 할수가 있다.


 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
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Pipelines.GetChromeData;
using System.Text.RegularExpressions;
using Sitecore.Diagnostics;
using Sitecore.Data.Items;
using Sitecore.Security.Accounts;
using Sitecore.Layouts;
using Sitecore.Data;
using Sitecore.Data.Fields;

namespace Sitecore.Placeholder.Restriction
{
    public class RemoveDeleteButton : GetPlaceholderChromeData
    {
        public override void Process(GetChromeDataArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            Assert.IsNotNull(args.ChromeData, "Chrome Data");
            if ("placeholder".Equals(args.ChromeType, StringComparison.OrdinalIgnoreCase))
            {
                string placeholderKey = args.CustomData["placeHolderKey"] as string;

                Sitecore.Data.Fields.LayoutField layoutField = new Sitecore.Data.Fields.LayoutField(Context.Item.Fields[Sitecore.FieldIDs.FinalLayoutField]);
                LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
                DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(Context.Device.ID.ToString());

                // Initial setting path is "/sitecore/system/Modules/Placeholder Restriction Manager/Remove Delete Button"
                Item removeDeleteButtonItem = Context.Item.Database.GetItem("/sitecore/system/Modules/Placeholder Restriction/Remove Delete Button");

                if (removeDeleteButtonItem == null)
                {
                    Assert.IsNull(removeDeleteButtonItem, "Item is null");
                    return;
                }

                MultilistField listOfRenderingItemsDelete = removeDeleteButtonItem.Fields["List of Rendering Items"];
                string listOfUserRolesDelete = removeDeleteButtonItem.Fields["List of Users and Roles"].Value.Trim();
                List<string> eachListOfUserRolesDelete = !String.IsNullOrEmpty(listOfUserRolesDelete) ? listOfUserRolesDelete.Split(';').ToList() : null;

                // Remove "Delete" button in placeholder
                if (eachListOfUserRolesDelete != null && eachListOfUserRolesDelete.Select(e => User.Current.IsInRole(e) || User.Current.LocalName.Equals(e)).Any())
                {
                    args.ChromeData.Custom["removeAddHereButton"] = true;
                    foreach (ID renderingItemId in listOfRenderingItemsDelete.TargetIDs)
                    {
                        RenderingItem r = RenderingItem.GetItem(renderingItemId, Context.Data.Database, true);
                        string renderingName = Regex.Replace(r.Name.ToLower(), @"\s+", "");
                        string displayName = Regex.Replace(args.ChromeData.DisplayName.ToLower(), @"\s+", "");

                        // Only when rendering name matches to its display name
                        // Only when rendering item's display name contains rendering name
                        if (renderingName == displayName || displayName.Contains(renderingName))
                        {
                            args.ChromeData.Custom["editable"] = false;
                        }
                    }
                }
            }
        }
    }
}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getChromeData>
        <processor
          type="Sitecore.Placeholder.Restriction.RemoveDeleteButton, Sitecore.Placeholder.Restriction"
          patch:after="processor[@type='Sitecore.Pipelines.GetChromeData.GetPlaceholderChromeData, Sitecore.Kernel']"/>
      </getChromeData>
    </pipelines>
  </sitecore>
</configuration>

2017년 8월 8일 화요일

사이트코어와 Slack (슬랙) API 연동하기

이번에는 사이트코어와 Slack API 연동에 대하여 알아보도록 하겠다.

Slack API은 메신저와 같은 커뮤니케이션 공간으로써, 많은 유저들이 하나의 커뮤니티 공간에서 많은 채널 (채팅방)을 서로 공유하며, 정보를 주고 받을 수가 있다.

개발자들 및 IT 유저들 사이에는 이미 널리 알려진 웹어플리케이션으로써, 필자가 일하고 있는 회사에서도 역시 직원들의 커뮤니티 공간으로 잘 활용을 하고 있다.

이전에 포스트에서 올려놓았던 Workflow Related Item 업데이트 하기에서 Custom Workflow를 소개하였으며, Workflow가 업데이트 될때마다 관리자 및 검증자가 업데이트 되어진 정보를 이메일로 받는 동시에, Slack API를 통하여 Slack 채널에도 업데이트된 정보가 올려지도록 클래스를 많들었다.

우선, Real-Time message를 전송하기 위하여, Slack 플러그인 (Incoming WebHooks)을 추가적으로 설치한 후,  Webhook URL의 값을 WebClient의 UploadString 메쏘드 파라미터로 입력을 한다.


 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public static class PostSlackMessage
{
    public static void PostMessage(Item pageItem, User user, bool isPublished, string comment = null)
    {
        if (pageItem == null) {
            Sitecore.Diagnostics.Assert.IsNull(null, "Page Item can't be found!");
        }
        if (user == null) {
            Sitecore.Diagnostics.Assert.IsNull(null, "User can't be found!");
        }

        string pagePath = pageItem.Paths.Path.ToString().ToLower();
        pagePath = pagePath.Replace(Sitecore.Context.Data.Site.RootPath.ToLower(), "");
        pagePath = pagePath.Replace(Sitecore.Context.Data.Site.StartItem.ToLower(), "");

        string requestedByEmail = user.Profile.Email;
        string requestedByFullName = user.Profile.FullName;
        string updatedByEmail = User.FromName(pageItem.Statistics.UpdatedBy, true).Profile.Email;
        string updatedByFullName = User.FromName(pageItem.Statistics.UpdatedBy, true).Profile.FullName;
        string requestedDate = DateTime.Now.ToString("g");
        
        string pretextMsg = "Approval Request :file_cabinet:";
        string fallbackMsg = "ReferenceError - UI is not defined";
        string titleMsg = "<https://yoursitecms.com" + pagePath + "|" + pageItem.Fields["Page Title"].Value + "> - " + pagePath;
        string commentMsg = comment;
        string colorMsg = "#FF8000";
        
        if (isPublished)
        {
            pretextMsg = "Approved and Published :white_check_mark:";
            titleMsg = "<http://yoursitecds.com" + pagePath + "|" + pageItem.Fields["Page Title"].Value + ">";
            colorMsg = "#36a64f";
        }
        var field = new[]{
                new {
                    title = (isPublished) ? "Approved by" : "Requested by",
                    value = "<mailto:" + requestedByEmail + "|" + requestedByFullName + ">",
                    @short = true
                },
                new {
                    title = "Updated by",
                    value = "<mailto:" + updatedByEmail + "|" + updatedByFullName + ">",
                    @short = true
                },
                new {
                    title = (isPublished) ? "Approved Date" : "Requested Date",
                    value = requestedDate,
                    @short = true
                },
                new {
                    title = "Page Version",
                    value = pageItem.Version.Number.ToString(),
                    @short = true
                }
        };

        var message = new
        {
            username = "Mr. Sitecore",
            channel = "G690XEDK4", // Private Channel "Sitcore Publishing"
            icon_emoji = ":sitecore:",
            attachments = new[]{ 
                new {
                    pretext = pretextMsg,
                    fallback = fallbackMsg,
           title = titleMsg,
           text = commentMsg,
                    color = colorMsg,
                    fields = field
                }
            }
        };

        var json = JsonConvert.SerializeObject(message);

        var webClient = new WebClient();
        webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
        webClient.UploadString("https://hooks.slack.com/services/your/code/here", json);
    }
}



1
2
// Post Slack Message
PostSlackMessage.PostMessage(args.DataItem, User.Current, true, args.CommentFields["Comments"].ToString());