16

I have some code where I am going through all of the Fields that belong to a content type. I'm curious if there is a way to check to see if the SPField was created by a user or if it is a system field like Created Date or Title.

  • checking for field.CanBeDeleted should be the easiest thing to determine that field is created by user or not. – Sami Mar 13 '17 at 14:55
20

There are two possibilities here:

1) You need to determine if field is completely custom (added to list by a user from GUI). You can use SourceId property for this purpose. You will get Url, starting with "http://schemas.microsoft.com" for standard fields, and some random GUIDs for custom fields.

Sample code:

SPField field = // get your field from a list or content type here
if (!field.SourceId.StartsWith("http://"))
{
   // do your action for completely custom fields
}

2) You need to determine if field is non-system. There are many system fields, usually filled up with some meta information, and they are usually hidden from list management GUI, but they are in your list, internally. You can use FromBaseType property for this purpose, it will be true for all system fields.

Sample code:

SPField field = // get your field from a list or content type here
if (!field.FromBaseType)
{
   // do your action for "valuable" fields (non-system)
}

For testing, I created list "Contacts" from standard Contacts list definition on my local SharePoint Portal. I added column "User created column" to it from GUI through [List settings].

After this, I run following PS script ($l stores my SPList object):

$l.Fields | select StaticName, FromBaseType, SourceId

And got following result:

enter image description here

  • This will actually in a .NET application so I need a way to do it using the object model. – Abe Miessler Apr 12 '11 at 20:32
  • Oh, PowerShell script is only needed for demonstration of property values for different field types. Anyway, added some C# code to make the answer more visual! Hope this helps :) – Andrey Markeev Apr 12 '11 at 20:54
3

Is a given field one of SharePoint's built in field

  • This has the same result, as SourceId-variant in my answer. But in the same time, it is little more elegant solution. So, my personal +1. – Andrey Markeev Apr 12 '11 at 22:21
0

One note is that these solutions are not failsafe. Some fields are created using OM, some fields that are defined in XML also specify as the SourceID but are not built-in. The short answer is simply 'no, you cannot'.

0

You have two options:

  1. Check if the field is a built-in field:

SPBuiltInFieldId.Contains(field.Id)

  1. Check on the SPField.SourceId (from MSDN):

Gets either the namespace that defines a built-in field or, if it a custom field, the GUID that identifies the list or Web site where it was created.

Reference from https://stackoverflow.com/questions/5340746/custom-fields-only-from-sharepoint-list

0

field.CanBeDeleted gives you purely your created fields and its obvious from name

I could get fields created by me only with

SPField field = // get your field from a list or content type here
if (field.CanBeDeleted)
{
   // do your action for completely custom fields
}

All other things were true for some auto created fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.