检索Outlook日历工作时间

时间:2014-05-23 15:52:41

标签: exchangewebservices managed-ews

我正在使用Exchanged Web服务,并希望检索用户“工作时间”。工作时间是日历上的设置,有助于自由/忙碌计算,但我想获得或计算实际值。

我可以完全访问日历。如果我可以使用EWS托管API,那将是我的偏好。我在网上搜索过,看过GetUserAvailability操作,但我找不到能给我这个数据的方法。

2 个答案:

答案 0 :(得分:2)

如果您使用Exchange 2010或更高版本,则可以使用EWS中的GetUserConfiguration操作从IPM.Configuration.WorkHours UserConfiguration FAI对象(文件夹关联项)获取工作时间配置(记录在http://msdn.microsoft.com/en-us/library/ee202895(v=exchg.80).aspx中){{3 }。例如

UserConfiguration usrConfig = UserConfiguration.Bind(service, "WorkHours", WellKnownFolderName.Calendar, UserConfigurationProperties.All);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new MemoryStream(usrConfig.XmlData));
XmlNodeList nlList =  xmlDoc.GetElementsByTagName("WorkHoursVersion1");
Console.WriteLine(nlList.Item(0).InnerXml);

答案 1 :(得分:1)

我想我会为VBA更新它,我知道它是一个旧线程,但可以帮助人们并节省一些时间。我编写了以下内容,以便在Excel中使用以获取Outlook日历设置。欢迎提供任何有关更好/更简洁代码编写的反馈和技巧。

Function GetUserWorkingHours(WHType As String, oCalendarFolder As Object) As String
' Returns user's Calendar Start or End work times
' Uses existing Outlook calendar folder object
' The workinghours data is stored in a hidden Outlook storage binary stream in xml format (no, seriously, it is!)
' ... with a sign on the door saying "beware of the leopard"
'
' Cheshire Catalyst software July 2020
'
Dim olStorage   As Object
Dim olPropacc   As Object
Dim olBytes()   As Byte
Dim a           As Variant
Dim xmlString   As String       ' xml stream text stored here
Dim objDOM      As Object       ' xml object to parse the xml stream
Dim Result      As String       ' Holding place for return value

    ' Loads the hidden Outlook xml store to retrieve WorkingHours
    Set olStorage = oCalendarFolder.GetStorage("IPM.Configuration.workhours", 2)
    Set olPropacc = olStorage.PropertyAccessor
    olBytes = olPropacc.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x7C080102")

    ' Translate binary stream into text byte by byte (there may be a better way to do this but this way works)
    For Each a In olBytes
        xmlString = xmlString & Chr(a)
    Next a

    ' Generate the xml object to parse
    Set objDOM = CreateObject("Msxml2.DOMDocument.3.0")

    ' Load the xml stream into the xml parser
    objDOM.LoadXML xmlString

    ' Filter on what we are looking for
    Select Case WHType
        Case "Start"
            Result = objDOM.SelectSingleNode("Root/WorkHoursVersion1/TimeSlot/Start").Text
        Case "End"
            Result = objDOM.SelectSingleNode("Root/WorkHoursVersion1/TimeSlot/End").Text
        Case Else
            ' Perhaps we should have tested for this before all that messing about with Outlook stores
            Result = "Invalid"     ' Invalid request
    End Select

    GetUserWorkingHours = Result

    ' Tidy up all those objects
    Set olStorage = Nothing
    Set olPropacc = Nothing
    Set objDOM = Nothing
    Erase olBytes
End Function

Sub testit()
Dim oOutlook    As Object   ' Outlook instance
Dim oNS         As Object   ' Outlook namespace
Dim oCalendar   As Object   ' Calendar folder of Outlook instance
    Set oOutlook = GetObject(, "Outlook.Application")
    Set oNS = oOutlook.GetNamespace("MAPI")
    Set oCalendar = oNS.GetDefaultFolder(9)

    MsgBox ("Start: " & GetUserWorkingHours("Start", oCalendar) & " End: " & GetUserWorkingHours("End", oCalendar))
End Sub