New to Telerik UI for WinFormsStart a free 30-day trial

How to Import/Export ICalendar Data With Resources

Updated over 6 months ago

Environment

Product VersionProductAuthor
2024.2.514RadScheduler for WinFormsDinko Krastev

Description

The attached 2 classes provide support for resources in ICalendar Import/Export in RadScheduler.

Migrated from Telerik Code Library

Solution

C#
// Export
public class CustomSchedulerICalendarExporter : SchedulerICalendarExporter
{
   private SchedulerResourceCollection m_Resources;

   public CustomSchedulerICalendarExporter(SchedulerResourceCollection resources)
	  : base()
   {
	  m_Resources = resources;
   }

   protected override void WriteAdditionalDataForAppointment(IEvent appointment, CalObject calObject)
   {
	  base.WriteAdditionalDataForAppointment(appointment, calObject);

	  // new);
	  if (appointment.ResourceId != null)
	  {
		 string resources = BuildResources(appointment.ResourceIds);
		 calObject.AddProperty("RESOURCES", resources);
	  }
   }

   private string BuildResources(ObservableCollection<EventId> resourceIds)
   {
	  string res = string.Empty;

	  foreach (EventId resourceId in resourceIds)
	  {
		 string resource = m_Resources.GetById(resourceId.KeyValue).Name;
		 if (res == string.Empty)
			res += resource;
		 else
		 {
			res += "," + resource;
		 }
	  }

	  return res;
   }
}

// Import
public class CustomSchedulerICalendarImporter : SchedulerICalendarImporter
{
	private SchedulerResourceCollection m_Resources;

	public CustomSchedulerICalendarImporter(SchedulerResourceCollection resources)
		: base()
	{
		m_Resources = resources;
	}

	/// <summary>
	/// Remove duplicates based on UniqueId
	/// </summary>
	/// <exception cref="CalendarParseException"><c>CalendarParseException</c>.</exception>
	protected override void GetAppointments(CalObject calendar, ICollection<IEvent> collection)
	{
		base.GetAppointments(calendar, collection);

		Dictionary<EventId, IEvent> dictionary = new Dictionary<EventId, IEvent>();
		foreach (IEvent ievent in collection)
		{
		dictionary[ievent.UniqueId] = ievent;
		}

		collection.Clear();
		foreach (IEvent iEvent in dictionary.Values)
		collection.Add(iEvent);
	}

	/// <summary>
	/// Writes the additional data for appointment.
	/// </summary>
	/// <param name="appointment">The appointment.</param>
	/// <param name="calObject">The cal object.</param>
	protected override void ApplyAdditionalData(IEvent appointment, CalObject calObject)
	{
		base.ApplyAdditionalData(appointment, calObject);

		CalProperty prop = calObject["RESOURCES"];
		if (prop != null)
		{
		AddResourceIds(prop, appointment);
		}
	}

	private void AddResourceIds(CalProperty prop, IEvent appointment)
	{
		string[] resources = CalProperty.ToText(prop).Split(new[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);

		foreach (string resource in resources)
		{
		if (m_Resources.GetById(resource) == null)
			m_Resources.Add(new Resource(resource, resource));
		appointment.ResourceIds.Add(new EventId(resource));
		}
	}
}

In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support