Tuesday, January 19, 2010

How to update ShowInFileDlg programmatically


At some point in your SharePoint development life time you will probably need to update the ShowInFileDlg attribute of a Site Column.

If you will try to do that by a feature and update the XML with new Field's attributes values, the changes will only take effect on new instances, and will not effect on exsisting ContentTypes or Lists that use these Site Columns.

Programmatically updating the Site Column and the Content Type, the changes will be deployed to the children as well.

Here is a generic method for updating any attribute of the Field element and forcing the changes to the Content Type and its children:

public void UpdateFieldAttribute(string siteCollectionURL, string siteColumnName, string contentTypeName, string fieldAttributeName, string fieldAttributeValue)
{
SPSite site = null;
SPWeb web = null;
try
{
using (site = new SPSite(siteCollectionURL))
{
using (web = site.OpenWeb())
{

SPField siteColumn = site.RootWeb.Fields.GetField(siteColumnName);
XmlDocument fieldSchemaXml = new XmlDocument();
fieldSchemaXml.LoadXml(siteColumn.SchemaXml);
XmlNode fieldXmlNode = fieldSchemaXml.SelectSingleNode("Field");
XmlAttribute attr = fieldXmlNode.Attributes[fieldAttributeName];
attr.InnerText = fieldAttributeValue; ;

siteColumn.SchemaXml = fieldXmlNode.OuterXml;
siteColumn.Update(true);

SPContentType contentType = site.RootWeb.ContentTypes[contentTypeName];

contentType.Update(true, false); //Updating the content type is important or the changes will not be reflected
}
}
}
finally
{
if (web != null)
web.Dispose();
if (site != null)
site.Dispose();
}
}

12 comments:

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

The information here is great. I will invite my friends here.

Thanks

Anonymous said...

你的部落格很棒,我期待更新喔........................................

Anonymous said...

Genial post and this fill someone in on helped me alot in my college assignement. Thank you on your information.

Anonymous said...

nice one! but why did you create the instance of spweb? it´s never used.

Anonymous said...

jtnkilp, xrumer linklist , eIDrfiS.

神待ち said...

クリスマス目前に家出少女にプレゼントしませんか?神待ちサイトに登録してる娘に連絡するだけで、物凄いプレゼントになるでしょう!!当然プレゼント返しは望みのまま

出合い系 said...

独り身で寂しい彼氏、彼女、クリスマスを一緒に過ごす異性が欲しくありませんか?確実に出会える出合い系サイトでパートナーをゲットして、今年のクリスマスは安心ですね

ワンピース said...

ワンピースの物知り自慢を争ってみませんか?なんか海賊王争いみたいで良いでしょう。ちょっとレアな問題とかも出ますが、結果によってはレアな称号、ヒグマ山賊団の頭の称号が手に入るかもって、いらないかな

何婉羽 said...

Google's vision of a pc in which all the things was on the net turned heads while in the tech world past yr, should the first Chromebooks appeared. The real acer aspire 5536 battery
COMPAQ CQ61 Laptop Batteries earth? Considerably less impressed. Apple's iPad proved a much more persuasive concept for people today spending their hard-earned monies, but Google, undeterred, has gone back again towards the drawing board and up to date its Chrome OS.

Martin said...

Setting ShowInFileDlg doesn't work for Managed Metadata columns. You must customize Document Information Panel. More information can be found here http://social.technet.microsoft.com/Forums/en-US/17e1f67a-cf24-4abe-9366-2fba8e1d8fe4/hide-managed-metadata-column-in-document-information-pannel