migarating calendatar data from shared exchange 2010 calendar to sharepoint 2013

       public static void AddAppointmentToSharepointWithClientObjectModule(DateTime s,DateTime e,string title,string desc)
        {
            string siteUrl = "https://intern.abc.net.cn/";
            ClientContext clientContext = new ClientContext(siteUrl);
            NetworkCredential Cred = new NetworkCredential("test", "abcdefg", "domain");
            clientContext.Credentials = Cred;


            //end authentication
            Microsoft.SharePoint.Client.List oList = clientContext.Web.Lists.GetByTitle("cal");

            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(itemCreateInfo);
            oListItem["Title"] = title;
            oListItem["Description"] = "New Event created using SharePoint Object Model";
            //oListItem["Location"] = "First Floor";
            oListItem["EventDate"] = s;
            oListItem["EndDate"] = e;
            //oListItem["Category"] = "Business";
            //oListItem["fAllDayEvent"] = false;
            oListItem.Update();

            clientContext.ExecuteQuery();

        }


        public static void GetExchangePublicCalendar()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            ExData.ExchangeService service = new ExData.ExchangeService(ExData.ExchangeVersion.Exchange2010_SP2);
            service.Credentials = new NetworkCredential("test", "abcdefg", "domain");
            service.Url = new Uri("https://mail.abc.local/ews/Exchange.asmx");
            
            
            //service.Credentials = new NetworkCredential("test", "abcdefg", "temp");
            //service.Url = new Uri("https://cas.temp.local/ews/Exchange.asmx");

            var rootfolder = ExData.Folder.Bind(service, ExData.WellKnownFolderName.PublicFoldersRoot);
            rootfolder.Load();
            foreach (ExData.Folder folder in rootfolder.FindFolders(new ExData.FolderView(int.MaxValue)))
            {
                if (!string.IsNullOrEmpty(folder.DisplayName))
                {
                    Console.WriteLine(folder.DisplayName);
                    try
                    {
                        foreach (ExData.Folder f1 in folder.FindFolders(new ExData.FolderView(int.MaxValue)))
                        {
                            if (f1.DisplayName.ToLower() == "calender")
                            {
                                Console.WriteLine("   ---------");
                                sb.AppendLine("   ---------");
                                var _cal = f1.Id;
                                var _calendarView = new Microsoft.Exchange.WebServices.Data.CalendarView(System.DateTime.Now.AddYears(-6).AddYears(-1), System.DateTime.Now.Date.AddYears(-6).AddYears(1));
                                foreach (Microsoft.Exchange.WebServices.Data.Appointment appointmentItem in service.FindAppointments(_cal, _calendarView))
                                {
                                    AddAppointmentToSharepointWithClientObjectModule(appointmentItem.Start, appointmentItem.End,appointmentItem.Subject,"");
                                    Console.WriteLine(appointmentItem.Subject);
                                    sb.AppendLine(appointmentItem.Subject);
                                    Console.WriteLine(appointmentItem.Start);
                                    sb.AppendLine(appointmentItem.Start.ToString());
                                    Console.WriteLine(appointmentItem.End);
                                    sb.AppendLine(appointmentItem.End.ToString());
                                    //appointmentItem.Load();
                                    //Console.WriteLine(appointmentItem.Body);

                                }
                                Console.WriteLine("   ---------");
                                sb.AppendLine("   ---------");

                            }
                            Console.WriteLine(" ++" + f1.DisplayName);
                            foreach (ExData.Folder f2 in f1.FindFolders(new ExData.FolderView(int.MaxValue)))
                            {
                                Console.WriteLine("    ++" + f2.DisplayName);
                                sb.AppendLine("    ++" + f2.DisplayName);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(" --" + ex.Message);
                        sb.AppendLine(" --" + ex.Message);
                    }
                }
            }
            log(sb.ToString());
        }

        static void log(string str)
        { 
            System.IO.File.AppendAllText(@"c:\log.txt",str);
        }

 

posted @ 2013-05-24 08:48  zyip  阅读(309)  评论(0编辑  收藏  举报