Back To Home

Name: Ngày 14: Sitecore Serialization (Unicorn / TDS)

🎯 Mục tiêu Ngày 14

Bạn sẽ hiểu:

  1. Serialization trong Sitecore là gì

  2. TDS vs Unicorn – nên dùng cái nào?

  3. Cách cài đặt và cấu hình Unicorn

  4. Cách sync / revert / bảo vệ dữ liệu

  5. Best Practices khi làm việc với serialization


🧩 1. Serialization trong Sitecore là gì?

Sitecore Serialization là quá trình đưa item trong Content Tree (template, rendering, content…) xuống codebase, giúp:

  • Dễ dàng deploy giữa các máy (Dev → Staging → Prod)

  • Fix lỗi mất content

  • Giảm phụ thuộc vào DB

  • Làm việc theo team mà không bị ghi đè item

Serialization là bắt buộc khi làm Sitecore chuyên nghiệp.


🥇 2. Unicorn vs TDS — Nên chọn gì?

Tiêu chí Unicorn TDS
Giá Miễn phí Trả phí
Tích hợp Git Rất tốt Tốt
Tốc độ Nhanh Trung bình
Visual Studio Integration Không
Dùng phổ biến Rộng rãi (SXA, Habitat) Enterprise
Dễ config Trung bình Dễ nhất

👉 Khuyến nghị:

  • Dev team dùng Git → Unicorn

  • Công ty đang dùng Visual Studio Enterprise Toolset → TDS


🧰 3. Unicorn – Best Choice cho hầu hết dự án

✔ Unicorn hoạt động như thế nào?

  • Mỗi item được serialize thành file .yml

  • File được lưu trong serialization folder của project

  • Unicorn đảm bảo đồng bộ 1 chiều hoặc 2 chiều dựa trên cấu hình

  • Sync được thực hiện qua một trang web:

 
/unicorn.aspx

🧱 4. Cấu trúc Unicorn chuẩn (theo Habitat)

 
/src /Project /MySite /serialization /content /renderings /settings /Feature /Foundation /Serialization Unicorn.Config

⚙ 5. Cài đặt Unicorn

1. Install qua NuGet (preferred)

Trong project Website:

 
Install-Package Unicorn Install-Package Rainbow

2. Publish Unicorn Control Panel

Kiểm tra thư mục:

 
/sitecore/admin/Unicorn /unicorn.aspx

📝 6. Tạo Configuration File cho Unicorn

Mỗi module cần 1 file config riêng:

Ví dụ:

 
App_Config/Include/zzz.Project/MyProject.Unicorn.config

Cấu hình cơ bản:

 
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <unicorn> <configurations> <configuration name="Project.MySite.Content" description="Serialize content of MySite" role:require="Standalone"> <targetDataStore physicalRootPath="$(dataFolder)/serialization/MySite/Content" useDataCache="true" /> <predicate> <include name="Home" path="/sitecore/content/MySite/Home" /> <include name="Data" path="/sitecore/content/MySite/Data" /> </predicate> </configuration> </configurations> </unicorn> </sitecore> </configuration>

🔧 7. Các phần cần Serialize

✔ Luôn Serialize:

  • Templates

  • Branch Templates

  • Rendering

  • Layout

  • Media items (nếu là static SVG, PNG nhỏ)

  • Dictionary

❌ Không Serialize:

  • Media Library (ảnh upload người dùng)

  • Marketing content

  • Forms submissions

  • Log items

  • Campaigns


🔄 8. Cách sử dụng Unicorn

1. Mở Unicorn Panel:

 
/unicorn.aspx

2. Các thao tác chính:

Action Ý nghĩa
Sync Import item từ source → Sitecore
Reserialize Xuất toàn bộ item hiện tại → file YAML
Revert Xóa DB items không có trong serialized

🔒 9. Bảo mật Unicorn

Trong Production, bắt buộc khóa Unicorn bằng secret key:

 
<authenticationProvider type="Unicorn.ControlPanel.Security.ApiKeyAuthenticationProvider, Unicorn"> <param desc="API Key">YOUR-SECURE-KEY</param> </authenticationProvider>

Không làm vậy → ai cũng sync được → nguy hiểm!


📦 10. TDS (Team Development for Sitecore)

Nếu team bạn dùng TDS, đây là workflow:

  • Người dùng chọn item trong Sitecore → “Add to TDS”

  • Build sẽ tạo file .item

  • Deploy bằng MSBuild

  • Hỗ trợ update package

  • Có plugin để sync thông minh

TDS phù hợp Enterprise, còn Unicorn phù hợp mọi dự án.


🧪 11. Thực hành bạn nên làm hôm nay

✔ Tạo 1 Unicorn config cho Project

✔ Serialize Template + Renderings của bạn

✔ Thử Sync trên môi trường mới

✔ Commit files YAML vào Git

Donald Trump

Để trở thành người chiến thắng, bạn cần biết khi nào là đủ. Đôi khi trong cuộc sống, bạn phải từ bỏ cuộc chiến và chuyển sang mục tiêu mới mang lại hiệu quả hơn

Related Post