简体   繁体   中英

How to Dynamically fetch the fields from item in Sitecore using C# code?

I have a template called ' Footer ' which contains a Rich-Text Field.

带富文本字段的页脚模板

Using the Footer Template I have created an Item called ' FooterComponent ' and filled in the Rich-Text Field.

填充了RTF文本字段的FooterComponent项

I have a sublayout called ' Footer-Sublayout ' and Mapped the Visual studio sublayout with this.

Using Footer-Sublayout C# code I have to fetch the Rich-Text field from the 'FooterComponent' and display it in my Home Page.

You need to add the footer sublayout to the presentation detail of the homepage and bind it to the placeholder that corresponds to the one in the code.

In the code behind of the footer sublayout, you will then need to fetch the value from the rich text field of the item. So, first get the footer item either by path or template ID. Second, read the value of the rich text field.

Thanks

Give your Footer-Sublayout a datasource. In your code you can request the datasource of the rendering/sublayout (the exact how can differ, depending on the sitecore version, frameworks used (glass, ..) but can be easily found).

By using a datasource (and not hardcoding the item in your code) you give your editors the ability to change the item without changing code - or use the personalization/testing features of Sitecore.

eg (in v7 without frameworks):

public Item GetDataSource(Control parent)
{
  var sublayout = parent as Sublayout;
  return sublayout != null ? Context.Database.GetItem(sublayout.DataSource) : null;
}

Use the parent of your control in this function to retrieve the datasource item. Once you have the item, you can attach it to a FieldRenderer or fetch the fields in code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM