Pergunta

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.

Foi útil?

Solução

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

Outras dicas

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'.

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

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top