Skip to content

Commit

Permalink
Merge pull request #1292 from kris6673/edit-contacts
Browse files Browse the repository at this point in the history
Update contact listing and editing functionality
  • Loading branch information
KelvinTegelaar authored Feb 4, 2025
2 parents c7a12b8 + 1ceba27 commit 60f3c3f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ Function Invoke-EditContact {

# Prepare the body for the Set-Contact cmdlet
$bodyForSetContact = [pscustomobject] @{
'DisplayName' = $contactInfo.DisplayName
'WindowsEmailAddress' = $contactInfo.mail
'Identity' = $contactInfo.ContactID
'DisplayName' = $contactInfo.displayName
'WindowsEmailAddress' = $contactInfo.email
'FirstName' = $contactInfo.firstName
'LastName' = $contactInfo.LastName
'Title' = $contactInfo.jobTitle
'Title' = $contactInfo.Title
'StreetAddress' = $contactInfo.StreetAddress
'PostalCode' = $contactInfo.PostalCode
'City' = $contactInfo.City
'CountryOrRegion' = $contactInfo.Country
'Company' = $contactInfo.companyName
'mobilePhone' = $contactInfo.MobilePhone
'phone' = $contactInfo.BusinessPhone
'identity' = $contactInfo.ContactID
'CountryOrRegion' = $contactInfo.CountryOrRegion
'Company' = $contactInfo.Company
'mobilePhone' = $contactInfo.mobilePhone
'phone' = $contactInfo.phone
}

# Call the Set-Contact cmdlet to update the contact
$null = New-ExoRequest -tenantid $TenantID -cmdlet 'Set-Contact' -cmdParams $bodyForSetContact -UseSystemMailbox $true
$null = New-ExoRequest -tenantid $TenantID -cmdlet 'Set-MailContact' -cmdParams @{Identity = $contactInfo.ContactID; HiddenFromAddressListsEnabled = [System.Convert]::ToBoolean($contactInfo.hidefromGAL) } -UseSystemMailbox $true
$Results = "Successfully edited contact $($contactInfo.DisplayName)"
Write-LogMessage -user $ExecutingUser -API $APINAME -tenant $TenantID -message $Results -Sev Info
$StatusCode = [HttpStatusCode]::OK
Expand All @@ -57,6 +58,6 @@ Function Invoke-EditContact {
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $StatusCode
Body = $responseResults
Body = $Results
})
}
69 changes: 48 additions & 21 deletions Modules/CIPPCore/Public/Entrypoints/Invoke-ListContacts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,62 @@ Function Invoke-ListContacts {
[CmdletBinding()]
param($Request, $TriggerMetadata)

$APIName = $TriggerMetadata.FunctionName
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'

$selectlist = 'id', 'companyName', 'displayName', 'mail', 'onPremisesSyncEnabled', 'editURL', "givenName", "jobTitle", "surname", "addresses", "phones"
# Define fields to retrieve
$selectList = @(
'id',
'companyName',
'displayName',
'mail',
'onPremisesSyncEnabled',
'editURL',
'givenName',
'jobTitle',
'surname',
'addresses',
'phones'
)

# Write to the Azure Functions log stream.
Write-Host 'PowerShell HTTP trigger function processed a request.'
# Get query parameters
$TenantFilter = $Request.Query.tenantFilter
$ContactID = $Request.Query.id

# Validate required parameters
if (-not $TenantFilter) {
$StatusCode = [HttpStatusCode]::BadRequest
$GraphRequest = 'tenantFilter is required'
Write-Host 'Error: Missing tenantFilter parameter'
} else {
try {
# Construct Graph API URI based on whether an ID is provided
$graphUri = if ([string]::IsNullOrWhiteSpace($ContactID) -eq $false) {
"https://graph.microsoft.com/beta/contacts/$($ContactID)?`$select=$($selectList -join ',')"
} else {
"https://graph.microsoft.com/beta/contacts?`$top=999&`$select=$($selectList -join ',')"
}

# Interact with query parameters or the body of the request.
$TenantFilter = $Request.Query.TenantFilter
$ContactID = $Request.Query.ContactID
# Make the Graph API request
$GraphRequest = New-GraphGetRequest -uri $graphUri -tenantid $TenantFilter

Write-Host "Tenant Filter: $TenantFilter"
Write-Host "This is the Contact ID: $ContactID"
try {
$GraphRequest = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/contacts/$($ContactID)?`$top=999&`$select=$($selectlist -join ',')" -tenantid $TenantFilter | Select-Object $selectlist | ForEach-Object {
$_.editURL = "https://outlook.office365.com/ecp/@$TenantFilter/UsersGroups/EditContact.aspx?exsvurl=1&realm=$($env:TenantID)&mkt=en-US&id=$($_.id)"
$_
if ([string]::IsNullOrWhiteSpace($ContactID) -eq $false) {
$HiddenFromGAL = New-EXORequest -tenantid $TenantFilter -cmdlet 'Get-Recipient' -cmdParams @{RecipientTypeDetails = 'MailContact' } -Select 'HiddenFromAddressListsEnabled,ExternalDirectoryObjectId' | Where-Object { $_.ExternalDirectoryObjectId -eq $ContactID }
$GraphRequest | Add-Member -NotePropertyName 'hidefromGAL' -NotePropertyValue $HiddenFromGAL.HiddenFromAddressListsEnabled
}
# Ensure single result when ID is provided
if ($ContactID -and $GraphRequest -is [array]) {
$GraphRequest = $GraphRequest | Select-Object -First 1
}
$StatusCode = [HttpStatusCode]::OK
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
$StatusCode = [HttpStatusCode]::InternalServerError
$GraphRequest = $ErrorMessage
}
$StatusCode = [HttpStatusCode]::OK
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
$StatusCode = [HttpStatusCode]::Forbidden
$GraphRequest = $ErrorMessage
}

# Return response
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $StatusCode
Body = @($GraphRequest | Where-Object -Property id -NE $null)
Body = @($GraphRequest | Where-Object { $null -ne $_.id })
})

}

0 comments on commit 60f3c3f

Please sign in to comment.