Skip to content

Conversation

@gautamdsheth
Copy link
Collaborator

Before creating a pull request, make sure that you have read the contribution file located at

https://github.com/pnp/powerShell/blob/dev/CONTRIBUTING.md

Type

  • Bug Fix
  • New Feature
  • Sample

Related Issues?

Fixes #X, partially fixes #Y, mentioned in #Z, etc.

What is in this Pull Request ?

Please describe the changes in the PR.

Guidance

  • You can delete this section when you are submitting the pull request.*
  • Please update this PR information accordingly. We use this as part of our release notes in monthly communications.
  • Please target your PR to Dev branch. If you do not target the Dev branch we will not accept this PR.
Copilot AI review requested due to automatic review settings September 22, 2025 11:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for the -KnowledgeAgentSelectedSitesList parameter to the Set-PnPTenant cmdlet, enabling administrators to configure which site collections should be targeted by the tenant Knowledge Agent. The implementation converts site URLs to site IDs and handles error scenarios gracefully.

Key Changes

  • Added new KnowledgeAgentSelectedSitesList parameter that accepts an array of site URLs
  • Implemented URL-to-site-ID resolution logic with error handling and warning messages
  • Added comprehensive documentation for the new parameter with usage examples

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Commands/Admin/SetTenant.cs Adds the new parameter property and implements the logic to resolve site URLs to IDs with error handling
documentation/Set-PnPTenant.md Documents the new parameter with detailed usage instructions and examples

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

{
// Build a GUID array by resolving each provided site URL to its SiteProperties and extracting the Id
var siteIdList = new List<Guid>();
var tenantForLookup = new Tenant(AdminContext);
Copy link

Copilot AI Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new Tenant instance for each parameter processing is inefficient. Consider reusing the existing Tenant object that's already loaded in the ExecuteCmdlet method at line 539.

Copilot uses AI. Check for mistakes.
Comment on lines +1696 to +1724
foreach (var siteUrl in KnowledgeAgentSelectedSitesList)
{
if (string.IsNullOrWhiteSpace(siteUrl))
{
continue;
}

try
{
// The GetSitePropertiesByUrl call requires the tenant admin context
var siteProps = tenantForLookup.GetSitePropertiesByUrl(siteUrl, includeDetail: false);
tenantForLookup.Context.Load(siteProps, sp => sp.SiteId);
tenantForLookup.Context.ExecuteQueryRetry();
if (siteProps != null && siteProps.SiteId != Guid.Empty)
{
siteIdList.Add(siteProps.SiteId);
}
else
{
LogWarning($"Unable to resolve site URL '{siteUrl}' to a site id. It will be skipped.");
}
}
catch (Exception ex)
{
// Don't fail the whole cmdlet for one bad URL; log warning and continue
LogWarning($"Error resolving site URL '{siteUrl}': {ex.Message}. It will be skipped.");
}
}

Copy link

Copilot AI Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a separate API call for each site URL. Consider batching these operations or collecting all site properties before executing queries to reduce the number of round trips to SharePoint.

Suggested change
foreach (var siteUrl in KnowledgeAgentSelectedSitesList)
{
if (string.IsNullOrWhiteSpace(siteUrl))
{
continue;
}
try
{
// The GetSitePropertiesByUrl call requires the tenant admin context
var siteProps = tenantForLookup.GetSitePropertiesByUrl(siteUrl, includeDetail: false);
tenantForLookup.Context.Load(siteProps, sp => sp.SiteId);
tenantForLookup.Context.ExecuteQueryRetry();
if (siteProps != null && siteProps.SiteId != Guid.Empty)
{
siteIdList.Add(siteProps.SiteId);
}
else
{
LogWarning($"Unable to resolve site URL '{siteUrl}' to a site id. It will be skipped.");
}
}
catch (Exception ex)
{
// Don't fail the whole cmdlet for one bad URL; log warning and continue
LogWarning($"Error resolving site URL '{siteUrl}': {ex.Message}. It will be skipped.");
}
}
var sitePropsList = new List<SiteProperties>();
var siteUrlList = new List<string>();
for (int i = 0; i < KnowledgeAgentSelectedSitesList.Length; i++)
{
var siteUrl = KnowledgeAgentSelectedSitesList[i];
if (string.IsNullOrWhiteSpace(siteUrl))
{
continue;
}
try
{
var siteProps = tenantForLookup.GetSitePropertiesByUrl(siteUrl, includeDetail: false);
tenantForLookup.Context.Load(siteProps, sp => sp.SiteId);
sitePropsList.Add(siteProps);
siteUrlList.Add(siteUrl);
}
catch (Exception ex)
{
LogWarning($"Error resolving site URL '{siteUrl}': {ex.Message}. It will be skipped.");
}
}
try
{
tenantForLookup.Context.ExecuteQueryRetry();
}
catch (Exception ex)
{
LogWarning($"Error executing batch site property lookup: {ex.Message}");
}
for (int i = 0; i < sitePropsList.Count; i++)
{
var siteProps = sitePropsList[i];
var siteUrl = siteUrlList[i];
if (siteProps != null && siteProps.SiteId != Guid.Empty)
{
siteIdList.Add(siteProps.SiteId);
}
else
{
LogWarning($"Unable to resolve site URL '{siteUrl}' to a site id. It will be skipped.");
}
}
Copilot uses AI. Check for mistakes.
@gautamdsheth gautamdsheth merged commit f0d03d0 into dev Sep 22, 2025
4 checks passed
@gautamdsheth gautamdsheth deleted the feat/ka-sites-list branch October 3, 2025 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant