SharePoint - Feature Receiver code : To update the navigation settings of Publishing Web

SharePoint - Feature Receiver code: To update the navigation settings of Publishing Web.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    WebNavigationSettings settings = new WebNavigationSettings(web);
    bool allowUnsafeUpdateFlag = false;

    try
    {
        if (web != null)
        {
            allowUnsafeUpdateFlag = web.AllowUnsafeUpdates;
            if (!allowUnsafeUpdateFlag)
            {
                web.AllowUnsafeUpdates = true;
            }

            try
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                settings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
                //settings as per the project need
                publishingWeb.Navigation.CurrentIncludePages = false;
                publishingWeb.Navigation.GlobalIncludePages = false;
                publishingWeb.Navigation.GlobalIncludeSubSites = false;

                publishingWeb.Update();
            }
            catch (Exception ex)
            {
                // Handle the exception
            }
        }
    }
    catch (Exception ex)
    {
        // Handle the exception
    }
}

Brief summary of what your code does,

  • It checks if a valid SPWeb object is obtained from the feature properties.
  • It temporarily sets the web.AllowUnsafeUpdates property to true to allow updates to the web.
  • It retrieves the PublishingWeb object and updates its navigation settings.
  • It catches any exceptions that might occur during this process, although the catch blocks are empty in your provided code.