Posted by: R Manimaran | November 9, 2010

SharePoint: Setting ShowInFileDlg using Object Model.

In the Previous post I wrote how to set the value for ShowInFileDlg attribute in Fields.xml which will show/hide the field in Word Document Information panel.

Suppose you have added the Site Columns using SharePoint Object model like this.

SPFieldText Fieldtxt = (SPFieldText) Fields.Add(“Keyword”,SPFieldType.Text,false);

You may thing that there will be a properties like ShowInFileDlg. But in object model it is missing.

Then how to set it.

We need to modify the Schema.xml file of the Site Column. I wrote a Small helper function which will set the value for the ShowFileDlg attribute.

public static void ShowHideFieldinFileDlg(string siteUrl,string strFieldName,bool valuestring)

{

using (SPSite site = new SPSite(siteUrl))

{

using (SPWeb web = site.OpenWeb())

{

//Get the Field from the Site Columns

SPField siteColumn = site.RootWeb.Fields.GetField(strFieldName);

XmlDocument fieldSchemaxml = new XmlDocument();

fieldSchemaxml.LoadXml(siteColumn.SchemaXml);

//Create a New Attribute for ShowInFileDlg

XmlAttribute newAttribute = fieldSchemaxml.CreateAttribute(“ShowInFileDlg”);

newAttribute.Value = valuestring.ToString().ToUpper();

XmlNode fldXmlNode = fieldSchemaxml.SelectSingleNode(“Field”);

//get the attribute collection

XmlAttributeCollection attr = fldXmlNode.Attributes;

attr.Append(newAttribute);

siteColumn.SchemaXml = fldXmlNode.OuterXml;

siteColumn.Update(true);

}

}

}

Advertisements

Responses

  1. I tried that, I got an error “value does not fall in the expected range”. Any idea?

    • Hi, in which line you got the error? is it possible to post your code here?

  2. Could I hide the fields per item (dynamically)?

    • For Item level you can’t hide it using OOTB. In that case you need to write JQuery or Javascript in NewForm.aspx or you need to create the custom form for getting the input.


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

Categories

%d bloggers like this: