How do I fix the error “Expression must evaluate to a node-set” setting web.config entries?
-
22-10-2019 - |
Question
I'm trying to update web.config in SharePoint when the feature is activated. I have created Event receiver. The code inside receiver is:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = (properties.Feature.Parent as SPSite).WebApplication;
SPWebConfigModification modification = new SPWebConfigModification("mode", "system.web/customErrors");
modification.Owner = "modifyAccountFeatureOwner";
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
modification.Value = "Off";
webApp.WebConfigModifications.Add(modification);
webApp.Update();
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
When I'm deploying feature I have received the following error: "Error occurred in deployment step 'Activate Features': Expression must evaluate to a node-set."
What is wrong with this?
Solution
I think the error occurs on this line:
SPWebConfigModification modification = new SPWebConfigModification("mode", "system.web/customErrors");
And the problem is the second parameter
"system.web/customErrors"
which is an XPath expression. And this expression seems to be incorrect, which means it does not select an existing node in the web.config file. It probably has to be:
"configuration/system.web/customErrors/@mode"
Please note: I did not test this.
OTHER TIPS
Add the following line after webApp.WebConfigModifications.Add(modification);
webApp.WebConfigModifications.Clear();
it clears all the non-required modifications from the object.