1

I have created an web event receiver but the webprovisioned event handler fires twice. I have set the scope to web, but it still fires twice.

Problem 1

This is elements file:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers Scope="Web">
    <Receiver>
      <Name>WebEventReceiverWebProvisioned</Name>
      <Type>WebProvisioned</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>WebEventReceiver</Class>
      <SequenceNumber>10000</SequenceNumber>         
    </Receiver>
  </Receivers>
</Elements>

Event:

    public override void WebProvisioned(SPWebEventProperties properties)
    {
    SPWeb web = (SPWeb)properties.Web
web.Navigation.UseShared = true;
    web.Update();
    }

I saw another post which mentioned that if the scope is set to web it will fire only once, but it my case it is still firing.

Update 1

I tried the following code. It sets the options correctly, but they are not applied to the UI yet. I have to click OK once again to see the changes.

Just to be sure that you guys get what I am trying to achieve. I am trying to set the top navigation, not the left navigation.

                    // PublishingWeb Navigation
                    var pubWeb = PublishingWeb.GetPublishingWeb(web);

                    var webNavigationSettings = new WebNavigationSettings(web);


                    webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb;


                    webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.PortalProvider;
                    pubWeb.Navigation.CurrentIncludeSubSites = true;
                    pubWeb.Navigation.CurrentIncludePages = true;
                    pubWeb.Navigation.CurrentDynamicChildLimit = 20;

                    pubWeb.IncludeInGlobalNavigation = true;
                    pubWeb.Navigation.InheritGlobal = true;
                    pubWeb.Navigation.GlobalIncludeSubSites = true;
                    pubWeb.Navigation.GlobalIncludePages = true;

                    webNavigationSettings.AddNewPagesToNavigation = false;
                    webNavigationSettings.CreateFriendlyUrlsForNewPages = false;

                    webNavigationSettings.Update();
                    pubWeb.Update();
                    web.Update();

Problem 2

When I create a new site the top navigation does not get applied to this newly created site.Any ideas?

2 Answers 2

1

You might have a couple instances of the same event receiver attached. You can check by running the following PowerShell commands and look for duplicates:

If you attach event receivers to a site collection:

Add-PSSnapin microsoft.sharepoint.powershell
$site = Get-SPWeb http://webapp/sites/SiteCollection
$site.EventReceivers | sort Type | ft Class, Type

If you attach event receivers to a web:

Add-PSSnapin microsoft.sharepoint.powershell
$web = Get-SPWeb http://webapp/sites/SiteCollection/web
$web.EventReceivers | sort Type | ft Class, Type

For your second problem, try using the following code:

var web = properties.Web as SPWeb;
var pubWeb = PublishingWeb.GetPublishingWeb(web);
pubWeb.InheritGlobalNavigation = true;
pubWeb.Update();

or

var web = properties.Web as SPWeb;
var pubWeb = PublishingWeb.GetPublishingWeb(web);
pubWeb.Navigation.InheritGlobal = true;
pubWeb.Update();

You might need to update web object as well.

Hope this helps.

0

Problem 1

The EventReceiver is normal. Please change the scope of your Feature where the EventReceiver is provisioned from Web to WebApplication Scope or create a new one and add your EventReceiver to a WebApplication scoped Feature.

Problem 2

And here is the solution for your navigation problem https://msdn.microsoft.com/en-us/library/office/ms196088(v=office.14).aspx.

childWeb.Navigation.UseShared = true;

Your Answer

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

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