Skip to content
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

SPSite: Fix: SPSite Constructur returns $null when run as Ressource on SPSE #1443

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- SPShellAdmins
- Fixed that the Member comparison was not case insensitive.
- SPSite
- The Get Method failed to get an existing Site Collection on SharePoint Server
Subscription Edition


## [5.5.0] - 2024-04-22

Expand Down
24 changes: 18 additions & 6 deletions SharePointDsc/DSCResources/MSFT_SPSite/MSFT_SPSite.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,29 @@ function Get-TargetResource
$params = $args[0]
$site = $null

try
$productVersion = Get-SPDscInstalledProductVersion
if ($productVersion.FileMajorPart -eq 16 `
-and $productVersion.FileBuildPart -gt 13000)
{
$centralAdminWebApp = [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local
$centralAdminSite = Get-SPSite -Identity $centralAdminWebApp.Url -ErrorAction Stop

$site = New-Object "Microsoft.SharePoint.SPSite" -ArgumentList @($params.Url, $centralAdminSite.SystemAccount.UserToken)
# On SharePoint Subscription Edition the Microsoft.SharePoint.SPSite Constructor never gets a Site Collection when run by the LCM.
# Fixes 1442: https://github.com/dsccommunity/SharePointDsc/issues/1442
$site = Get-SPSite -Identity $params.Url -ErrorAction SilentlyContinue
}
catch [System.Exception]
else
{
try
{
$centralAdminWebApp = [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local
$centralAdminSite = Get-SPSite -Identity $centralAdminWebApp.Url -ErrorAction Stop

$site = New-Object "Microsoft.SharePoint.SPSite" -ArgumentList @($params.Url, $centralAdminSite.SystemAccount.UserToken)
}
catch [System.Exception]
{
}
}


if ($null -eq $site)
{
Write-Verbose "Site Collection not found"
Expand Down
16 changes: 13 additions & 3 deletions tests/Unit/SharePointDsc/SharePointDsc.SPSite.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Invoke-TestSetup

$script:testEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:DSCModuleName `
-DSCResourceName $script:DSCResourceFullName `
-DscResourceName $script:DSCResourceFullName `
-ResourceType 'Mof' `
-TestType 'Unit'
}
Expand All @@ -50,7 +50,7 @@ try
InModuleScope -ModuleName $script:DSCResourceFullName -ScriptBlock {
Describe -Name $Global:SPDscHelper.DescribeHeader -Fixture {
BeforeAll {
Invoke-Command -ScriptBlock $Global:SPDscHelper.InitializeScript -NoNewScope
Invoke-Command -Scriptblock $Global:SPDscHelper.InitializeScript -NoNewScope

# Initialize tests
try
Expand Down Expand Up @@ -184,6 +184,16 @@ try
$ArgumentList[1] -eq "CentralAdminSystemAccountUserToken"
}

# Mock Get-SPSite for SPSSE on Get-TargetResource Call
if ($Global:SPDscHelper.CurrentStubBuildNumber.Build -gt 13000)
{
Mock -CommandName Get-SPSite -MockWith {
return $null
} -ParameterFilter {
$Identity -eq "http://site.sharepoint.com"
}
}

$global:SPDscGetSPSiteCalled = $false
Mock -CommandName Get-SPSite -MockWith {
if ($global:SPDscGetSPSiteCalled)
Expand Down Expand Up @@ -294,7 +304,7 @@ try
Assert-MockCalled Set-SPSite
}

It "Should return true from the test method" {
It "Should return false from the test method" {
Test-TargetResource @testParams | Should -Be $false
}
}
Expand Down
Loading