Reputation: 97
Can anyone tell me how to read the custom Field value of the outlook using c#
Right now i tried "UserProperties" and "ItemProperties". Both are throwing error. My sample code as follows
Microsoft.Office.Interop.Outlook.Application f = new Microsoft.Office.Interop.Outlook.Application();
NameSpace outlookNS = f.GetNamespace("MAPI");
MAPIFolder inboxFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
foreach (object obj in inboxFolder.Items)
{
MailItem item = obj as MailItem;
if (item != null)
{
Console.WriteLine(item.UserProperties["test"].Value);
Console.WriteLine(item.ItemProperties["test"].Value);
}
}
Thanks in advance
Upvotes: 1
Views: 4116
Reputation: 133
This reply might be a bit late for you. But I came across the same problem as the original poster (need to extract values from User-Defined Fields (Custom fields) of Outlook).
Someone somewhere shared a VBA macro which did the job. So I went around looking for a way to execute Outlook macro VBA in powershell. As it turns out Outlook stopped supporting /autorun of macro after version 2003 SP2. I nearly switched from Outlook 2019 to Outlook 2003 just for the /autorun. Then it occurred to me Outlook 2003 isn't as good with showing multiple calendars side by side as Outlook 2019. So I decided to keep going with 2019 afterall. Cut long story short, I massaged the VBA macro to run in Powershell and it worked for me. I basically added in a few dollar signs in front of things, and removed some stuff here and there, and then it just worked. I hope it works for everyone else too who is still looking for answer:
Function Get-ContactUDF
{
Add-Type -assembly "Microsoft.Office.Interop.Outlook" -ErrorAction Stop -ErrorVariable "OutlookError"
$Outlook = New-Object -comobject Outlook.Application -ErrorAction stop -ErrorVariable "ApplicationError"
$objNameSpace = $Outlook.GetNameSpace("MAPI")
$objContacts = $objNameSpace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderContacts)
$objContact = $objContacts.Items.Find("[FirstName] = ""John""")
$objProperty = $objContact.UserProperties.Find("Favorite Character in Lord of the Rings")
$objProperty.Value
}
Just in case the code is not clear. This powershell function will look for the Contact whose FirstName is "John", and then find the value under the custom field called "Favorite Character in Lord of the Rings" and print out the value (for example Frodo). To run it, ofcourse you just do this under powershell:
Get-ContactUDF
By the way if you just want to print values from default fields (those built-in fields that came with Outlook eg. Firstname, Surname, Birthday, phone number, spouse name, title etc), then it's like the following:
Function Get-OutlookContacts
{
Add-Type -assembly "Microsoft.Office.Interop.Outlook" -ErrorAction Stop -ErrorVariable "OutlookError"
$Outlook = New-Object -comobject Outlook.Application -ErrorAction stop -ErrorVariable "ApplicationError"
$namespace = $Outlook.GetNameSpace("MAPI")
$contactObject = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderContacts)
}
Call this function by just typing:
Get-OutlookContacts
in Powershell
THIS WILL LIST EVERYTHING IN THE CONTACTS DATABASE:
$contactObject.Items
THIS WILL LIST ALL OBJECTS FIRSTNAME, LASTNAME, BIRTHDAY, EMAILADDRESS, MOBILE IN THE CONTACTS DATABASE:
$contactObject.Items | Select-Object -Property FirstName, LastName, Birthday, Email1Address, MobileTelephoneNumber
ISSUE this next command to grab objects and list only for where the firstname object equals Rambo:
$contactObject.Items | Select-Object -Property FirstName, LastName, Birthday, Email1Address, MobileTelephoneNumber | where-object {$_.Firstname -eq "Rambo" }
Hope these examples are clear. I use Outlook 2019 on Windows 10 totally offline mode (no cloud, no 365). If you use 365 then the code above probably needs modifications.
I also remember other people asking for a way to extract Outlook Calendar events using Powershell. If I come across that question again I'll post the answer. Basically the same drill. I read some VB code from somewhere and then turned it into Powershell script. You guys can do it yourself if you like. Just add in dollar signs and remove the DIM lines.
Upvotes: 1
Reputation: 12405
This answer has been rewritten following experiments with Outlook.
My C# is good enough for me to know what you are doing but I have not tried to access Outlook from C# yet.
Both Items
and Item
are used within the Outlook model. I do not know it you can use item
in the way you are attempting.
UserProperties would throw an error if user property "test" did not exist. Below I show how to test for existence. Are you adding a user property and forgetting to save the amended mail item?
The following shows access to user properties from Outlook VBA. I would expect the InterOp model to be as similar as the syntax allows. Important differences of which I know:
Set
is not required with C#.Nothing
is the VBA equivalent of null
.Variables:
The following adds a user property with name "TestProp" and type olText, sets its value to "Value for TestProp" and saves the amended mail item. Without the Save, the previous statements have no effect.
With FolderItem
Set UserProp = .UserProperties.Add("TestProp", olText)
UserProp.Value = "Value for TestProp"
.Save
End with
The following outputs to the immediate window the name and value of every user property against the mail item.
For InxUP = 1 To .UserProperties.Count
Debug.Print " User prop " & InxUP & _
.UserProperties(InxUP).Name & " " & _
.UserProperties(InxUP).Value
Next
The following checks that user property "TestProp" exists and, if so, outputs its value to the immediate window.
Set UserProp = .UserProperties.Find("TestProp")
If Not UserProp Is Nothing Then
Debug.Print " TestProp " & .UserProperties("TestProp").Value
End If
Hope this helps
Upvotes: 1