Saturday, March 8, 2025

Powershell - Create a Site Template

 # ref https://pnp.github.io/powershell/cmdlets/index.html

# Variables for site URL and site name

$tenantId = "u7ub14b9-dc5c-44fe-a920-7e6bb7e310a6"
$tenantUrl = "https://[tenant].sharepoint.com"
$siteUrl = "https://[tenant].sharepoint.com/sites/DesignSite"
# set script id to output of Add-PnPSiteScript
$scriptId = "06763a50-0f1b-4a01-b7d6-a7c2fd54703a"

Connect-PnPOnline -Url $siteUrl -Tenant $tenantId -Interactive -ClientId c544a357-50a0-4ce9-99f8-79fc70fc8478

# check if working
Get-PnPTenantSite
Get-PnPConnection
# set site for future work, should be set from connect-pnponline

# get site script for template
$siteJson = Get-PnPSiteScriptFromWeb -IncludeAll

Add-PnPSiteScript -Title "Org Intranet" -Content $siteJson -Description "Org Intranet Site Template"

#set sciptId to output of Add-PnPSiteScript
$scriptId = "06763a50-0f1b-4a01-b7d6-a7c2fd54703a"  # output of Add-PnPSiteScript

Add-PnPSiteDesign -Title "Org Template 2" -SiteScriptIds $scriptId -Description "Org Template 2" -WebTemplate "68"

# check if it worked, look for the ID returned from Add-PnPSiteDesign or the Title used

Get-PnPSiteDesign

# switch site once logged in with
Connect-PnPOnline -Url "https://1495304ab.sharepoint.com/sites/TemplateSite03" -UseWebLogin

# set logo
$logoUrl = "https://[tenant].sharepoint.com/sites/DesignSite/SiteAssets/tgicon.png"
Set-PnPWeb -SiteLogoUrl $logoUrl

PowerShell - Copy All File in Library to Another Site

 # Variables

$sourceSiteUrl = "https://[yourtenant].sharepoint.com/sites/SrcSite"
$destinationSiteUrl = "https://[yourtenant].sharepoint.com/sites/DestSite"
$libraryName = "TempDocs"
$tenantId = "t6yb14b9-dc5c-44fe-a920-7e6bb7e310a6"
$clientId = "u7i4a357-50a0-4ce9-99f8-79fc70fc8478"

# Connect to source site
Connect-PnPOnline -Url $sourceSiteUrl -Tenant $tenantId -Interactive -ClientId $clientId

# Get all files in the library
$files = Get-PnPListItem -List $libraryName | Where-Object { $_.FieldValues.FileRef -ne $null }

# Connect to Destination Site
Connect-PnPOnline -Url $destinationSiteUrl -Tenant $tenantId -Interactive -ClientId $clientId
# Ensure destination folder exists
$destinationLibraryPath = "$destinationSiteUrl/$libraryName"
if (-not (Test-PnPListItem -List $libraryName -Identity "$libraryName")) {
    Write-Host "Creating destination folder: $destinationLibraryPath"
    New-PnPListItem -List $libraryName -Values @{"Title" = $libraryName }
}

# Copy each file
foreach ($file in $files) {
    $fileUrl = $file.FieldValues.FileRef
    $targetUrl = "$destinationLibraryPath"
    Write-Host "Copying $fileUrl to $targetUrl"
   
    Copy-PnPFile -SourceUrl $fileUrl -TargetUrl $destinationLibraryPath -OverwriteIfAlreadyExists -Force
}

# Disconnect
Disconnect-PnPOnline

Wednesday, March 5, 2025

Create a Copilot Agent with SSO Authentication

 Steps to Create a Copilot Agent with SSO Authentication

