How do I automatically configure the "Top Link Bar" to "Use Links from Parent" as the default for all new sites in a non-publishing site collection, regardless of what a user might choose in the Navigation Inheritance section of the newsbweb.aspx page?
Situation: On-Prem SharePoint 2016, Team Site site collection, created a new Visual Studio 2015 farm solution and added a new Event Receiver of type Web Events that handles "A site was provisioned".
Configured the feature Visual Studio created for it with a title and description and a scope of Site, then modified the Elements.xml like so:
<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"><Receivers Scope="Site" ><Receiver><Name>InheritNavTestEventReceiver1</Name><Type>WebProvisioned</Type><Assembly>$SharePoint.Project.AssemblyFullName$</Assembly><Class>InheritNavTest.EventReceiver1.EventReceiver1</Class><SequenceNumber>99999</SequenceNumber><Synchronization>Synchronous</Synchronization></Receiver></Receivers></Elements>
Coded the event receiver so that it would set the description of the site at the same time (so I would know it was executing.)
public class EventReceiver1 : SPWebEventReceiver { public override void WebProvisioned(SPWebEventProperties properties) { base.WebProvisioned(properties); bool saveState = properties.Web.AllowUnsafeUpdates; properties.Web.AllowUnsafeUpdates = true; properties.Web.Navigation.UseShared = true; properties.Web.Description = "my Navigation.UseShared is now set to " + properties.Web.Navigation.UseShared.ToString(); properties.Web.Update(); properties.Web.AllowUnsafeUpdates = saveState; } }
If I use the PowerShell cmdlet to create the site, this WebProvisioned SPEventReceiver works great--sets both the web.Navigation.UseShared property along with the Description. No problem.
new-spweb http://sp2016a/sites/TS1/ps1 -Template "STS#0"
But here's the thing: if I create a new site using the "new subsite" link on the "Site Contents" web page, my code replaces the description with "my Navigation.UseShared is now set to True"; however, that property is somehow set back to false and the Top link bar settings page isn't using links from the parent site.
Afterwards, I can fix it in settings, or I can use PowerShell to fix it:
$w=get-spweb http://sp2016a/sites/TS1/gui2 $w.Navigation.UseShared=$true #no $w.update() required
But, the requirement is to hide that section on the new site web page (with CSS) and default all subsites to inherit the Top Link Bar when they are created regardless of how (web page, PowerShell, CSOM, site templates).
I've searched Bing for the terms webprovisioned with navigation.useshared and I can see tons of examples where this should work. Do you have the book "Inside Microsoft SharePoint 2013"? Chapter 11 has this exact same property and event receiver as the example. Someone on the forums said to create a PostConfigurationHandler.ashx but didn't say how (don't think that would apply to PowerShell and CSOM anyway)
I've tried creating another feature that sets the property, and then used Web.Features.Add() to activate the feature from the WebProvisioned() function, but that didn't work either. Nor did feature stapling. Nor doing it all over again in SharePoint 2013. What's the missing ingredient that will make this work no matter how the site is created?