Skip Ribbon Commands
Skip to main content
Navigate Up
Sign In
Schaeflein Consulting > Blog > Posts > SPFieldCollection.TryGetFieldByStaticName() is inconsistent
November 29
SPFieldCollection.TryGetFieldByStaticName() is inconsistent

Today’s task required me to check if a column exists on a list, and if not then add it from the site column gallery. Since I knew that SP2010 added a method TryGetList(), I thought I would look around for a similar method for fields.

Sure enough, I found TryGetFieldByStaticName() on the SPFieldCollection class. This is exactly what I was looking for. Why it uses the StaticName property instead of the DisplayName (like the Item method does) or the Internal Name (like CAML does) is totally beyond me. But at least I can check for a field without anticipating an exception.

After hours of digging and bothering people on IM and the twitter, I discovered that TryGetFieldByStaticName() works as you would expect when the SPFieldCollection is based on an SPList object. However, the exact same method based on an SPWeb object (like the site columns gallery) always returns null!

Here is a snippet that illustrates the problem:

TryGetFieldByStaticName sample
  1. private void AddColumnIfNotPresentOnList(SPList list, string columnName)
  2. {
  3.   SPField listField = null;
  4.   SPField siteField = null;
  5.  
  6.   listField = list.Fields.TryGetFieldByStaticName(columnName);
  7.   if (listField == null)
  8.   {
  9.     // *** this line always returns null ***
  10.     siteField = list.ParentWeb.Site.RootWeb.
  11.                   Fields.TryGetFieldByStaticName(columnName);
  12.  
  13.     // had to use the following instead...
  14.     try
  15.     {
  16.       siteField = list.ParentWeb.Site.RootWeb.Fields[columnName];
  17.     }
  18.     catch (Exception)
  19.     {
  20.       // yes, I shudder when writing this code...
  21.     }
  22.     if (siteField != null)
  23.     {
  24.       list.Fields.Add(siteField);
  25.     }
  26.   }
  27. }

 

** NOTE **
The above is an illustration, not working code.

Comments

nxbwwmvo

czgdetdibfgmfjo, <a href="http://www.npgkvbdlbv.com">ysktwavzjm</a> , [url=http://www.ptavtszgtp.com]edzomitblo[/url], http://www.luxtddxotk.com ysktwavzjm
 on 9/17/2012 12:58 PM

oekbztdibfgmfjo, http://ntx-homes.com/ Buy Kamagra, WEwXbkK.

oekbztdibfgmfjo, http://ntx-homes.com/ Buy Kamagra, WEwXbkK.
 on 9/21/2012 8:08 AM

szujftdibfgmfjo, http://tadalafilpharm.com/ tadalafil, BbjmoGz.

szujftdibfgmfjo, http://tadalafilpharm.com/ tadalafil, BbjmoGz.
 on 9/21/2012 8:10 AM

xslwltdibfgmfjo, http://meridiareview.com/ Buy Meridia, ozwnnoT.

xslwltdibfgmfjo, http://meridiareview.com/ Buy Meridia, ozwnnoT.
 on 9/21/2012 8:11 AM

pmdtatdibfgmfjo, http://www.twoladybugs.net/ How long is ativan in the body, FyTJTUB.

pmdtatdibfgmfjo, http://www.twoladybugs.net/ How long is ativan in the body, FyTJTUB.
 on 9/21/2012 8:11 AM

tkjxqtdibfgmfjo, http://stemspacelab.com/ Generic Ativan, RBhkjTs.

tkjxqtdibfgmfjo, http://stemspacelab.com/ Generic Ativan, RBhkjTs.
 on 9/21/2012 8:12 AM

kpzfmtdibfgmfjo, http://www.depressioncrisis.com/64/klonopin-for-panic-attacks/ Klonopin iv, xOmLpvK.

 on 9/21/2012 8:13 AM

itghstdibfgmfjo, http://ultram247.net/ Ultram epilepsy, tJqMWxX.

itghstdibfgmfjo, http://ultram247.net/ Ultram epilepsy, tJqMWxX.
 on 9/21/2012 8:15 AM

lzncrtdibfgmfjo, http://www.buyambien247.com/ Ambien cr dosage, PwfUiUo.

lzncrtdibfgmfjo, http://www.buyambien247.com/ Ambien cr dosage, PwfUiUo.
 on 9/21/2012 8:16 AM

kgcihtdibfgmfjo, http://www.drugsonreview.com/anticonvulsant/ambien/ Side effects of ambien 10 mg, yFwPoFr.

kgcihtdibfgmfjo, http://www.drugsonreview.com/anticonvulsant/ambien/ Side effects of ambien 10 mg, yFwPoFr.
 on 9/21/2012 8:30 AM
1 - 10Next
 

 About this blog

 
Paul Schaeflein - a face for radio

I am a long-time SharePoint developer currently working as a consultant.

I like hockey, margaritas and SharePints.

INETA Community Speakers Program


Blog Blog