Skip to content

Commit 696d9c9

Browse files
committed
Fix: Add graphId to contacts to enable setting SOA
1 parent cb0d56a commit 696d9c9

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Administration/Contacts/Invoke-ListContacts.ps1

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using namespace System.Collections.Generic
22
using namespace System.Text.RegularExpressions
33

4-
Function Invoke-ListContacts {
4+
function Invoke-ListContacts {
55
<#
66
.FUNCTIONALITY
77
Entrypoint
@@ -18,10 +18,9 @@ Function Invoke-ListContacts {
1818
# Early validation and exit
1919
if (-not $TenantFilter) {
2020
return ([HttpResponseContext]@{
21-
StatusCode = [HttpStatusCode]::BadRequest
22-
Body = 'tenantFilter is required'
23-
})
24-
return
21+
StatusCode = [HttpStatusCode]::BadRequest
22+
Body = 'tenantFilter is required'
23+
})
2524
}
2625

2726
# Pre-compiled regex for MailTip cleaning
@@ -52,35 +51,35 @@ Function Invoke-ListContacts {
5251
$phones = if ($phoneCapacity -gt 0) {
5352
$phoneList = [List[hashtable]]::new($phoneCapacity)
5453
if ($Contact.Phone) {
55-
$phoneList.Add(@{ type = "business"; number = $Contact.Phone })
54+
$phoneList.Add(@{ type = 'business'; number = $Contact.Phone })
5655
}
5756
if ($Contact.MobilePhone) {
58-
$phoneList.Add(@{ type = "mobile"; number = $Contact.MobilePhone })
57+
$phoneList.Add(@{ type = 'mobile'; number = $Contact.MobilePhone })
5958
}
6059
$phoneList.ToArray()
6160
} else { @() }
6261

6362
return @{
64-
id = $Contact.Id
65-
displayName = $Contact.DisplayName
66-
givenName = $Contact.FirstName
67-
surname = $Contact.LastName
68-
mail = $mailAddress
69-
companyName = $Contact.Company
70-
jobTitle = $Contact.Title
71-
website = $Contact.WebPage
72-
notes = $Contact.Notes
73-
hidefromGAL = $MailContact.HiddenFromAddressListsEnabled
74-
mailTip = $cleanMailTip
63+
id = $Contact.Id
64+
displayName = $Contact.DisplayName
65+
givenName = $Contact.FirstName
66+
surname = $Contact.LastName
67+
mail = $mailAddress
68+
companyName = $Contact.Company
69+
jobTitle = $Contact.Title
70+
website = $Contact.WebPage
71+
notes = $Contact.Notes
72+
hidefromGAL = $MailContact.HiddenFromAddressListsEnabled
73+
mailTip = $cleanMailTip
7574
onPremisesSyncEnabled = $Contact.IsDirSynced
76-
addresses = @(@{
77-
street = $Contact.StreetAddress
78-
city = $Contact.City
79-
state = $Contact.StateOrProvince
80-
countryOrRegion = $Contact.CountryOrRegion
81-
postalCode = $Contact.PostalCode
82-
})
83-
phones = $phones
75+
addresses = @(@{
76+
street = $Contact.StreetAddress
77+
city = $Contact.City
78+
state = $Contact.StateOrProvince
79+
countryOrRegion = $Contact.CountryOrRegion
80+
postalCode = $Contact.PostalCode
81+
})
82+
phones = $phones
8483
}
8584
}
8685

@@ -98,20 +97,29 @@ Function Invoke-ListContacts {
9897
}
9998

10099
if (!$Contact -or !$MailContact) {
101-
throw "Contact not found or insufficient permissions"
100+
throw 'Contact not found or insufficient permissions'
102101
}
103102

104103
$ContactResponse = ConvertTo-ContactObject -Contact $Contact -MailContact $MailContact
105104

106105
} else {
107106
# Get all contacts - simplified approach
108-
Write-Host "Getting all contacts"
107+
Write-Host 'Getting all contacts'
109108

110109
$ContactResponse = New-EXORequest -tenantid $TenantFilter -cmdlet 'Get-Contact' -cmdParams @{
111-
Filter = "RecipientTypeDetails -eq 'MailContact'"
110+
Filter = "RecipientTypeDetails -eq 'MailContact'"
112111
ResultSize = 'Unlimited'
113112
} | Select-Object -Property City, Company, Department, DisplayName, FirstName, LastName, IsDirSynced, Guid, WindowsEmailAddress
114113

114+
# Add Graph ID to each contact based on email match
115+
$GraphContacts = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/contacts' -tenantid $TenantFilter
116+
foreach ($contact in $ContactResponse) {
117+
$GraphMatch = $GraphContacts | Where-Object { $_.mail -eq $contact.WindowsEmailAddress }
118+
if ($GraphMatch) {
119+
$contact | Add-Member -MemberType NoteProperty -Name 'graphId' -Value $GraphMatch.id -Force
120+
}
121+
}
122+
115123
# Return empty array if no contacts found
116124
if (!$ContactResponse) {
117125
$ContactResponse = @()
@@ -128,7 +136,7 @@ Function Invoke-ListContacts {
128136
}
129137

130138
return ([HttpResponseContext]@{
131-
StatusCode = $StatusCode
132-
Body = $ContactResponse
133-
})
139+
StatusCode = $StatusCode
140+
Body = $ContactResponse
141+
})
134142
}

0 commit comments

Comments
 (0)