diff --git a/documentation/Set-PnPHomeSite.md b/documentation/Set-PnPHomeSite.md index 004a01309..b00ef17e8 100644 --- a/documentation/Set-PnPHomeSite.md +++ b/documentation/Set-PnPHomeSite.md @@ -20,7 +20,7 @@ Sets the home site for your tenant. The home site needs to be a communication si ## SYNTAX ```powershell -Set-PnPHomeSite -HomeSiteUrl [VivaConnectionsDefaultStart ] [-Connection ] +Set-PnPHomeSite -HomeSiteUrl [VivaConnectionsDefaultStart ] [-Force ] [-DraftMode ] [-Connection ] ``` ## DESCRIPTION @@ -43,6 +43,13 @@ Set-PnPHomeSite -HomeSiteUrl "https://yourtenant.sharepoint.com/sites/myhome" -V Sets the home site to the provided site collection url and keeps the Viva Connections landing experience to the SharePoint home site. +### EXAMPLE 3 +```powershell +Set-PnPHomeSite -HomeSiteUrl "https://yourtenant.sharepoint.com/sites/myhome" -VivaConnectionsDefaultStart:$true -DraftMode:$true +``` + +Sets the home site to the provided site collection url and keeps the Viva Connections landing experience to the SharePoint home site but it will be in draft mode. + ## PARAMETERS ### -Connection @@ -86,6 +93,32 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -DraftMode +When set to $true, the DraftMode parameter will keep the Viva Connections landing experience to the SharePoint home site in draf mode. + +```yaml +Type: SwitchParameter +Parameter Sets: (All) +Required: False +Position: Named +Default value: true +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Force +Use the -Force flag to bypass the confirmation question + +```yaml +Type: SwitchParameter +Parameter Sets: (All) +Required: False +Position: Named +Default value: true +Accept pipeline input: False +Accept wildcard characters: False +``` + ## RELATED LINKS [Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) diff --git a/src/Commands/Admin/SetHomeSite.cs b/src/Commands/Admin/SetHomeSite.cs index 87808ff34..24cd1e555 100644 --- a/src/Commands/Admin/SetHomeSite.cs +++ b/src/Commands/Admin/SetHomeSite.cs @@ -1,6 +1,9 @@ using Microsoft.SharePoint.Client; - +using Microsoft.SharePoint.PortalAndOrgNews; using PnP.PowerShell.Commands.Base; +using System; +using System.Collections.Generic; +using System.Linq; using System.Management.Automation; namespace PnP.PowerShell.Commands.Admin @@ -15,17 +18,74 @@ public class SetHomeSite : PnPAdminCmdlet [Parameter(Mandatory = false)] public SwitchParameter VivaConnectionsDefaultStart; + [Parameter(Mandatory = false)] + public SwitchParameter Force; + + [Parameter(Mandatory = false)] + public SwitchParameter DraftMode; + protected override void ExecuteCmdlet() { - if (VivaConnectionsDefaultStart) + Tenant.EnsureProperties(t => t.IsMultipleVivaConnectionsFlightEnabled, t => t.IsVivaHomeFlightEnabled); + + if (Tenant.IsMultipleVivaConnectionsFlightEnabled) { - Tenant.SetSPHSiteWithConfigurations(HomeSiteUrl, VivaConnectionsDefaultStart); + if (Force.IsPresent || ShouldContinue("Before you update a home site or Viva Connections experiences, make sure you review the documentation at https://aka.ms/homesites. Continue?", string.Empty)) + { + IEnumerable enumerable = Tenant.GetTargetedSitesDetails()?.Where((TargetedSiteDetails hs) => !hs.IsVivaBackendSite); + AdminContext.ExecuteQueryRetry(); + bool flag = false; + if (enumerable == null || enumerable.Count() == 0) + { + Tenant.AddHomeSite(HomeSiteUrl, 1, null); + AdminContext.ExecuteQueryRetry(); + flag = true; + } + else if (enumerable.Count() == 1 && !IsSameSiteUrl(enumerable.First().Url, HomeSiteUrl)) + { + Tenant.RemoveTargetedSite(enumerable.First().SiteId); + AdminContext.ExecuteQueryRetry(); + Tenant.AddHomeSite(HomeSiteUrl, 1, null); + AdminContext.ExecuteQuery(); + flag = true; + } + HomeSiteConfigurationParam configurationParam = new() + { + vivaConnectionsDefaultStart = VivaConnectionsDefaultStart, + IsVivaConnectionsDefaultStartPresent = VivaConnectionsDefaultStart, + isInDraftMode = DraftMode, + IsInDraftModePresent = DraftMode || flag + }; + ClientResult clientResult = Tenant.UpdateTargetedSite(HomeSiteUrl, configurationParam); + AdminContext.ExecuteQueryRetry(); + WriteObject(clientResult.Value); + } } - else + else if (Force.IsPresent || ShouldContinue("Before you set a Home site, make sure you review the documentation at https://aka.ms/homesites. Continue?", string.Empty)) { - Tenant.SetSPHSite(HomeSiteUrl); - } - AdminContext.ExecuteQueryRetry(); + Tenant.ValidateVivaHomeParameterExists(VivaConnectionsDefaultStart); + HomeSiteConfigurationParam configuration = null; + if (VivaConnectionsDefaultStart || DraftMode) + { + configuration = new HomeSiteConfigurationParam + { + vivaConnectionsDefaultStart = VivaConnectionsDefaultStart, + IsVivaConnectionsDefaultStartPresent = VivaConnectionsDefaultStart, + isInDraftMode = DraftMode, + IsInDraftModePresent = DraftMode + }; + } + ClientResult clientResult = Tenant.SetSPHSiteWithConfiguration(HomeSiteUrl, configuration); + AdminContext.ExecuteQueryRetry(); + WriteObject(clientResult.Value); + } + } + + private static bool IsSameSiteUrl(string url1, string url2) + { + Uri uri = new(url1); + Uri uri2 = new(url2); + return Uri.Compare(uri, uri2, UriComponents.Host | UriComponents.Path, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase) == 0; } } } \ No newline at end of file