Get specific field name from different template sections

In Sitecore; mainly we don't come across such problem that we have same names of fields in any Item in Content Tree. So as with me since today; when i got an issue that i had an item in which i have a field named Content in three sections of that Item; for which i was making Page.
and for each section i need to show "Content" in Tabs. i Google a lot but i could not find the Specific solution to this problem of mine. than i got a help form stackoverflow that how could i reach a field by its section name;
For working with fields in reference to Sections we need to involve Sitecore.Pipeline namespace.
Let me first copy the code to get Field data than i will elaborate it lIne by line.

public string GetContent(Item pageItem) {
string content = string.Empty;

Sitecore.Pipelines.RenderField.RenderFieldArgs args = new Sitecore.Pipelines.RenderField.RenderFieldArgs();
args.Item = pageItem;
content= args.Item[args.Item.Template.GetSection("Environmental Info").GetField("content").ID];


return content;
}

I created a function named "GetContent" with return type of "string," holding one parameter of type Sitecore.Data.Item".
In very first line of function I am creating a variable of string type

string content = string.Empty;

than create a Sitecore.Pipeline.RenderField.RenderFieldArgs object.
Sitecore.Pipelines.RenderField.RenderFieldArgs args = new Sitecore.Pipelines.RenderField.RenderFieldArgs();

Assign
PageItem parameter to args.Item
args.Item = pageItem;
now get args.Item's Template ID by getting GetSection() and GetField()
function and pass that ID as parameter name in args.item's array as described below. assign that value to content variable defined at the top.

content= args.Item[args.Item.Template.GetSection("Your Section Name").GetField("Your Field Name").ID];

return the content variable so that it can be used further.
return content;


That is all about accessing fields values through their sections.

enjoy coding....

Comments