I am trying to update web.config using SPWebConfigModification and I am getting the following error:

Exception calling "ApplyWebConfigModifications" with "0" argument(s): "Object reference not set to an instance of an
At line:11 char:1
+ $app.Parent.ApplyWebConfigModifications();
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException

This is my powershell script:

$app = Get-SPWebApplication http://test:8000/
$mod = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$mod.Path = “configuration/SharePoint/WebPartLimits”
$mod.Name = “MaxZoneParts”
$mod.Value = “100”
$mod.Sequence = 0
$mod.Owner = “webpartmod”
$mod.Type = 1    

I have verified that $app and $mod are not null. What else could be causing this error?

1 Answer 1


The error is stating that you can’t call ApplyWebConfigModifications() on $app.Parent. In this case, it is the Parent property of $app that is null.

SPWebApplication’s Parent property is of type SPPersistedObject which doesn’t have an ApplyWebConfigModifications method and is most likely null in this case as SPWebApplications aren’t hierarchical/nested.

ApplyWebConfigModifications() does exist on the SPWebService property of the SPWebApplication.

The line should read: $app.WebService.ApplyWebConfigModifications();

  • Thanks, this works on all except the central admin, which I also want to update
    – klevans
    Sep 18, 2020 at 11:53
  • What do you mean by “not working on central admin”? You need to use -IncludeCentralAdministration in the call to Get-SPWebApplication to have it be one of the objects returned.
    – Greg W
    Sep 18, 2020 at 11:57
  • Sorry, I mean I get the same error above when $app is set to central admin. All others work
    – klevans
    Sep 18, 2020 at 12:11
  • My guess is CA doesn’t support custom web.config mods.
    – Greg W
    Sep 18, 2020 at 12:12

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.