Skip to content

Fix #5006: improve error message when creating team sites #5013

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

Merged
merged 1 commit into from
Jul 12, 2025
Merged
Show file tree
Hide file tree
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
45 changes: 19 additions & 26 deletions src/Commands/Admin/NewSite.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Linq;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Enums;
using PnP.PowerShell.Commands.Attributes;
using Microsoft.SharePoint.Client;
using PnP.Core.Admin.Model.Microsoft365;
using PnP.Core.Admin.Model.SharePoint;
using PnP.Core.Services;
using PnP.PowerShell.Commands.Attributes;
using PnP.PowerShell.Commands.Enums;
using PnP.PowerShell.Commands.Utilities;
using PnP.Core.Admin.Model.Microsoft365;
using System;
using System.Linq;
using System.Management.Automation;
using System.Text.Json;

namespace PnP.PowerShell.Commands
Expand All @@ -33,7 +33,7 @@ public class NewSite : PnPSharePointCmdlet, IDynamicParameters

[Parameter(Mandatory = false)]
public SwitchParameter Wait;

[Parameter(Mandatory = false)]
public Framework.Enums.TimeZone TimeZone;

Expand Down Expand Up @@ -167,30 +167,23 @@ protected override void ExecuteCmdlet()

if (ClientContext.GetContextSettings()?.Type != Framework.Utilities.Context.ClientContextType.SharePointACSAppOnly)
{
try
{
var rc = Connection.PnPContext.GetSiteCollectionManager().CreateSiteCollection(teamSiteOptions, siteCreationOptions, vanityUrlOptions);
var rc = Connection.PnPContext.GetSiteCollectionManager().CreateSiteCollection(teamSiteOptions, siteCreationOptions, vanityUrlOptions);

if (ParameterSpecified(nameof(TimeZone)))
{
using (var newSiteContext = ClientContext.Clone(rc.Uri.AbsoluteUri))
{
newSiteContext.Web.EnsureProperties(w => w.RegionalSettings, w => w.RegionalSettings.TimeZones);
newSiteContext.Web.RegionalSettings.TimeZone = newSiteContext.Web.RegionalSettings.TimeZones.Where(t => t.Id == ((int)TimeZone)).First();
newSiteContext.Web.RegionalSettings.Update();
newSiteContext.ExecuteQueryRetry();
newSiteContext.Site.EnsureProperty(s => s.Url);
WriteObject(rc.Uri.AbsoluteUri);
}
}
else
if (ParameterSpecified(nameof(TimeZone)))
{
using (var newSiteContext = ClientContext.Clone(rc.Uri.AbsoluteUri))
{
newSiteContext.Web.EnsureProperties(w => w.RegionalSettings, w => w.RegionalSettings.TimeZones);
newSiteContext.Web.RegionalSettings.TimeZone = newSiteContext.Web.RegionalSettings.TimeZones.Where(t => t.Id == ((int)TimeZone)).First();
newSiteContext.Web.RegionalSettings.Update();
newSiteContext.ExecuteQueryRetry();
newSiteContext.Site.EnsureProperty(s => s.Url);
WriteObject(rc.Uri.AbsoluteUri);
}
}
catch (Exception ex)
else
{
LogError(ex);
WriteObject(rc.Uri.AbsoluteUri);
}
}
else
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/Base/PnPConnectedCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ protected override void ProcessRecord()
}
break;
case Core.MicrosoftGraphServiceException pgex:
errorMessage = (pgex.Error as Core.MicrosoftGraphError).Message;
var pgexError = pgex.Error as Core.MicrosoftGraphError;
errorMessage = $"{pgex.Message} - {pgexError.Code} {pgexError.HttpResponseCode} {pgexError.Message}";
break;
default:
errorMessage = ex.Message;
Expand All @@ -116,7 +117,7 @@ protected override void ProcessRecord()

LogError(errorMessage);
}

}
}
}
Expand Down
Loading