Datasource Item là khái niệm cực kỳ quan trọng vì nó giúp tách biệt thiết kế (rendering) và nội dung (content).
Nhờ datasource, cùng một component có thể hiển thị nội dung khác nhau tùy theo nơi nó được dùng (trang A → text khác, trang B → text khác).
Khi bạn thêm một component lên page (ví dụ: Banner, Promo box, Article block), Sitecore cho phép bạn chọn một mục trong Content Tree làm datasource.
Component sẽ đọc dữ liệu từ item này.
Component: Promo Box
Template datasource: Promo Data
Fields: Title, Image, CTA
📌 Khi content author kéo component vào trang, họ chọn:/sitecore/content/MySite/Data/Promotions/SummerSale
Component sẽ đọc fields từ item đó.
➡️ Điều này giúp:
Tái sử dụng dữ liệu nhiều nơi
Tách nội dung ra khỏi logic rendering
Dễ bảo trì và mở rộng
Khi tạo rendering (View Rendering / Controller Rendering), bạn có thể set:
Nơi hiển thị tree chọn item datasource, ví dụ:
/sitecore/content/MySite/Data/Promotions/*
Giới hạn template được phép chọn:
Promo Item Template
📌 Điều này bảo đảm tác giả nội dung không chọn nhầm item.
Bạn cấu hình điều này trong:
Rendering Item (trong /sitecore/layout/Renderings/…)
Hoặc Rendering Parameters
Có 2 cách:
Kéo component vào Page Editor → popup chọn datasource.
Chọn component → nút "Choose Data Source"
📌 Quan trọng nhất:
RenderingContext.Current.Rendering.Item → Datasource Item
Sitecore.Context.Item → Page Item
→ Controller Rendering thường dùng datasource, không nên viết logic sai dùng Page Item.
Có component không cần datasource:
✔ Navigation (menu)
✔ Breadcrumb
✔ Header, Footer
✔ Page Title (lấy từ Page Item)
Khi đó datasource để trống.
✔ Luôn cấu hình Datasource Location
✔ Dùng Template để hạn chế datasource hợp lệ
✔ Không hardcode đường dẫn
✔ Tách logic vào model
✔ Không dùng Page Item thay cho Datasource
Hôm nay bạn đã nắm:
Datasource Item là gì
Cách cấu hình datasource
Cách lấy datasource trong View/Controller
Phân biệt Page Item vs Datasource Item
Best practices khi dùng datasource