1. Create a New Copilot Agent
  • Sign in to Microsoft Copilot Studio: Go to the Copilot Studio portal (https://copilotstudio.microsoft.com/) and sign in with your Microsoft account.
  • Create a New Agent:
    • From the home page, select Create in the left navigation.
    • Choose New agent.
    • Provide a name, description, and instructions for your agent (e.g., "Helpdesk Agent" to assist with IT queries). Use conversational language to define its purpose and tone.
    • Select Create to initialize the agent.
2. Set Up Authentication in Copilot Studio
  • Navigate to Settings: Open your newly created agent, then go to Settings > Security > Authentication.
  • Choose Authentication Option:
    • By default, "No authentication" is selected. For SSO, select Authenticate manually (this allows custom configuration for SSO).
    • Turn on Require users to sign in to ensure authentication is enforced.
  • Configure Identity Provider:
    • For Microsoft Entra ID SSO:
      • Service provider: Select "Azure Active Directory v2".
      • You’ll need to provide a Client ID, Client Secret, and Scopes (e.g., profile openid). These will come from an app registration in Azure (see Step 3).
    • For other OAuth2 providers (e.g., Google, Okta), select "Generic OAuth 2.0 provider" and input the provider-specific details (consult your provider’s documentation).
3. Register an App in Microsoft Entra ID
  • Go to Azure Portal: Sign in to the Azure portal (https://portal.azure.com/) with an admin account in the same tenant as your Copilot Studio.
  • Create an App Registration:
    • Navigate to Microsoft Entra ID > App registrations > New registration.
    • Enter a name (e.g., "CopilotSSOAgent").
    • Set Supported account types to "Accounts in this organizational directory only" (or multi-tenant if needed).
    • Leave Redirect URI blank for now; you’ll add it later.
    • Click Register.
  • Configure the App:
    • Go to Authentication > Add a platform > Web.
    • Add a Redirect URI: Use https://unitedstates.token.botframework.com/.auth/web/redirect (or the region-specific URL shown in Copilot Studio’s authentication settings).
    • Enable Access tokens and ID tokens under "Implicit grant and hybrid flows".
    • Save changes.
  • Generate a Client Secret:
    • Go to Certificates & secrets > New client secret.
    • Add a description and set an expiry period (e.g., 6 months).
    • Copy the Value immediately (it won’t be shown again).
  • Copy the Client ID:
    • From the app’s Overview, copy the Application (client) ID.
  • Expose an API (for SSO):
    • Go to Expose an API > Add a scope.
    • Set the scope name (e.g., user_impersonation) and save the full scope URL (e.g., api://<client-id>/user_impersonation).
4. Complete Authentication Configuration in Copilot Studio
  • Return to Copilot Studio’s Authentication settings.
  • Fill in the fields:
    • Client ID: Paste the Application (client) ID from Azure.
    • Client Secret: Paste the secret value you copied.
    • Scopes: Enter profile openid (add more if needed, like email).
    • Token exchange URL: Paste the scope URL from the "Expose an API" step (required for SSO).
  • Click Save.
5. Publish the Agent
  • Test the Agent: In Copilot Studio, use the Test your agent pane to verify it prompts for sign-in and works as expected.
  • Publish: Go to the top of the page, select Publish, and confirm. Authentication changes take effect only after publishing.
6. Deploy to a Channel (e.g., Teams or Custom Website)
  • For Microsoft Teams:
    • Go to Channels > Microsoft Teams > Turn on Teams.
    • If using SSO, ensure the Teams app manifest includes the Client ID and token exchange URL (see Microsoft documentation for Teams SSO setup).
    • Republish after configuration.
  • For a Custom Website:
    • Go to Channels > Custom website > Copy the iframe embed code.
    • Implement a custom canvas to handle SSO token exchange (see below).
7. (Optional) Implement SSO on a Custom Canvas
  • If deploying to a website, use a custom HTML page to intercept the sign-in prompt and exchange tokens:
    • Use sample code from the Copilot Studio GitHub repo (e.g., https://github.com/microsoft/copilotstudio-samples).
    • Modify it to acquire an Entra ID token for the signed-in user and post it to the secure URL provided by Copilot Studio.
    • This avoids manual login prompts, enabling seamless SSO.