-
Couldn't load subscription status.
- Fork 390
Add -KnowledgeAgentSelectedSitesList parameter to Set-PnPTenant cmdlet #5091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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
KnowledgeAgentSelectedSitesListparameter 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); |
Copilot AI Sep 22, 2025
There was a problem hiding this comment.
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.
| 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."); | ||
| } | ||
| } | ||
| |
Copilot AI Sep 22, 2025
There was a problem hiding this comment.
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.
| 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."); | |
| } | |
| } |
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
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