19
Feb
10

InfoPath Code to Hide All Toolbars When Filling Out a Form

There are probably several ways to do this, but this seemed pretty straightforward to me.  The code loops through the CommandBars collection that is part of Microsoft.Office.Core and sets the Enabled property to false.  If you want, you can selectively inspect each toolbar in the collection and enable/disable specific ones.

You can use the Context Changed Event to set a spiffy title for your form.  Be aware that, if the Context Changed Event is grayed out, you need to change the form’s browser compatibility options so it is not able to be opened in InfoPath Forms Services.

image

Uncheck the box “Design a form template that can be opened in a browser or InfoPath.”

image

Since we are using code, you’ll have to fully trust the form and sign it.

image

We’re using C# in this example, so set the form template code language to C#.

image

Add a reference to Microsoft.Office.Core by browsing to MSO.DLL in your Microsoft Shared folder.  You can see it here in my list of recent references.

image

image

Now you should be able to add the USING statement for Microsoft.Office.Core.

 

using Microsoft.Office.InfoPath;
using Microsoft.Office.Core; // Required for Commandbars
using System;
using System.Xml;
using System.Xml.XPath;
using System.Windows.Forms;
using mshtml;

namespace Test_Form
{
    public partial class FormCode
    {
        #region EventHandlers
        public void InternalStartup()
        {
            EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
            EventManager.FormEvents.ContextChanged += new ContextChangedEventHandler(FormEvents_ContextChanged);           
        }
        #endregion

        #region Toolbars

        public void FormEvents_ContextChanged(object sender, ContextChangedEventArgs e)
        {
            if (this.New)
            {
                this.Application.ActiveWindow.Caption = “Fill out my form!”;
            }
        }

        public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
            HideAllToolbars();
        }

        void HideAllToolbars()
        {
            // The main toolbar with the file menu will be hidden (msoBarTypeMenuBar)
            // along with all the other toolbars (msoBarTypeNormal)
            // if HideToolbarsFlag is true in the datasource
            /* List of enumerated menu bars
                Menu Bar msoBarTypeMenuBar 1
                Standard msoBarTypeNormal 2
                Print Preview
                Formatting
                Tables
                Ink
                Task Pane
                Property Editor
                Office Clipboard
                XML Source
                Research
                XML Document
                Signatures
                Document Actions
                Clip Art
                Selection and Visibility
                Document Management
                Document Updates
                Mail Merge Panes
                Fax Service
                Meeting Workspace
                Attachment Options
                Clipboard
                Envelope
                System
                Online Meeting
             * */
            XPathNavigator root = MainDataSource.CreateNavigator();
            string hideToolbarsFlag = root.SelectSingleNode(“//my:HideToolbarsFlag”, NamespaceManager).ToString();
            bool flag;
            if (hideToolbarsFlag.ToLower() == “true”)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }

            CommandBars myCommandBars = this.Application.ActiveWindow.CommandBars as CommandBars;
            CommandBar myCommandBar = (this.Application.ActiveWindow.CommandBars as CommandBars).ActiveMenuBar;
            foreach (CommandBar item in myCommandBars)
            {
                //MessageBox.Show(item.Name.ToString() + ” ” + item.Type.ToString() + ” ” + item.Index.ToString());
                if (flag == true && (item.Type == MsoBarType.msoBarTypeMenuBar || item.Type == MsoBarType.msoBarTypeNormal))
                {
                    item.Enabled = false;
                }
            }
        }
        #endregion

    }
}

In order to control whether the toolbars are displayed or not, a field is added to the data source.  Add the element, HideToolbarsFlag, that will control whether or not to hide the toolbars.

image

image

This bit of code retrieves it’s value.  If you set the default value to true, it hides the toolbars; anything else, it doesn’t.

string hideToolbarsFlag = root.SelectSingleNode(“//my:HideToolbarsFlag”, NamespaceManager).ToString();

And now preview your form.

image

Advertisement

2 Responses to “InfoPath Code to Hide All Toolbars When Filling Out a Form”


  1. 1 Diana
    June 3, 2012 at 11:33 pm

    This works nicely in 2007, but not so much in 2010. The Menu Bar and Quick Launch are still visible. Would you have code to resolve this in InfoPath 2010?

    Thank you,

    Diana

  2. 2 Chris
    October 8, 2014 at 1:11 pm

    Yes, 2010 update please!!!!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


Asif Rehmani’s SharePoint Videos

SharePoint-Videos

Click to access a wealth of SharePoint videos

SharePoint Rx

SharePoint Rx Home

Categories

Posts by Date

February 2010
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
Support Wikipedia

%d bloggers like this: