0

I got following error when I tried to remove item from the WebConfigModification.

Exception calling "Remove" with "1" argument(s): "Object reference not set to a n instance of an object."

I need to remove all the entries from webConfigModification where owner is PowerShell.

$webApp = Get-SPWebApplication "myhomesite.com"
$webApp.WebConfigModifications

**This returns:**

Name                        : add[@key="RedirectToHelloPage"]
Path                        : configuration/appSettings
Sequence                    : 0
Owner                       : PowerShell
Value                       : <add key="RedirectToHelloPage" value="Hello.aspx" />
Type                        : EnsureChildNode
UpgradedPersistedProperties : {}

Name                        : add[@key="IsString"]
Path                        : configuration/appSettings
Sequence                    : 0
Owner                       : PowerShell
Value                       : <add key="IsString" value="false" />
Type                        : EnsureChildNode
UpgradedPersistedProperties : {}

Name                        : add[@key="IsString"]
Path                        : configuration/appSettings
Sequence                    : 0
Owner                       : PowerShell112
Value                       : <add key="IsString" value="false" >
Type                        : EnsureChildNode
UpgradedPersistedProperties : {}

And I want to Remove all entries with Owner = PowerShell

$config = $webApp.WebConfigModifications | Where-Object {$_.Owner -eq "PowerShell"}
$webApp.WebConfigModifications.Remove($config)
$webApp.Update()
$webApp.Parent.ApplyWebConfigModifications()

$config contains:

PS C:\> write-host $config
Microsoft.SharePoint.Administration.SPWebConfigModification

But I always get above mentioned error: Can somebody help?

2 Answers 2

1

There is an example on Technet illustrating how to remove WebConfigurationSection's in PowerShell here.

This will delete all entries, but you could easily change it to work for specific sections:

#=================================================================== 
# function to remove all existing node and changes to attributes matching the Owner name 
#=================================================================== 
function DeleteSections($webapp, $Owner, $removeOnly) 
{ 
    $count =  $webapp.WebConfigModifications.Count 
    $foundNodes = $false 
    for ($i=$count ;$i -gt -1; $i--) 
    { 
        if ($webapp.WebConfigModifications[$i].Owner -eq $Owner) 
        { 
            $foundNodes = $true 
            Write-Host "`n$i" 
            $webapp.WebConfigModifications.Remove($webapp.WebConfigModifications[$i]) 
        } 
    } 

    if ($foundNodes -and $removeOnly) 
    { 
        $webapp.Parent.ApplyWebConfigModifications() 
        $webapp.Update() 
    } 
} 
3
  • I updated the answer with specific PowerShell code sample. Could you try with that and report back? If this doesnt work it might be a problem on "your side" such as missing permissions or similar Commented Aug 7, 2013 at 10:35
  • This code generates same problem. Exception calling "Remove" with "1" argument(s): "Object reference not set to an instance of an object." I am running as Administrator. I really don't understand reason behind this error...really frustating..But thanks anyway... Commented Aug 7, 2013 at 13:02
  • you should run the code in ISE and attach a break point to see the exact content of the variable that you feed to the method. Please note that comments should not be abused for chat like troubleshooting. Futher comments and questions should be moved to the chat area. Commented Aug 7, 2013 at 13:04
1

For others that find this thread due to searching for the "Object reference not set" error when trying to call SPWebConfigModifications.Remove... In my case this was caused by the SPWebConfigModification being created with a null value for the 'name' property. I had to programmatically set the name (make sure it is unique). Then I could remove the item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.