Wednesday, February 13, 2013

Field with name … was not found - SPFieldCollection

A booby trap is a device or setup that is intended to kill, harm or surprise a person, unknowingly triggered by the presence or actions of the victim.

The SharePoint 2010 Object Model has a couple of ‘booby traps’ and if you don’t pay careful attention you might end up wasting precious time trying to troubleshoot code which should be working.

The following issue is a classic example of syntax which is really easy to implement and descriptive enough to prevent us from making mistakes…but it can be a little misleading.

If you want to retrieve an existing site column (field) from a SPWeb and you know what the internal name of the field is you can use the following to get the field:

SPField newField = web.Fields.GetField(fieldname);

If you don’t know the internal name of the field but you know what the display name of the field is you can use the following to get the field:

SPField newField = web.Fields.GetField(fieldname);

or

SPField newField = web.Fields[fieldname];

Now, imagine you want to programmatically (C#) add a site column (field) to a SharePoint list, library or content type.

First you want to check if the field exists, then you want to retrieve the field and finally you want to add the field to a list, library or content type.

1-You can use the display name of the field to check if it exists:

image

The result is = true so

2-You want to use the display name of the field to fetch the field from the web:

image

You can see from the two screenshots (above) that the comments indicate that the ‘display name’ can be used in both functions so by looking at it one would assume that if web.Fields.ContainsField(“SolutionExpert”) is true then you will be able to retrieve the field by using SPField newfield = web.Fields[“SolutionExpert”] ... but there is a problem!!!

The internal name of my field is ‘SolutionExpert’ and the display name is ‘Solution Expert’ so I made a mistake!!

If you look closely at web.Fields.ContainsField(string fieldName) you will see that the method accepts either the display name or the internal name of the field, so you might find yourself in a situation where you passed in the internal name of a field to web.Fields.ContainsField and the result was true, but when you then use the same field name to try access the field with newfield = web.Fields[fieldname] you will get an error because newfield = web.Fields[fieldname] only accepts the display name of the field and not the internal name.

Example:

image

So, remember that it is much safer to reference fields by its internal name and instead of using

SPField newField = web.Fields[fieldname]

rather use:

SPField newField = web.Fields.GetField(fieldname);

image

Also see: http://msdn.microsoft.com/en-us/library/ms196070.aspx

3 comments:

ADmin said...

When you have this essential structure set up, you can begin pondering what number this site of expressions to compose for every part of your article.

Arsalan Yousuf said...

Thank you for this fantastic and needful read. I was very encouraged to find this site. Thanks a lot. coursework writing

Jason said...

Thank you also my error is Column with name "'HybridParserTimeAvg' was not found." I wanted to post this to show that this isn't just about the above fields. But the code can be used if slightly modified.

Post a Comment