Back To Home

Name: Ngày 19: Datasource Item

🎯 Ngày 19: Datasource Item trong Sitecore

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)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).


⭐ 1. Datasource là gì?

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.

Ví dụ:

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


⭐ 2. Cấu hình Datasource Location & Datasource Template

Khi tạo rendering (View Rendering / Controller Rendering), bạn có thể set:

Datasource Location

Nơi hiển thị tree chọn item datasource, ví dụ:

/sitecore/content/MySite/Data/Promotions/*

Datasource Template

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


⭐ 3. Gán Datasource cho một component

Có 2 cách:

Cách 1: Chọn datasource khi thêm component

Kéo component vào Page Editor → popup chọn datasource.

Cách 2: Chỉnh trong Experience Editor

Chọn component → nút "Choose Data Source"


⭐ 4. Lấy datasource trong code

View Rendering (.cshtml)

 
@{ var datasource = RenderingContext.Current.Rendering.Item; } <h2>@datasource["Title"]</h2> <img src="@datasource.ImageUrl("Image")" />

Controller Rendering

Controller

 
public ActionResult Promo() { var datasource = RenderingContext.Current.Rendering.Item; var model = new PromoModel(datasource); return View(model); }

View

 
<h2>@Model.Title</h2> <img src="@Model.ImageUrl" />

⭐ 5. Datasource vs Page Item

📌 Quan trọng nhất:

  • RenderingContext.Current.Rendering.ItemDatasource Item

  • Sitecore.Context.ItemPage Item

→ Controller Rendering thường dùng datasource, không nên viết logic sai dùng Page Item.


⭐ 6. Trường hợp component không dùng datasource

Có component không cần datasource:

✔ Navigation (menu)
✔ Breadcrumb
✔ Header, Footer
✔ Page Title (lấy từ Page Item)

Khi đó datasource để trống.


⭐ 7. Kiểm tra component có datasource hay không

 
var ds = RenderingContext.Current.Rendering.Item; if (ds == null) { // fallback từ Page Item hoặc ẩn component }

⭐ 8. Best Practices

✔ 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


🎉 Tổng kết Ngày 19

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

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