SharePoint Add/Send Appointment Using Exchange Web Services (EWS)

Add Web Service  : https://domain/ews/services.wsdl


try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    EWS.ExchangeServiceBinding esb = new ExchangeServiceBinding();

                    esb.RequestServerVersionValue = new RequestServerVersion();

                    esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010_SP1;
                    //URL = https://domain/EWS/exchange.asmx
                    esb.Url = CommonConstants.Instance.CONST_EWS_EXCHANGE_WEBSERVICE_URL;
                    
                    esb.Credentials = new System.Net.NetworkCredential(CommonConstants.Instance.CONST_EWS_USERNAME,
                        CommonConstants.Instance.CONST_EWS_PASSWORD,
                        CommonConstants.Instance.CONST_EWS_DOMAIN);
                    esb.PreAuthenticate = true;

                    System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, errors) => true);

                    #region Create the appointment.
                    CalendarItemType appointment = new CalendarItemType();
                    // Add item properties to the appointment.
                    appointment.Body = new BodyType();
                    appointment.Body.BodyType1 = BodyTypeType.HTML;
                    appointment.Body.Value = _body;
                    //appointment.Categories = new string[] { "Category1", "Category2" };
                    appointment.Importance = ImportanceChoicesType.High;
                    appointment.ImportanceSpecified = true;
                    appointment.ItemClass = "IPM.Appointment";
                    appointment.Subject = _subject;
                    appointment.LegacyFreeBusyStatus = EWS.LegacyFreeBusyType.Busy;
                    appointment.LegacyFreeBusyStatusSpecified = true;
                    appointment.ReminderIsSet = true;
                    appointment.ReminderIsSetSpecified = true;
                    appointment.ReminderMinutesBeforeStart = CommonConstants.Instance.CONST_EWS_APPOINTMENT_REMINDER;
                    appointment.Location = _location;
                    // Add calendar properties to the appointment.
                    appointment.Start = new DateTime(_startdate.Year, _startdate.Month, _startdate.Day, _startdate.Hour + 1, _startdate.Minute, _startdate.Second, DateTimeKind.Local);
                    appointment.StartSpecified = true;
                    appointment.End = new DateTime(_enddate.Year, _enddate.Month, _enddate.Day, _enddate.Hour + 1, _enddate.Minute, _enddate.Second, DateTimeKind.Local);
                    appointment.EndSpecified = true;

                    // Setup the RequiredAttendees array
                    appointment.RequiredAttendees = new AttendeeType[_attendees.Count];

                    int int_attendee = 0;
                    foreach (string email in _attendees)
                    {
                        appointment.RequiredAttendees[int_attendee] = new AttendeeType();

                        appointment.RequiredAttendees[int_attendee].Mailbox = new EmailAddressType();

                        appointment.RequiredAttendees[int_attendee].Mailbox.EmailAddress = email;

                        appointment.RequiredAttendees[int_attendee].Mailbox.RoutingType = "SMTP";

                        int_attendee++;

                    }
                    #endregion

                    // Identify the destination folder that will contain the appointment.
                    DistinguishedFolderIdType folder = new DistinguishedFolderIdType();
                    folder.Id = DistinguishedFolderIdNameType.calendar;

                    // Create the array of items that will contain the appointment.
                    NonEmptyArrayOfAllItemsType arrayOfItems = new NonEmptyArrayOfAllItemsType();
                    arrayOfItems.Items = new ItemType[1];

                    // Add the appointment to the array of items.
                    arrayOfItems.Items[0] = appointment;

                    #region Create the CreateItem request.
                    CreateItemType createItemRequest = new CreateItemType();

                    // The SendMeetingInvitations attribute is required for calendar items.
                    createItemRequest.SendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToAllAndSaveCopy;
                    createItemRequest.SendMeetingInvitationsSpecified = true;

                    // Add the destination folder to the CreateItem request.
                    createItemRequest.SavedItemFolderId = new TargetFolderIdType();
                    createItemRequest.SavedItemFolderId.Item = folder;

                    // Add the items to the CreateItem request.
                    createItemRequest.Items = arrayOfItems;
                    #endregion

                    try
                    {
                        // Send the request and get the response.
                        CreateItemResponseType createItemResponse = esb.CreateItem(createItemRequest);

                        // Get the response messages.
                        ResponseMessageType[] rmta = createItemResponse.ResponseMessages.Items;

                        //foreach (ResponseMessageType rmt in rmta)
                        //{
                        //    //rmt.ResponseCode = ResponseCodeType.NoError
                        //    ArrayOfRealItemsType itemArray = ((ItemInfoResponseMessageType)rmt).Items;
                        //    ItemType[] items = itemArray.Items;

                        //    // Get the item identifier and change key for each item.
                        //    foreach (ItemType item in items)
                        //    {

                        //    }
                        //}
                    }
                    catch (Exception ex)
                    {
                        CommonHelper.ExceptionHandler(ex);
                    }
                });
            }
            catch (Exception ex)
            {
                CommonHelper.ExceptionHandler(ex);
            }

Comments

Popular posts from this blog

SQL SERVER – FIX : Error 3154: The backup set holds a backup of a database other than the existing database

SharePoint Close methods for SP Long Operations and Redirect Error Page

The model backing the context has changed since the database was created. Consider using Code First Migrations to update the database