using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Exchange.WebServices.Data;
using System.IO;



namespace CreateAppointment
{
    class Program
    {
        static void Main(string[] args)
        {
           
            if (args.Length<1)
            {
                Console.WriteLine("the format of this application must be like following:");
                Console.WriteLine("createAppointment <jobtype> <csvFileName>");
                    Environment.Exit(0);
            }
         string filePath = args[1];
            switch (args[0])
            {
                case "addstu":
                    Console.WriteLine("Start creating student calendar");
                    addStu(args[1]);
                    break;
                    
                case "addteacher":
                    Console.WriteLine("Start creating teacher calendar");
                    addTeacher(args[1]);
                    break;

                case "delteacher":
                    Console.WriteLine("Start deleting teacher calendar");
                    delTeacher(args[1]);
                    break;
                case "addroom":
                    Console.WriteLine("Start creating room calendar");
                    addRoom(args[1]);
                    break;
                case "delroom":
                    Console.WriteLine("Starting deleting room calendar");
                    delRoom(args[1]);
                    break;

                case "delstu":
                    Console.WriteLine("start deleting student appoint");
                    delStu(args[1]);
                    break;

                default:
                    Console.WriteLine("Can not find the spicified job type, try again");
                    Environment.Exit(0);
                    break;
            }
          }
         static public void addStu(string csvFilePath)
               {
                   
             var reader = new StreamReader(File.OpenRead(csvFilePath));
       
                 while (!reader.EndOfStream)
                   {
                     var line = reader.ReadLine();
                       var values = line.Split(',');

                       //administrator,11237,5,1,B8,RF00,11
                     //username,classname,day,period,location,teacher,group
                     //values0 ,   1,      2,   3,     4      , 5     , 6
                       string emailAddress = values[0] + "@emailaddress";
                     string subject = values[1] + "-" + values[4] + "-" + values[5];
                     addApp(emailAddress, values[4],subject, values[2], values[3], values[1]);
                   //addApp(string emailAddress, string location, string subject, string day, string period,string body)
                   }
                  }

