Retrieve RelatedItem Custom Field From Events Module
I have added a Custom Field named "MyImage" to Events Module.
I need to retrieve it from code behind. How can I do that? My RelatedMedia field is basically an Image that i want to retrieve for an Event.
I was hoping something like this would work, but it doesn't:
var mgr = EventsManager.GetManager();
Event ev = mgr.GetEvents().Where(e => e.UrlName == urlName).FirstOrDefault();
var photo = obj.GetValue("MyImage");
MyImage is a custom field of type RelatedMedia (Image) added in the Events module through the Events Content page(Right hand menu).
Kindly help, I am stuck with this for hours.
Anyone?
Hello,
It is unclear in your small code snippet what "obj" is in obj.GetValue("MyImage").
The following sample worked for me to retrieve the related image and it's info.
EventsManager mgr = EventsManager.GetManager();
Event eventItem = mgr.GetEvents().FirstOrDefault();
eventItem = mgr.Lifecycle.GetLive(eventItem)
as
Event;
var imageField = eventItem.GetRelatedItems<Telerik.Sitefinity.Libraries.Model.Image>(
"MyImage"
).SingleOrDefault();
var imageurl = imageField.Url;
var imageid = imageField.Id;
LibrariesManager librariesManager = LibrariesManager.GetManager();
Telerik.Sitefinity.Libraries.Model.Image image = librariesManager.GetImages().Where(i => i.Id == imageid).FirstOrDefault();
if
(image !=
null
)
image = librariesManager.Lifecycle.GetMaster(image)
as
Image;
Dimitri,
Thanks alot for replying. I tried your approach, But I am unable to call GetRelatedItems method on eventItem. There is no method available named GetRelatedItems on Event object.
Also, in my initial question 'obj' is 'Event' i.e eventItem.
Please see the attached screenshot. Why am I not getting the GetRelatedItems method through Event's object.
GetRelatedItems<>() is however available using EventsManager's object. But, its takes too many values as parameters (Screenshot2 attached) & I am not sure what values to pass to it.
Thanks, Looking forward to your response.
Hello,
Please make sure you add the following using statement:
using
Telerik.Sitefinity.RelatedData;
Well, thats embarassing. I tried Control+Dot on it but it wouldn't come up in intellisense.
Thanks for the help man.