Jun 29 2023 10:37 AM
Greetings!
I was wondering if there is a way to get the names of people from AAD every week, for example, and add those names to the "Name" Column automatically. If the name is already there, don't do it.
Is there any way to accomplish this?
Thanks!
Jun 30 2023 12:59 AM
HI @danbmunoz,
Yes, it is possible to retrieve the names of people from Azure Active Directory (AAD) and automatically add them to a SharePoint Online (SPO) list, considering certain conditions. Here's an approach to achieve this:
1. Use Microsoft Graph API: Utilize the Microsoft Graph API to fetch the user data from AAD. Specifically, you can use the Users endpoint (`/users`) to retrieve user information.
2. Get the user display names: In the API response, extract the display names of the users you want to add to the SharePoint list.
3. Connect to SharePoint List: Establish a connection to your SharePoint Online list using SharePoint REST API or SharePoint Online PowerShell cmdlets.
4. Query the existing names: Retrieve the existing names already present in the "Name" column of the SharePoint list. You can use a REST API query or PowerShell cmdlets to fetch the data.
5. Compare and Add new names: Compare the fetched user display names with the existing names in the SharePoint list. If a name doesn't exist in the list, add it to the "Name" column using the REST API or PowerShell cmdlets.
6. Schedule the process: Set up a scheduled job or script that runs weekly to retrieve the user names from AAD and update the SharePoint list accordingly.
You can choose the approach that best suits your requirements, either using custom code with REST API or PowerShell, or utilizing third-party tools and connectors for integration between AAD and SharePoint.
Certainly! Here's an example PowerShell script that retrieves user display names from Azure Active Directory (AAD) and adds them to a SharePoint Online (SPO) list:
# SharePoint Online Credentials
$SiteURL = "https://your-site-url"
$ListName = "YourListName"
# Connect to SharePoint Online
$Credential = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credential.UserName, $Credential.Password)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Context.Credentials = $Credentials
# Retrieve existing names from SharePoint List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Items = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Context.Load($Items)
$Context.ExecuteQuery()
# Get display names from Azure Active Directory
$AADUsers = Get-AzureADUser | Select-Object -ExpandProperty DisplayName
# Add new names to SharePoint List
foreach ($User in $AADUsers) {
# Check if the name already exists in the SharePoint list
$NameExists = $Items | Where-Object { $_["Name"] -eq $User }
if (!$NameExists) {
# Create a new list item and set the Name column value
$NewItem = $List.AddItem()
$NewItem["Name"] = $User
$NewItem.Update()
$Context.ExecuteQuery()
}
}
Write-Host "Names added to SharePoint List successfully."
# Dispose SharePoint objects
$Context.Dispose()
Make sure to replace the following placeholders with your actual values:
Before executing the script, ensure that you have the SharePoint Online PowerShell module installed (Install-Module -Name Microsoft.Online.SharePoint.PowerShell) and the AzureAD module (Install-Module -Name AzureAD) installed on your machine.
Kindest regards
Jun 30 2023 08:09 AM