Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit 9e186e5

Browse files
committed
Added support for importing and exporting search settings configuration from the tenant level.
1 parent 944fdac commit 9e186e5

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

Commands/Enums/SearchConfigurationScope.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public enum SearchConfigurationScope
44
{
55
Web,
6-
Site
6+
Site,
7+
Subscription
78
}
89
}

Commands/Search/GetSearchConfiguration.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using System.Management.Automation;
1+
using System;
2+
using System.Management.Automation;
23
using Microsoft.SharePoint.Client;
4+
using Microsoft.SharePoint.Client.Search.Administration;
5+
using Microsoft.SharePoint.Client.Search.Portability;
36
using OfficeDevPnP.PowerShell.CmdletHelpAttributes;
47
using OfficeDevPnP.PowerShell.Commands.Enums;
8+
using Resources = OfficeDevPnP.PowerShell.Commands.Properties.Resources;
59

610
namespace OfficeDevPnP.PowerShell.Commands.Search
711
{
@@ -15,6 +19,10 @@ namespace OfficeDevPnP.PowerShell.Commands.Search
1519
Code = @"PS:> Get-SPOSearchConfiguration -Scope Site",
1620
Remarks = "Returns the search configuration for the current site collection",
1721
SortOrder = 2)]
22+
[CmdletExample(
23+
Code = @"PS:> Get-SPOSearchConfiguration -Scope Subscription",
24+
Remarks = "Returns the search configuration for the current tenant",
25+
SortOrder = 3)]
1826
public class GetSearchConfiguration : SPOWebCmdlet
1927
{
2028
[Parameter(Mandatory = false)]
@@ -34,6 +42,20 @@ protected override void ExecuteCmdlet()
3442
WriteObject(ClientContext.Site.GetSearchConfiguration());
3543
break;
3644
}
45+
case SearchConfigurationScope.Subscription:
46+
{
47+
if (!ClientContext.Url.ToLower().Contains("-admin"))
48+
{
49+
throw new InvalidOperationException(Resources.CurrentSiteIsNoTenantAdminSite);
50+
}
51+
52+
SearchObjectOwner owningScope = new SearchObjectOwner(ClientContext, SearchObjectLevel.SPSiteSubscription);
53+
var config = new SearchConfigurationPortability(ClientContext);
54+
ClientResult<string> configuration = config.ExportSearchConfiguration(owningScope);
55+
ClientContext.ExecuteQueryRetry(10, 60*5*1000);
56+
WriteObject(configuration.Value);
57+
break;
58+
}
3759
}
3860
}
3961
}

Commands/Search/SetSearchConfiguration.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System.Management.Automation;
1+
using System;
2+
using System.Management.Automation;
23
using Microsoft.SharePoint.Client;
4+
using Microsoft.SharePoint.Client.Search.Administration;
35
using OfficeDevPnP.PowerShell.CmdletHelpAttributes;
46
using OfficeDevPnP.PowerShell.Commands.Enums;
7+
using Resources = OfficeDevPnP.PowerShell.Commands.Properties.Resources;
58

69
namespace OfficeDevPnP.PowerShell.Commands.Search
710
{
@@ -15,6 +18,10 @@ namespace OfficeDevPnP.PowerShell.Commands.Search
1518
Code = @"PS:> Set-SPOSearchConfiguration -Configuration $config -Scope Site",
1619
Remarks = "Sets the search configuration for the current site collection",
1720
SortOrder = 2)]
21+
[CmdletExample(
22+
Code = @"PS:> Set-SPOSearchConfiguration -Configuration $config -Scope Subscription",
23+
Remarks = "Sets the search configuration for the current tenant",
24+
SortOrder = 3)]
1825
public class SetSearchConfiguration : SPOWebCmdlet
1926
{
2027
[Parameter(Mandatory = true)]
@@ -37,6 +44,16 @@ protected override void ExecuteCmdlet()
3744
ClientContext.Site.SetSearchConfiguration(Configuration);
3845
break;
3946
}
47+
case SearchConfigurationScope.Subscription:
48+
{
49+
if (!ClientContext.Url.ToLower().Contains("-admin"))
50+
{
51+
throw new InvalidOperationException(Resources.CurrentSiteIsNoTenantAdminSite);
52+
}
53+
54+
ClientContext.ImportSearchSettings(Configuration, SearchObjectLevel.SPSiteSubscription);
55+
break;
56+
}
4057
}
4158
}
4259
}

0 commit comments

Comments
 (0)