Manipulating site navigation when provisioning a site

by zebsadiq 21. November 2012 07:38

Quite often, a client wants a SharePoint site to be completely setup after deployment. Such was the case on a recent project that I worked on.

I had a custom site configuration inside a custom ONET.xml file. Which defined a set of features to be activated in a particular order. One of these features was designed to manipulate the current web’s navigation settings.

Once the solution was deployed in a dev environment, I found that the navigation feature would error if it was being activated from the ONET.xml at the time the SPWeb was being provisioned. However, if one activated this feature manually from site settings, it would function perfectly fine.

This made me curious enough to formulate a solution. The activation of the navigation feature was one of the only manual steps required in the deployment guide and made the process a little bit inconsistent.

I guessed that this would be a problem with the timing of events taking place at site provisioning time. Here was my solution to the problem. Notice that the FeatureActivated code creates a new thread which calls the SetUpNavigation() method. Inside this method, you’ll find that the thread delays itself for 3 seconds by calling the Thread.Sleep(3000) method. Delaying this functionality was key to getting the feature working at the time that the site is provisioned.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			if (TraceSwitch.TraceInfo)
			{
				TraceHelper.TraceInfo("Entering 'FeatureActivated'");
			}

			#region -- Navigation --
			try
			{
				if (properties.Feature.Parent is SPWeb)
				{
					SPWeb web = (SPWeb)properties.Feature.Parent;

                    ThreadPool.QueueUserWorkItem(new WaitCallback(SetUpNavigation), web.Url);
				}
				else
				{
                    if (TraceSwitch.TraceError) TraceHelper.TraceError("Parent is not a Web");
			
				}
			}
			catch (Exception exception)
			{
				var stringBuilder = new StringBuilder();
				stringBuilder.AppendLine(String.Format("-- Main exception  - Source:{0} Message:{1}", exception.Source, exception.Message));

				while (exception.InnerException != null)
				{
					stringBuilder.AppendLine(String.Format("-- Inner exception - Source:{0} Message:{1}", exception.Source, exception.Message));
					exception = exception.InnerException;
				}

				var error = stringBuilder.ToString();

				if (TraceSwitch.TraceError)
				{
					TraceHelper.TraceError(error);
				}
			}
			#endregion

			if (TraceSwitch.TraceInfo)
			{
				TraceHelper.TraceInfo("Exiting 'FeatureActivated'");
			}
		}

        public void SetUpNavigation(object state)
        {
            Thread.Sleep(3000);
            try
            {

                string url = (string)state;
                using (SPSite site = new SPSite(url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {

                        #region -- Navigation --

                        if(web.Provisioned)
                        {
                            // Fix navigation
                            var publishingWeb = PublishingWeb.GetPublishingWeb(web);
                            publishingWeb.InheritGlobalNavigation = false;
                            publishingWeb.Update();
                        }
                        else
                        {
                            ThreadPool.QueueUserWorkItem(new WaitCallback(SetUpNavigation), state);
                        }
                        
                        #endregion
                    }
                }
            }
            catch (Exception exception)
            {
                var stringBuilder = new StringBuilder();
                stringBuilder.AppendLine(String.Format("-- Main exception  - Source:{0} Message:{1}", exception.Source, exception.Message));

                while (exception.InnerException != null)
                {
                    stringBuilder.AppendLine(String.Format("-- Inner exception - Source:{0} Message:{1}", exception.Source, exception.Message));
                    exception = exception.InnerException;
                }

                var error = stringBuilder.ToString();

                if (TraceSwitch.TraceError)
                {
                    TraceHelper.TraceError(error);
                }
            }
        }

Tags: , , ,

C# | SharePoint 2007 | SharePoint Feature

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Calendar

<<  July 2019  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar