Wednesday 16 January 2008

SPFieldCollection indexer (Field[string]) Vs SPFieldCollection. GetField(string)

The Question can be asked don’t they do the same thing and reading the SDK doesn’t clear that up (see http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spfieldcollection.aspx )
So a bit of work with Reflector and the difference becomes apparent

The Indexer calls the internal method GetFieldByDisplayName(strDisplayName, true)
Which in turn accesses the Display Name Hashtable using the parameter as the key value DisplayNameDict[strDisplayName];
(Note the second Pramater of GetFieldByDisplayName is a bool which sets if the method will throw any exceptions)
GetField(string) calls GetField(strName, true) which in turn will call GetFieldByInternalName(strName, false) which accesses the Internal Name Hashtable using the parameter as the key value InternalNameDict[strName] if this fails to return a value it accesses the InternalNameWithPrefixDict Hashtable. If GetFieldByInternalName fails to return a value GetField will make a call to GetFieldByDisplayName(strName, false)

In practice how does this help
The Indexer only works with Display Names while the GetField() will look for the internal name first but if it can’t find a field it will then try the parameter as the display name. This matches up nicely with the ContainsField(string) method as this method will return true if the string is found as ether a display name or a internal name. Using this we have a nice way of avoiding unnecessary exception handling when accessing List data

Eg
if (item.Fields.ContainsField(_fieldName))
{
holderstring = item[_fieldName].ToString();
}

Not checking to see if the field exists can cause a System.NullReferenceException - Object reference not set to an instance of an object on the ToString() method

Note there is also a GetFieldByInternalName(string) which will only call GetFieldByInternalName(strName, false)

Been a while

It has been a while (6 months) since my last post and in that time I have acquired ownership of some rather messy projects (Please I am begging people don’t modify out of the box files even if you think you know what you are doing because most likely YOU DONT …. ok maybe Application.master and the like but then again only if absolutely necessary and you and your client understand what Microsoft has to say http://blogs.msdn.com/sharepoint/archive/2007/10/30/kb-944105-how-to-customize-application-pages-in-the-layouts-folder-in-wss-3-0-and-moss-2007.aspx ) and passed my MCTS - WSS 3.0 Application Development gotten engaged and learnt a lot about WSS and MOSS and to be honest ASP.NET

I will try and Blog all of what I have discovered and where I went badly wrong over the next few months and return what I can to the SharePoint community.
Hope to post more as time goes on :)

PS I would like to see more internet facing WSS 3/Moss sites on the web they are easy to develop and even easier to maintain for the non technical minded.