Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions src/Commands/Copilot/GetCopilotAgent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Management.Automation;
using System.Text.Json;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Model.Copilot;
using System.Linq;
using System.Management.Automation;
using System.Text.Json;

namespace PnP.PowerShell.Commands.Copilot
{
Expand Down Expand Up @@ -37,38 +37,50 @@ protected override void ExecuteCmdlet()
else
{
// find all doclibraries
var doclibs = ClientContext.LoadQuery(CurrentWeb.Lists.Where(l => l.BaseTemplate == 101));
ClientContext.ExecuteQuery();
var camlQuery = CamlQuery.CreateAllItemsQuery();
var doclibs = ClientContext.LoadQuery(CurrentWeb.Lists.Where(l => l.BaseTemplate == (int)ListTemplateType.DocumentLibrary));
ClientContext.ExecuteQueryRetry();

camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"File_x0020_Type\" /><Value Type=\"Text\">agent</Value></Eq></Where></Query></View>";
foreach (var doclib in doclibs)
{
camlQuery.ListItemCollectionPosition = null;
do
{
camlQuery.ListItemCollectionPosition = GetAgents(doclib, camlQuery);

} while (camlQuery.ListItemCollectionPosition != null);
GetAgents(doclib);
}
}
}

private ListItemCollectionPosition GetAgents(List list, CamlQuery camlQuery)
private void GetAgents(List list)
{
var items = list.GetItems(camlQuery);
list.Context.Load(items, i => i.IncludeWithDefaultProperties(li => li.FieldValuesAsText), i => i.ListItemCollectionPosition);
ClientContext.ExecuteQueryRetry();
foreach (var item in items)
var camlQuery = CamlQuery.CreateAllItemsQuery(100);
camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"File_x0020_Type\" /><Value Type=\"Text\">agent</Value></Eq></Where></Query></View>";

// Initialize position to null for first page
ListItemCollectionPosition position = null;

// Continue fetching until no more pages
do
{
var agentContents = CurrentWeb.GetFileAsString(item.FieldValuesAsText["FileRef"]);
var agentObject = JsonSerializer.Deserialize<CopilotAgent>(agentContents);
agentObject.ServerRelativeUrl = item.FieldValuesAsText["FileRef"];

WriteObject(agentObject);
}
// Set the position for the current request
camlQuery.ListItemCollectionPosition = position;

// Get current batch of items
var items = list.GetItems(camlQuery);
list.Context.Load(items, i => i.IncludeWithDefaultProperties(li => li.FieldValuesAsText), i => i.ListItemCollectionPosition);
ClientContext.ExecuteQueryRetry();

// Process current batch
foreach (var item in items)
{
var agentContents = CurrentWeb.GetFileAsString(item.FieldValuesAsText["FileRef"]);
var agentObject = JsonSerializer.Deserialize<CopilotAgent>(agentContents);
agentObject.ServerRelativeUrl = item.FieldValuesAsText["FileRef"];

WriteObject(agentObject);
}

// Get position for next batch
position = items.ListItemCollectionPosition;

} while (position != null);

return items.ListItemCollectionPosition;
}
}
}
Loading