Skip to content

Commit 0918650

Browse files
authored
Merge pull request #5223 from kinga-altF4/searchcrawllog
Increase time out for Get-PnPSearchCrawlLog
2 parents de3cb64 + ee71afb commit 0918650

4 files changed

Lines changed: 92 additions & 8 deletions

File tree

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "PnP.PowerShell (pwsh + import)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"program": "C:/Program Files/PowerShell/7/pwsh.exe",
12+
"args": [
13+
"-NoLogo",
14+
"-NoProfile",
15+
"-ExecutionPolicy", "Bypass",
16+
"-Command",
17+
"Import-Module \"$PWD/build/debug/PnP.PowerShell/PnP.PowerShell.psd1\" -Force; 'Ready: Import complete'; Read-Host 'Press Enter to exit'"
18+
],
19+
"cwd": "${workspaceFolder}",
20+
"console": "externalTerminal",
21+
"justMyCode": true
22+
}
23+
,
724
{
825
"name": "Debug with nugets in new PowerShell session",
926
"type": "coreclr",

documentation/Get-PnPSearchCrawlLog.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Returns entries from the SharePoint search crawl log. Make sure you are granted
1616

1717
```powershell
1818
Get-PnPSearchCrawlLog [-LogLevel <LogLevel>] [-RowLimit <Int32>] [-Filter <String>]
19-
[-ContentSource <ContentSource>] [-StartDate <DateTime>] [-EndDate <DateTime>] [-RawFormat]
19+
[-ContentSource <ContentSource>] [-StartDate <DateTime>] [-EndDate <DateTime>] [-RawFormat] [-IncreaseRequestTimeout]
2020
[-Connection <PnPConnection>]
2121
```
2222

@@ -75,6 +75,16 @@ Get-PnPSearchCrawlLog -RowLimit 3 -RawFormat
7575

7676
Returns the last 3 crawl log entries showing the raw crawl log data.
7777

78+
### EXAMPLE 8
79+
```powershell
80+
$ClientID= "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
81+
$env:SharePointPnPHttpTimeout = -1 #👈
82+
Connect-PnPOnline -Url https://<tenant>-admin.sharepoint.com/ -Interactive -ClientId $ClientID -ErrorAction Stop # 👈
83+
84+
Get-PnPSearchCrawlLog -Filter "https://contoso-my.sharepoint.com/sites/Intranet" -IncreaseRequestTimeout
85+
```
86+
Increases the request timeout allowing the call to last up to 3 minutes. The `ClientRuntimeContext` enforces a three-minute limit; when increasing the timeout to its maximum of three minutes, this threshold may still be exceeded.
87+
7888
## PARAMETERS
7989

8090
### -Connection
@@ -134,6 +144,20 @@ Accept pipeline input: False
134144
Accept wildcard characters: False
135145
```
136146
147+
### -IncreaseRequestTimeout
148+
Extend the request timeout to permit command execution for up to 3 minutes.
149+
150+
```yaml
151+
Type: Switch
152+
Parameter Sets: (All)
153+
154+
Required: False
155+
Position: Named
156+
Default value: None
157+
Accept pipeline input: False
158+
Accept wildcard characters: False
159+
```
160+
137161
### -LogLevel
138162
Filter what log entries to return (All, Success, Warning, Error). Defaults to All
139163

src/Commands/Search/GetSearchCrawlLog.cs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using System.Management.Automation;
55
using Microsoft.SharePoint.Client;
6-
using Microsoft.SharePoint.Client.Search.Administration;
6+
using Microsoft.SharePoint.Client.Search.Administration;
77
using PnP.PowerShell.Commands.Attributes;
88

99
namespace PnP.PowerShell.Commands.Search
@@ -13,7 +13,9 @@ public enum LogLevel
1313
All = -1,
1414
Success = 0,
1515
Warning = 1,
16-
Error = 2
16+
Error = 2,
17+
Deleted = 3,
18+
TopLEvel =4
1719
}
1820

1921
public enum ContentSource
@@ -26,14 +28,17 @@ public class CrawlEntry
2628
{
2729
public string Url { get; set; }
2830
public DateTime CrawlTime { get; set; }
31+
public DateTime LastTouchedTime { get; set; }
2932
public DateTime ItemTime { get; set; }
3033
public LogLevel LogLevel { get; set; }
3134
public string Status { get; set; }
3235
public int ItemId { get; set; }
3336
public int ContentSourceId { get; set; }
37+
38+
public string DatabaseName { get; set; }
3439
}
3540

36-
[Cmdlet(VerbsCommon.Get, "PnPSearchCrawlLog", DefaultParameterSetName = "Xml")]
41+
[Cmdlet(VerbsCommon.Get, "PnPSearchCrawlLog")]
3742
[ApiNotAvailableUnderApplicationPermissions]
3843
public class GetSearchCrawlLog : PnPWebCmdlet
3944
{
@@ -58,12 +63,34 @@ public class GetSearchCrawlLog : PnPWebCmdlet
5863
[Parameter(Mandatory = false)]
5964
public SwitchParameter RawFormat;
6065

66+
[Parameter(Mandatory = false)]
67+
public SwitchParameter GetCountOnly;
68+
69+
[Parameter(Mandatory = false)]
70+
public SwitchParameter IncreaseRequestTimeout;
71+
72+
6173
private const int MaxRows = 100000;
6274

6375
protected override void ExecuteCmdlet()
6476
{
6577
try
6678
{
79+
if(IncreaseRequestTimeout)
80+
{
81+
string timeoutValue = Environment.GetEnvironmentVariable("SharePointPnPHttpTimeout");
82+
if (string.IsNullOrEmpty(timeoutValue))
83+
{
84+
LogWarning("The timeout may be only increased if the SharePointPnPHttpTimeout environment variable is set to 180000 or -1.");
85+
LogWarning("Use $env:SharePointPnPHttpTimeout = -1 command and then, establish new connection with Connect-PnPOnline.");
86+
return;
87+
}
88+
else
89+
{
90+
//Max 3 minutes, because Default CSOM timeout is 180,000 ms
91+
ClientContext.RequestTimeout=3*60*1000;
92+
}
93+
}
6794
var crawlLog = new DocumentCrawlLog(ClientContext, ClientContext.Site);
6895
ClientContext.Load(crawlLog);
6996

@@ -94,7 +121,9 @@ protected override void ExecuteCmdlet()
94121
RowLimit = MaxRows;
95122
}
96123

97-
var logEntries = crawlLog.GetCrawledUrls(false, RowLimit, Filter, true, contentSourceId, (int)LogLevel, -1, StartDate, EndDate);
124+
bool countOnly= GetCountOnly ? true: false;
125+
126+
var logEntries = crawlLog.GetCrawledUrls(countOnly, RowLimit, Filter, true, contentSourceId, (int)LogLevel, -1, StartDate, EndDate);
98127
ClientContext.ExecuteQueryRetry();
99128

100129
if (RawFormat)
@@ -139,7 +168,16 @@ protected override void ExecuteCmdlet()
139168
}
140169
catch (Exception e)
141170
{
142-
LogError($"Error: {e.Message}. Make sure you are granted access to the crawl log via the SharePoint search admin center at https://<tenant>-admin.sharepoint.com/_layouts/15/searchadmin/crawllogreadpermission.aspx");
171+
if(e.Message=="The operation has timed out." )
172+
{
173+
174+
LogError($"Error: {e.Message}. Default CSOM timeout is 180,000 ms (≈3 minutes). If you are querying large crawl logs or broad ranges, the server may take longer than that. ");
175+
176+
}
177+
else
178+
{
179+
LogError($"Error: {e.Message}. Make sure you are granted access to the crawl log via the SharePoint search admin center at https://<tenant>-admin.sharepoint.com/_layouts/15/searchadmin/crawllogreadpermission.aspx");
180+
}
143181
}
144182
}
145183

@@ -175,7 +213,9 @@ private static CrawlEntry MapCrawlLogEntry(Dictionary<string, object> dictionary
175213
ItemId = (int)dictionary["URLID"],
176214
ContentSourceId = (int)dictionary["ContentSourceID"],
177215
Url = dictionary["FullUrl"].ToString(),
178-
CrawlTime = (DateTime)dictionary["TimeStampUtc"]
216+
CrawlTime = (DateTime)dictionary["TimeStampUtc"],
217+
LastTouchedTime= (DateTime)dictionary["LastTouchedTime"],
218+
DatabaseName= (string)dictionary["DatabaseName"]
179219
};
180220
long.TryParse(dictionary["LastRepositoryModifiedTime"] + "", out long ticks);
181221
if (ticks != 0)

src/Tests/Search/GetPnPSearchCrawlLogTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,17 @@ public void GetPnPSearchCrawlLogTest()
7777
// From Cmdlet Help: Show raw crawl log data
7878
var rawFormat = "";
7979

80+
var increaseRequestTimeout= "";
81+
8082
var results = scope.ExecuteCommand("Get-PnPSearchCrawlLog",
8183
new CommandParameter("LogLevel", logLevel),
8284
new CommandParameter("RowLimit", rowLimit),
8385
new CommandParameter("Filter", filter),
8486
new CommandParameter("ContentSource", contentSource),
8587
new CommandParameter("StartDate", startDate),
8688
new CommandParameter("EndDate", endDate),
87-
new CommandParameter("RawFormat", rawFormat));
89+
new CommandParameter("RawFormat", rawFormat),
90+
new CommandParameter("IncreaseRequestTimeout", increaseRequestTimeout));
8891

8992
Assert.IsNotNull(results);
9093
}

0 commit comments

Comments
 (0)