/// <summary>
		/// Inserts the specified appointment.
		/// </summary>
		/// <param name="owner">The owner RadScheduler instance.</param>
		/// <param name="appointmentToInsert">The appointment to insert.</param>
		public override void Insert(RadScheduler owner, Appointment appointmentToInsert)
		{
			CreateRecurrenceExceptionContext createExceptionContext = owner.ProviderContext as CreateRecurrenceExceptionContext;
			if (createExceptionContext != null)
			{
				Debug.Assert(appointmentToInsert.RecurrenceState == RecurrenceState.Exception);
				InsertRecurrenceException(owner, appointmentToInsert, createExceptionContext.RecurrenceExceptionDate);
				return;
			}

          
			CalendarItemType calendarItem = CreateCalendarItem(owner, appointmentToInsert);
     
			CreateItemType createItemRequest = new CreateItemType();

            DistinguishedFolderIdType destFolder = new DistinguishedFolderIdType();
            destFolder.Id = DistinguishedFolderIdNameType.calendar;

            EmailAddressType emailAddressType = new EmailAddressType();
            emailAddressType.EmailAddress = this.CalendarNames;

            destFolder.Mailbox = emailAddressType;

            createItemRequest.SavedItemFolderId = new TargetFolderIdType();
            createItemRequest.SavedItemFolderId.Item = destFolder;

            
			createItemRequest.SendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToNone;
			createItemRequest.SendMeetingInvitationsSpecified = true;
			createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
			createItemRequest.Items.Items = new CalendarItemType[] { calendarItem };
           
			CreateItemResponseType response = Service.CreateItem(createItemRequest);
			ResponseMessageType responseMessage = response.ResponseMessages.Items[0];

			if (responseMessage.ResponseCode != ResponseCodeType.NoError)
			{
				throw new Exception("CreateItem failed with response code " + responseMessage.ResponseCode);
			}
		}
        /// <summary>
        /// Finds all Calendar Items for the current User's Mailbox.
        /// </summary>
        /// <returns></returns>
        //protected internal CalendarItemType[] FindCalendarItems()
        //{
        //    // Identify which folders to search.
        //    DistinguishedFolderIdType[] parentFolderIds = new DistinguishedFolderIdType[1];

        //    parentFolderIds[0] = new DistinguishedFolderIdType();
        //    parentFolderIds[0].Id = DistinguishedFolderIdNameType.calendar;

        //    return FindCalendarItems(parentFolderIds);
        //}

		protected internal CalendarItemType[] FindCalendarItems(DistinguishedFolderIdType[] parentFolderIds)
		{
			// Form the FindItem request.
			FindItemType findItemRequest = new FindItemType();

			// Define the item properties that are returned in the response.
			ItemResponseShapeType itemProperties = new ItemResponseShapeType();
			itemProperties.BaseShape = DefaultShapeNamesType.IdOnly;
			
			PathToUnindexedFieldType calendarIsRecurringFieldPath = new PathToUnindexedFieldType();
			calendarIsRecurringFieldPath.FieldURI = UnindexedFieldURIType.calendarIsRecurring;
			
			PathToUnindexedFieldType calendarItemTypeFieldPath = new PathToUnindexedFieldType();
			calendarIsRecurringFieldPath.FieldURI = UnindexedFieldURIType.calendarCalendarItemType;

			PathToUnindexedFieldType calendarStartFieldPath = new PathToUnindexedFieldType();
			calendarStartFieldPath.FieldURI = UnindexedFieldURIType.calendarStart;

			PathToUnindexedFieldType calendarEndFieldPath = new PathToUnindexedFieldType();
			calendarEndFieldPath.FieldURI = UnindexedFieldURIType.calendarEnd;

            //location
            //PathToUnindexedFieldType calendarLocation = new PathToUnindexedFieldType();
            //calendarLocation.FieldURI = UnindexedFieldURIType.calendarLocation;
            //// body
            //PathToUnindexedFieldType itemBody = new PathToUnindexedFieldType();
            //itemBody.FieldURI = UnindexedFieldURIType.itemBody;

			itemProperties.AdditionalProperties = new PathToUnindexedFieldType[]
			                                      	{
			                                      		calendarIsRecurringFieldPath, 
			                                      		calendarItemTypeFieldPath,
			                                      		calendarStartFieldPath,
			                                      		calendarEndFieldPath
			                                      	};
			findItemRequest.ItemShape = itemProperties;

			findItemRequest.ParentFolderIds = parentFolderIds;

			// Define the sort order of items.
			FieldOrderType[] fieldsOrder = new FieldOrderType[1];
			fieldsOrder[0] = new FieldOrderType();
			PathToUnindexedFieldType subjectOrder = new PathToUnindexedFieldType();
			subjectOrder.FieldURI = UnindexedFieldURIType.calendarStart;
			fieldsOrder[0].Item = subjectOrder;
			fieldsOrder[0].Order = SortDirectionType.Ascending;
			findItemRequest.SortOrder = fieldsOrder;

			// Define the traversal type.
			findItemRequest.Traversal = ItemQueryTraversalType.Shallow;

			// Send the FindItem request and get the response.
			FindItemResponseType findItemResponse = Service.FindItem(findItemRequest);

			// Access the response message.
			ArrayOfResponseMessagesType responseMessages = findItemResponse.ResponseMessages;
			ResponseMessageType responseMessage = responseMessages.Items[0];

			if (responseMessage is FindItemResponseMessageType)
			{
				FindItemResponseMessageType firmt = (responseMessage as FindItemResponseMessageType);
				FindItemParentType fipt = firmt.RootFolder;
				object obj = fipt.Item;

				if (obj is ArrayOfRealItemsType)
				{
					ArrayOfRealItemsType items = (obj as ArrayOfRealItemsType);

					if (items.Items != null)
					{
						List<CalendarItemType> calendarItems = new List<CalendarItemType>(items.Items.Length);
						foreach (ItemType item in items.Items)
						{
							CalendarItemType calendarItem = item as CalendarItemType;

							if (calendarItem != null)
							{
								calendarItems.Add(calendarItem);
							}
						}

						return calendarItems.ToArray();
					}

					return new CalendarItemType[0];
				}
			}

			return null;
		}
		/// <summary>
		/// Reads the appointments from the Exchange server.
		/// </summary>
		/// <param name="owner">The owner RadScheduler instance.</param>
		/// <returns></returns>
		public override IEnumerable<Appointment> GetAppointments(RadScheduler owner)
		{
            List<Appointment> appointments = new List<Appointment>();

		    foreach (Resource resource in owner.Resources)
		    {
                string sharedCalendarName = this.CalendarNames;// resource.Text;

                // Identify which folders to search.
                DistinguishedFolderIdType distinguishedFolderIdType = new DistinguishedFolderIdType();
                distinguishedFolderIdType.Id = DistinguishedFolderIdNameType.calendar;
               

                EmailAddressType emailAddressType = new EmailAddressType();
                emailAddressType.EmailAddress = sharedCalendarName;

                distinguishedFolderIdType.Mailbox = emailAddressType;

                List<ItemIdType> itemIds = new List<ItemIdType>();

                foreach (CalendarItemType item in FindCalendarItems(new DistinguishedFolderIdType[] { distinguishedFolderIdType }))
                {
                    if ((item.Start < owner.VisibleRangeEnd && item.End > owner.VisibleRangeStart) ||
                        item.CalendarItemType1 == CalendarItemTypeType.RecurringMaster)
                    {
                        itemIds.Add(item.ItemId);
                    }
                }

                appointments.AddRange(GetAppointments(owner, sharedCalendarName, itemIds.ToArray()));
		    }

            return appointments;
		}