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.