2018년 6월 18일 월요일

사이트코어 내의 URL 마스크 (Alias) 설정하기

사이트코어 CMS에는 많은 기능이 포함되어있다. 그 중의 하나가 바로 Alias. 이 기능은 흔희 말하는 URL Mask 또는 cloaked URL이라고 불리며, 원래의 URL 링크주소를 Mask하여 다른 이름으로 바꿔주는 것이다.

보통 이 기능은 웹서버  (Rewrite Module) 또는 프로그래밍 상에서 Redirect Method (C#)를 사용하여 URL을 변경하지만, 사이트코어의 Aliases 기능을 통하여 쉽게 변경할수가 있다.

"http(s)://yoursite/blog/2018/11/25/My-Blog"를 "http(s)://mysite/blog/My-Blog"으로 변경하는것을 예로 들어 순서대로 세팅을 해보겠다.

  1. 해당 URL은 사이트 홈 (시작페이지) 아래의 "Blog" 아이템에서 URL이 시작하므로, "Blog" 아이템을 선택한다.
  2. Content Editor의 Presentation 탭 또는 Experience Editor의 Advance 탬에서 "Aliases"를 클릭한다.
  3. Name 필드에 "/blog" 입력하고, "Add" 버턴을 눌러 Aliases를 추가한다.

  4. 이젠, "My-Blog" 아이템을 눌러주고 같은 방법으로 Name 필드에 "/blog/My-Blog"를 추가한다.

  5. 위 두개의 아이템의 Aliases를 세팅함으로써 "/sitecore/system/Aliases/"에 "blog/My-Blog" 라는 아이템이 Linked 아이템과 함께 생성되었다.

  6. 원래 생성한 아이템 페이지로 가서 Related Item과 함께 아이템을 Publish하면, Alias가 적용된 URL로 페이지를 렌더링 할 수가 있다.
    http(s)://mysite/blog/My-Blog


* 참고로, 사이트코어의 Alias 기능은 Temporary Redirection 으로써 보통 Major Search Engine의 검색 결과에서는 기존의 URL (http(s)://mysite/blog/2018/11/25/My-Blog)로 결과물이 나타난다.

* 이번 예제는 Multi-Site가 아닌, 사이코어내의 기본 사이트 세팅에만 적용되는것이며, 한 Instance에 Multiple 사이트가 존재한다면, 아래의 링크를 통하여 Pipeline을 업데이트하길 바란다.
How to configure site-specific aliases (https://kb.sitecore.net/articles/565325)







2018년 5월 4일 금요일

Rich Text Editor (RTE)에 YouTube 버튼 추가하기

사이트코어는 Telerik 에디터를 기본 Rich Text Editor (RTE)로 사용하며, Core DB에서 HTML Profile 세팅을 통하여 CMS Author들에게 툴 사용 제한을 둘수가 있다.

최근 회사에서 블로그 페이지 및 툴을 새롭게 만들었으며, 블로그 Author로부터 YouTube 링크를 RTE 디자인 모드에서 추가하고 싶어한다. 이번에는 어떻게 새로운 버턴을 만들고 커맨드를 형성 시키는지 알아보도록하자.
  1. Core DB로 이동 후, 새로운 버턴을 추가하고 싶어하는 Tool에서 "__Html Editor Button"의 템플릿으로 새로운 아이템을 추가한다.



  2. 새로운 아이템의의 속성을 아래와 같이 변경해주며, 아이템 아이콘, 디스플레이 이름도 변경한다.



  3.  "/WebSite/sitecore/shell/Controls/Rich Text Editor/RichText Commands.js" 파일로 이동 후, 새로운 "RadEditorCommandList"를 추가하고, "Regular Expression"을 통하여, 유튜브 비디오 아이디만을 가져온다.

     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
    RadEditorCommandList["InsertYouTube"] = function (commandName, editor, args) {
        var d = Telerik.Web.UI.Editor.CommandList._getLinkArgument(editor);
        Telerik.Web.UI.Editor.CommandList._getDialogArguments(d, "A", editor, "DocumentManager");
    
        // Highlight 되어진 텍스트만 가져온다.
        var html = editor.getSelectionHtml();
        if (getId(html)) {
            var videoId = getId(html);
            scEditor = editor;
            scEditor.pasteHtml('<div class="blog-rte-youtube"><iframe width="560" height="315" src="//www.youtube.com/embed/' + videoId + '" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></div>');
        } else {
            // Highlight 되어진 텍스트가 YouTube 아이디와 매치가 되지 않을 경우, 메세지를 보낸다.
            alert("Please highlight YouTube link URL.");
        }
    };
    
    //Get YouTube video ID
    function getId(url) {
        var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
        var match = url.match(regExp);
    
        if (match && match[2].length == 11) {
            return match[2];
        } else {
            return false;
        }
    }
    


  4. Master DB로 이동 후, RichText Editor를 사용하고 페이지에서 Editor Popup을 실행한다.



YouTube 링크 버턴










2018년 3월 26일 월요일

사이트코어 Federated Authentication 설정 및 사용하기

이번에는 사이트코어 9에서 소개된 Federated Authentication (FA)에 대하여 알아보도록 하자.

사이트코어는 .NET 프레임워크를 베이스로 사용하여 플랫폼을 윈도우 환경에 설치한다. 많은 유저 및 컴퍼니들은 CMS 사용자 로그인 프로세스를 Active Directory (AD)와 연동하여 사용하고 있으며, 대표적인 모듈로는 사이트코어에서 제공하는 Activie Direcotry 모듈이 있다.
이는 AD 도메인을 사이트코어 플랫폼과 연동시킨다. 기존 AD에 세팅되는 모든 유저의 Role 및 Permission을 똑같이 사이트코어에 적용하여 추가적인 어카운트 세팅없이 AD에 account가 존재하다면, 똑같이 유저정보로 사이트코어 CMS에 로그인할수있다.

사이트코어 9에서는 다양한 Authentication Provider를 제공한다. 가장 대포적인 예가 ADFS를 통하여 사이트코어를 로그인 하는 방법이다. 이는 기본적으로 AD On-Perm을 Azure Activie Directory Connect synchronization을 통하여 AzureAD와 연동을 하고, AzureAD에 Sync된 정보를 Microsoft의 Owin 미들웨어를 통하여 Token 기반의 OpenID Connect를 사용한다.

지금부터 Federated Authentication을 세팅 및 설정하는 방법에 대하여 알아보도록 하자.
  1. 사이트코어에서 FA를 사용하기 위해서는 "/wwwroot/App_Config/Include/Examples"폴더에 "Sitecore.Owin.Authentication.Enabler.config" 파일을 Include 폴더에 복사하고, "FederatedAuthentication.Enabled"을 "true"로 설정을 한다.
  2. 새로운 class를 만들어 IdentityProvider를 설정한다.

     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
    using Microsoft.Owin.Security.OpenIdConnect;
    using Owin;
    using Sitecore.Configuration;
    using Sitecore.Diagnostics;
    using Sitecore.Owin.Authentication.Configuration;
    using Sitecore.Owin.Authentication.Pipelines.IdentityProviders;
    using Sitecore.Owin.Authentication.Services;
    using System.Globalization;
    using System.Security.Claims;
    using System.Threading.Tasks;
    using Microsoft.Owin.Security;
    
    namespace MyProject.Sitecore.Owin.AzureAD.Pipelines
    {
        public class CustomAzureADIdentityProvider : IdentityProvidersProcessor
        {
    
            public CustomAzureADIdentityProvider(FederatedAuthenticationConfiguration federatedAuthenticationConfiguration) : base(federatedAuthenticationConfiguration)
            {
            }
    
            protected override string IdentityProviderName => "AzureAd";
    
            protected override void ProcessCore(IdentityProvidersArgs args)
            {
                Assert.ArgumentNotNull(args, nameof(args));
    
                var identityProvider = this.GetIdentityProvider();
                var authenticationType = this.GetAuthenticationType();
    
                string aadInstance = Settings.GetSetting("AADInstance");
                string tenant = Settings.GetSetting("Tenant");
                string clientId = Settings.GetSetting("ClientId");
                string postLogoutRedirectURI = Settings.GetSetting("PostLogoutRedirectURI");
                string redirectURI = Settings.GetSetting("RedirectURI");
    
                string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
    
                args.App.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
                {
                    Caption = identityProvider.Caption,
                    AuthenticationType = authenticationType,
                    AuthenticationMode = AuthenticationMode.Passive,
                    ClientId = clientId,
                    Authority = authority,
                    PostLogoutRedirectUri = postLogoutRedirectURI,
                    RedirectUri = redirectURI,
                    
                    Notifications = new OpenIdConnectAuthenticationNotifications
                    {
                        SecurityTokenValidated = (context) =>
                        {
                            ClaimsIdentity identity = context.AuthenticationTicket.Identity;
                            foreach (Transformation current in identityProvider.Transformations)
                            {
                                current.Transform(identity, new TransformationContext(FederatedAuthenticationConfiguration, identityProvider));
                            }
                            return Task.FromResult(0);
                        }
                    }
                });
            }
        }
    }


  3. 새로운 만들어진 Provider를 MyProject.Owin.AzureADconfig 파일을 만들고, /Include 폴더에 적용시킨다.

      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
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
        <sitecore role:require="Standalone or ContentDelivery or ContentManagement">
            <settings>
                <setting name="ClientId" value="{{Azure Web service의 클라이언트 아이디를 입력한다}}" />
                <setting name="AADInstance" value="https://login.microsoftonline.com/{0}" />
                <setting name="Tenant" value="{{Azure의 Tenant 정보를 입력한다.}}" />
                <setting name="PostLogoutRedirectURI" value="https://mysite/sitecore/login" />
                <setting name="RedirectURI" value="https://mysite/sitecore" />
            </settings>
            <pipelines>
                <owin.identityProviders help="Processors should derive from Sitecore.Owin.Authentication.Pipelines.IdentityProviders.IdentityProvidersProcessor">
                    <processor type="MyProject.Sitecore.Owin.AzureAD.Pipelines.CustomAzureADIdentityProvider, MyProject.Sitecore.Owin.AzureAD" resolve="true" />            </owin.identityProviders>
            </pipelines>
            <federatedAuthentication type="Sitecore.Owin.Authentication.Configuration.FederatedAuthenticationConfiguration, Sitecore.Owin.Authentication">
                <identityProvidersPerSites hint="list:AddIdentityProvidersPerSites">
                    <mapEntry name="0" type="Sitecore.Owin.Authentication.Collections.IdentityProvidersPerSitesMapEntry, Sitecore.Owin.Authentication">
                        <sites hint="list">
                            <site>shell</site>
                            <site>login</site>
                            <site>admin</site>
                            <site>service</site>
                            <site>modules_shell</site>
                            <site>modules_website</site>
                            <site>website</site>
                            <site>scheduler</site>
                            <site>system</site>
                            <site>publisher</site>
                        </sites>
                        <identityProviders hint="list:AddIdentityProvider">
                            <identityProvider ref="federatedAuthentication/identityProviders/identityProvider[@id='AzureAd']" />
                        </identityProviders>
                        <externalUserBuilder type="Sitecore.Owin.Authentication.Services.DefaultExternalUserBuilder, Sitecore.Owin.Authentication">
                            <param desc="isPersistentUser">true</param>
                        </externalUserBuilder>
                    </mapEntry>
                </identityProvidersPerSites>
                <identityProviders hint="list:AddIdentityProvider">
                    <identityProvider id="AzureAd" type="Sitecore.Owin.Authentication.Configuration.DefaultIdentityProvider, Sitecore.Owin.Authentication">
                        <param desc="name">$(id)</param>
                        <param desc="domainManager" type="Sitecore.Abstractions.BaseDomainManager" resolve="true" />
                        <caption>Sitecore Login by Microsoft ADFS</caption>
                        <icon>/sitecore/shell/themes/standard/Images/24x24/msazure.png</icon>
                        <domain>sitecore</domain>
                        <transformations hint="list:AddTransformation">
                            <transformation name="set idp claim" ref="federatedAuthentication/sharedTransformations/setIdpClaim" />
                            <!-- 해당 그룹의 모든 유저에게 "sitecore\developer" role의 권한을 준다. -->
                            <transformation name="Sitecore Client Users Role" type="Sitecore.Owin.Authentication.Services.DefaultTransformation, Sitecore.Owin.Authentication">
                                <sources hint="raw:AddSource">
                                    <claim name="groups" value="f5428848-ca62-4bcd-a3d1-189977c26c2e" />
                                </sources>
                                <targets hint="raw:AddTarget">
                                    <claim name="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" value="sitecore\Sitecore Minimal Page Editor" />
                                </targets>
                                <keepSource>true</keepSource>
                            </transformation>
                        </transformations>
                    </identityProvider>
                </identityProviders>
                <sharedTransformations hint="list:AddSharedClaimsTransformation">
                    <setIdpClaim name="setIdpClaim" type="Sitecore.Owin.Authentication.Services.SetIdpClaimTransform, Sitecore.Owin.Authentication" />
                </sharedTransformations>
                <propertyInitializer type="Sitecore.Owin.Authentication.Services.PropertyInitializer, Sitecore.Owin.Authentication">
                    <maps hint="list">
                        <!-- Mapping을 통하여 해당 그룹에 있는 모든 유저에게 Administrator 권한을 준다. -->
                        <map name="Sitecore Admin to Administartor" type="Sitecore.Owin.Authentication.Services.DefaultClaimToPropertyMapper, Sitecore.Owin.Authentication">
                            <data hint="raw:AddData">
                                <source name="idp" value="AzureAd" />
                                <source name="groups" value="3e677ddd-89a7-4fab-9f33-b25c5c35f7e6" />
                                <target name="IsAdministrator" value="true" />
                            </data>
                        </map>
                        <!-- Mapping을 통하여 유저 Profile의 Email 필드에 유저 이메일 정보를 업데이트 한다. -->
                        <map name="Emailaddress to email" type="Sitecore.Owin.Authentication.Services.DefaultClaimToPropertyMapper, Sitecore.Owin.Authentication">
                            <data hint="raw:AddData">
                                <!-- <source name="idp" value="AzureAd" /> -->
                                <source name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn" />
                                <target name="Email" />
                            </data>
                        </map>
                        <!-- Mapping을 통하여 유저 Profile의 FullName 필드에 유저 이름을 업데이트 한다. -->
                        <map name="Name claim" type="Sitecore.Owin.Authentication.Services.DefaultClaimToPropertyMapper, Sitecore.Owin.Authentication">
                            <data hint="raw:AddData">
                                <source name="name" />
                                <target name="FullName" />
                            </data>
                        </map>
                        <!-- Mapping을 통하여 유저 Profile의 Comment 필드에 추가적인 정보를 업데이트 한다. -->
                        <map name="Description claim" type="Sitecore.Owin.Authentication.Services.DefaultClaimToPropertyMapper, Sitecore.Owin.Authentication">
                            <data hint="raw:AddData">
                                <source name="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" />
                                <target name="Comment" />
                            </data>
                        </map>
                    </maps>
                </propertyInitializer>
            </federatedAuthentication>
        </sitecore>
    </configuration>
    

  4. Azure Active Directory -> App registrations 에서 Sitecore CMS의 Sign-on URL을 등록한다.
  5. 등록되어진 사이트코어 App에서 Settings -> Reply URL에서 "https://{yoursitecoresite}/sitecore" 로 등록한다.
  6. Settings -> Required permissions에서 Window Azure Active Directory API의 Delecated Permission을 설정한다.
    1. 체크 "Access the directory as the signed-in user"
    2. 체크 "Sign in and read user profile"
    3. Save
  7. Manifest를 업데이트한다.
    1. "groupMembershipClaims": "SecurityGroup"
    2. "oauth2AllowImplicatFlow": true
  8. Sitecore 9+ 버전을 사용하고 있다면, Web.config 파일에서 "owin:AutomaticAppStartup" 세팅을 True로 설정한다.
  9. AzureAD에 등록된 이메일로 사이트코어 CMS에 로그인한다.









2018년 2월 20일 화요일

사이트코어 Config 파일 패치하기

우선, .NET 개발자라면은 WebConfig.config 또는 App 폴더안의 세팅파일들이 어플리케이션에서 어떤 중요한 역할을 하지는 기본적으로 알고있을것이다.
개발자들은 Configuration 파일을 통하여 어플리케이션을 Recompile 없이 변경할수 있으며, 사이트코어 역시 모듈 또는 기능 별로 많은 .config 파일을 파일시스템에 놓아둔다.

사이트코어는 버전 9를 릴리즈하면서, App_Config 폴더안의 파일 스트럭쳐를 폴더 별로 Organize 하였으면, 이번에는 어떻게 config 파일들을 수정하는지 알아보도록 하겠다.
.config 파일의 기본형식은 아시다시피, XML 파일의 형식이며 기본적이 구조는 아래와 같다.

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <sitecore>
    </sitecore>
</configuration>

보통 .Config 파일을 수정할때 해당 파일에 가서 변경하고 싶은 Node 또는 Value를 수정하는데, 사이트코어는 새로운 패치 파일을 만들어 변경 하는것을 선호한다.
예를 들어, Sitecore.config 파일안의 특정한 Node를 변경 또는 수정을 하고싶다면, WebRoot/App_Config 의 새로운 폴더 (예, 폴도명 zzz.custom)를 만들고, Configuration 노드에 XML Set Namespace를 추가하여 아래와 같은 패치파일을 만들수있다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<!-- 기존 세팅 Sitecore.Config 파일 -->
<sitecore >
    <settings>
        <!--  AUTOMATIC UNLOCK ON SAVED
                If true, the a saved item is automatically unlocked after
                saving.
        -->
        <setting name="AutomaticUnlockOnSaved" value="false"/>
    </settings>
</sitecore>


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<!-- 패치 파일 /App_config/zzz.Custom/Sitecore.Custom.config -->
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
    <sitecore >
        <settings>
            <!--  Original Setting from Sitecore.config -->
            <setting name="AutomaticUnlockOnSaved" set:value="true"/>
        </settings>
    </sitecore>
</configuration>

해당 업데이트를 "http(s)://website/sitecore/admin/showconfig.aspx" 페이지에서 확인을 하면, 패치되어진 값을 볼수가있다.





이런 형식으로 패치파일을 이용하면 차후 시스템 및 어플리케이션 조금 더 효율적으로 관리 및 유지할수있으며, 시스템을 초기화하는데도 용이하다.

*참고로 사이트코어를 config 파일들을 A-Z 순서대로 서칭하며 폴더안에 다른 폴더들과 파일들이 같이 존재한다면, 파일을 먼저 A-Z 순으로 패치하고, 다음으로 폴더순으로 패치하기위한 서칭을 시작한다.

2018년 1월 23일 화요일

Sitecore Migration - Azure Toolkit Error

이전 포스트에 얘기한것 처럼, 회사에서 Sitecore On-Prem을 Azure로 Migration 진행중이다.

먼저, 기존 On-Prem에 사용하던 데이타 (Sitecore 8.0 U5)를 새로운 버전 (Sitecore 9.0.0) 으로 Migration을 해야하는데 서버 및 데이타베이스 환경이 틀리다보니, 기존의 데이타 Deployment Package를 생성하여야한다. Azure Deployment Package는 사이트코어의 Azure Deployment Toolkit를 이용하여 생성할수있는데, 그 이전에 로컬환경에 Sitecore 9.0 (XP0)을 설치하고 기존 데이터를 Sitecore Expression Migration Tool 3.1을 사용하여 로컬 Siteocre 9.0로 Migration 한다. 로컬에서 Migration이 성공적으로 끝나며, Deployment 패키지를 Azure ToolKit을 이용하여 생성하면 되는데, 여기서 하나의 문제점이 생겼다.

아래의 스크린샷에 보이는 것처럼, PowerShell에서 패키지를 생성하는 도중 "The file is too long. This operation is currently limited to supporting files less than 2 gigabytes in size." 라는 오류가 발생하였다. 



Sitecore의 Flash Installtion의 사이즈가 얼마나 될지는 모르겠지만, Migration되어진 데이터는 족히 6GB는 넘는것으로 보인다. 몇일동안의 리서치와 테스팅 결과 솔루션이 찾지 못하였고, Siteocre Support Ticket을 만들어 Auzre ToolKit 이슈에 대하여 보고하였다.

몇일후, 사이트코어로 부터 답변을 받았고 이 이슈는 ToolKit의 오류로 확인되었으며, 사이코어에서 문제를 해결하고 있다고한다. 그리고 그 대안의 방법으로 SQL Server Management Studio을 사용하여 Sitecore의 Azure Database를 컨넥하고 Database를 Migration한다. Configuration 및 Assemblies 파일들은 수동의 옮겨야한다.

  1. Using Sitecore Azure Marketplace, deploy a clean solution to Azure: https://azuremarketplace.microsoft.com/en-us/marketplace/apps/Microsoft.AppSvc_SiteCore_IFrame
  2. Using FTP access, copy custom file-based content to your solution
  3. In the Azure SQL Server firewall, allow access for your IP address, via Azure Portal
  4. Connect to Azure SQL databases using the SQL Server Management Studio and restore your databases on top of clean ones As a result, you should get a customized solution (with all your files) that contains the custom content (your databases).

모든 과정이 끝나면, 반드시 Sitecore.config 파일에서 Temp Folder 경로가 제대로 설정되어있는지 확인 후, "Rebuild Indexes" 와 "Rebuild Link Databases" 작업을 사이트코어 Control Panel에서 수행하여야 한다.




2017년 12월 6일 수요일

사이트코어 개발자 시험버전 다운로드하기

최근 사이트코어는 XP를 처음 사용해보는 개발자들에게 60일 무료 시험버전을 제공하기 시작했다. 아래의 사이트에서 등록을 하면, 사이트코어는 60일 제한 라이센트를 제공하며, 개발자는 그 기간내에 어떻게 플랫폼의 API 및 Helix 프레임워크 사용과 플랫폼 확장 개발등을 할수가 있다. 만약 60일 기간이 만료가 되었다면, 한번 더 연장이 가능하므로 최대 120일까지 사용할수가 있다.

*혹시, 설치 및 사용에 대하여 궁금한점이 있다면 오른쪽의 메일 보내기를 통하여 글쓴이에게 연락하여 주십시오.




===================== 업데이트 =====================

최근 사이트코어는 이전에 공개하였던 개발자버전의 시험 라이센스 중단하였다. 개발자 라이센스는 사이트코어의 프리뷰 버전으로 공개하였던것으로, 2018년도에 새로운 라이센스버전을 공개할 예정이다. Waiting List에 등록을 해놓는다면 조금 더 빨리(?) 라이센스를 받고 테스트할수있는 기회가 주어진다.





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>