Hide a custom field from the Document Information Panel

So, I’ve been working with a custom field type recently. It’s quite a complicated one – I’ll not go into details – but we did hit a problem with it. When a Word document was opened it would try to display our field in the Document Information Panel, which isn’t really possible, and caused Word to die in a horrible fashion. I forget the exact error we were getting, but it was something to do with the XSN being invalid.

What we really needed was a way to set that the property wasn’t to be shown in the Document Information Panel. However, there is no easy way of doing this with a purely programmatically created column. There are properties on the SPField object that can be accessed programmatically, and the CAML for a FieldRef can set ShowInFileDlg – but there isn’t an obvious way to set this value from C# code.

Naturally, that’s what we wanted to do. Well, there is a combination approach – the SPField.SchemaXml property allows us to get/set the CAML that defines the field.

So, I came up with this static function:

static void SetShowInFileDlg(SPField f, bool value)
        {
            XmlDocument fieldSchemaXml = new XmlDocument();
            fieldSchemaXml.LoadXml(f.SchemaXml);
            XmlAttribute attr = fieldSchemaXml.CreateAttribute("ShowInFileDlg");
            attr.Value = value.ToString().ToUpper();
            XmlNode fieldXmlNode = fieldSchemaXml.SelectSingleNode("Field");
            XmlAttributeCollection fieldAttributes = fieldXmlNode.Attributes;
            fieldAttributes.Append(attr);

            f.SchemaXml = fieldXmlNode.OuterXml;
            f.Update(true);
        }

I’m not sure what happens if you’re trying to update an already widely used column – will it update all content types that that column – but this worked for us.

Advertisements
Hide a custom field from the Document Information Panel

3 thoughts on “Hide a custom field from the Document Information Panel

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s