0

I'm trying to configure a team site to "Use Links from Parent" in the "Top Link Bar" whenever a site (web) is created, regardless of what the user chooses on the New SharePoint Site page (gonna hide that section). I'm following this example from chapter 11 of Inside Microsoft SharePoint 2013, but I can't get it to work for me.

Here's the relevant code if you can't see the chapter (I added the Kilroy grafitti):

public override void WebProvisioned(SPWebEventProperties properties)
    {
        base.WebProvisioned(properties);
        SPWeb web = properties.Web;
        // Let sub site navigation inherit from the parent site
        web.Navigation.UseShared = true;
        web.Description = "Kilroy was here!"; // I added this just to make sure it runs
        web.Update();
    }

Strangely enough, If I use PowerShell, it works perfectly: it sets the description and also overrides the UseParentTopNav switch parameter, setting the Navigation.UseShared property to true.

New-SPWeb -Template "STS#0" -UseParentTopNav:$false -url http://mysite.test/TS1/ps1

However, I can't seem to get the property to stick when creating a site using the web page. I know that the event receiver is executing, because it replaces the description with "Kilroy was here!" But, the new site doesn't inherit from the parent until I go into the settings page or set Navigation.UseShared = $true with PowerShell. What am I missing?

0

After a lot of hacking and slashing, I finally settled on creating an asynchronous task in my WebProvisioned() event receiver that watches for SharePoint changing Navigation.UseShared back to false. I'm not real crazy about this solution though.

using System;
using System.Threading.Tasks;

properties.Web.Navigation.UseShared = true;

// the .UseShared doesn't stick if site is created through the gui, so we have try to make to stick it via a background task
Guid webId = properties.Web.ID;
Guid siteId = properties.Web.Site.ID;
Task.Factory.StartNew(() =>
{
    using (SPSite site = new SPSite(siteId))
    {
        for (int x = 0; x < 30; x++) // check twice a second for the next 15 seconds, then give it up
        {
            using (SPWeb web = site.OpenWeb(webId))
            {
                if (web.Navigation.UseShared == false)
                {
                    web.Navigation.UseShared = true;
                    break;
                }
                else
                {
                    Task.Delay(500).Wait();
                }
            }
        }
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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