         static public void delStu(string csvFilePath)
         {
             var reader = new StreamReader(File.OpenRead(csvFilePath));

             while (!reader.EndOfStream)
             {
                 var line = reader.ReadLine();
                 var values = line.Split(',');
                 delApp(values[0] + "@emailaddress", values[1]);
             }
         }
         static public void addRoom(string csvFilePath)
         {
             var reader = new StreamReader(File.OpenRead(csvFilePath));

             while (!reader.EndOfStream)
             {
                 var line = reader.ReadLine();
                 var values = line.Split(',');
                 //addapp(string emailAddress, string location, string subject, string day, string period,string body)
                 addApp(values[1] + "@emailaddress", values[1], values[0] + "-" + values[1] + "-" + values[2], values[3], values[4], values[0]);
             }
  
         }
         static public void delRoom(string csvFilePath)
         {
             var reader = new StreamReader(File.OpenRead(csvFilePath));

             while (!reader.EndOfStream)
             {
                 var line = reader.ReadLine();
                 var values = line.Split(',');
                 //values    classcode, locaiton, teachercode,day,period,homegroup
                 //values       0           1         2         3    4       5
                 //addapp(string emailAddress, string location, string subject, string day, string period,string body)
                 string emailAddress = values[1] + "@emailaddress";
                 delApp(emailAddress, values[0]);
             }

         }
         static public void addTeacher(string csvFilePath)
         {
             var reader = new StreamReader(File.OpenRead(csvFilePath));

             while (!reader.EndOfStream)
             {
                 var line = reader.ReadLine();
                 var values = line.Split(',');
                 //values    classcode, locaiton, teachercode,day,period,homegroup
                 //values       0           1         2         3    4       5
                 //addapp(string emailAddress,                string location,  string subject, string day, string period,string body)
                 string emailAddress = values[2] + "@emailaddress";
                 string subject = values[0] + "-" + values[1] + "-" + values[2];
                 addApp(emailAddress, values[1],    subject     , values[3], values[4], values[0]);
             }

         }
         static public void delTeacher(string csvFilePath)
         {
             var reader = new StreamReader(File.OpenRead(csvFilePath));

             while (!reader.EndOfStream)
             {
                 var line = reader.ReadLine();
                 var values = line.Split(',');
                 //values = 8SCI3,a4,AM01,1,4,8U
                 //values    classcode, locaiton, teachercode,day,period,homegroup
                 //values       0           1         2         3    4       5
                 //addapp(string emailAddress, string location, string subject, string day, string period,string body)
                 string emailAddress = values[2] + "@emailaddress";
                 delApp(emailAddress, values[0]);
             }

         }


//#########delete Appointment for student###################################################################
        static public void delApp(string emailAddress, string classname)
        {
                       
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.Credentials = new WebCredentials("administrator", "<Password>", "");
            service.AutodiscoverUrl(emailAddress);
            service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);
            ItemView view = new ItemView(1);
            view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
            SearchFilter sfSearchFilter;
            FindItemsResults<Item> findResults;
            sfSearchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, classname );
            findResults = service.FindItems(WellKnownFolderName.Calendar, sfSearchFilter, view);
            foreach (Item item in findResults.Items)
            {
                Appointment appt = Appointment.Bind(service, new ItemId(item.Id.ToString()));
                Console.WriteLine(item.Id.ToString());
                appt.Delete(DeleteMode.MoveToDeletedItems);             
            }
         }
        static public void addEachRoomApp(string classname, string location, string teachername, string day, string period,string homegroup)
        {
            string emailAddress = location + "@emailaddress";
            string subject = classname + "-" + location + "-" + teachername;
            string Body = subject;
            
                   }
        static public void addApp(string emailAddress, string location, string subject, string day, string period,string body)
        {
            DateTime termStart = new DateTime(2013, 10, 7, 10, 0, 0);
            DateTime termFinish = new DateTime(2013, 12, 20);
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.Credentials = new WebCredentials("administrator", "<Password>", "");
            service.AutodiscoverUrl(emailAddress);
            Appointment appointment = new Appointment(service);
            // Set properties on the appointment.
            appointment.Subject =subject;
            appointment.Body = body;
            appointment.Start = termStart;
            appointment.End = appointment.Start.AddHours(1.2);
            appointment.Location = location;
            service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);

            // Set the recurrence information on the appointment.
            // The appointment will recur on Monday and Wednesday of every second week, effective January 1, 2009, through March 31, 2009.
            DayOfTheWeek days = new DayOfTheWeek();
            switch (day)
            {

                case "1":
                    days = DayOfTheWeek.Monday;
                    break;
                case "2":
                    days = DayOfTheWeek.Tuesday;
                    break;
                case "3":
                    days = DayOfTheWeek.Wednesday;
                    break;
                case "4":
                    days = DayOfTheWeek.Thursday;
                    break;
                case "5":
                    days = DayOfTheWeek.Friday;
                    break;
            }

            appointment.Recurrence = new Recurrence.WeeklyPattern(appointment.Start.Date, 1, days);
            appointment.Recurrence.StartDate = appointment.Start.Date;
            appointment.Recurrence.EndDate = termFinish;
            // Save the recurring series.
            appointment.Save(WellKnownFolderName.Calendar, SendInvitationsMode.SendToNone);
        }    
    }
   }
      
    

 

The following code is for manage appointment through csv.

 

About Lei

I am an IT specialist with over 10 year experience - years on Automation, on-Premise or Azure.

I am happy to develop however never want be a full time developer. Only do what I have to do. If it has to be PowerShell,HTML, PHP, CSS, C#, VBS or JS, front end or backend, so be it, doesn't matter!

Spent years with Windows, SCCM, SharePoint, SQL and Exchange servers. For last several years, I have been actively working under On Premise > Azure environment.

THERE IS NO WAY BACK!!!

Current Certificates:
    Microsoft® Certified-
  • -Enterprise Administrator
  • -Database Administrator
  • -SharePoint Administrator
  • -Administering and Deploying SCCM 2012
Red Hat Certified Technician
ITIL V3 Foundation - Practitioner

Working on Azure Certificates now and hopefully they can stop upgrading their questions one day! GIVE ME A BREAK!

Contact Lei

Name *
Email *
Comments *

Traffic since 10/08/2016

Today13
Yesterday83
This week637
This month1933
Total416293

Visitor Info

  • IP: 54.221.73.186
  • Browser: Unknown
  • Browser Version:
  • Operating System: Unknown

Who Is Online

1
Online

2017-12-17

Login