2

When iterating the fields of a list how do you know which columns are yours and which are built-in ?

I know a solution for "classic" SDK using SPField.SourceId (https://sharepoint.stackexchange.com/questions/95488/retrieving-columns-created-by-user-not-built-in-columns) but I can't find one for CSOM. I thought UserCreated was the way to go for but it looks like it's not

So how do you tell apart theirs and mine ? Using a combination of Hidden CanBeDeleted FromBaseType ? Checking for a list of special internalNames ?

        ClientContext context = new ClientContext(siteUrl);
        context.Credentials = creds;
        Web web = context.Web;
        var list = web.Lists.GetByTitle(listName);

        context.Load(list.Fields);
        context.ExecuteQuery();

        foreach (Field field in list.Fields)
        {
        bool thisColumnWasCreatedByUser =   
                    field .FromBaseType == false &&
                    //this.InternalName != "Title" && // builtin but I'd rather keep it
                    field .InternalName != "_dlc_DocId" && // probably safe
                    field .InternalName != "_dlc_DocIdUrl" &&
                    field .InternalName != "_dlc_DocIdPersistId" &&
                    field .InternalName != "Combine" && // sucks
                    field .InternalName != "RepairDocument" // sucks too
        }

EDIT

Mixing suggestions by Viraj Gorajia and Dikesh Gandhi, my current solution is

bool thisColumnWasCreatedByUser = 
field .FromBaseType == false &&
(!field .SchemaXml.Contains(" SourceID=\"http")) && // remove "Title", "Combine", "RepairDocument" 
(!field .EntityPropertyName.StartsWith("OData__")) // field .Sealed == true could be working too but I don't know if it could exclude valid results
frenchone
  • 217
  • 2
  • 13

3 Answers3

3

Set condition as shown below:

if(!field.FromBaseType)
{
}
Viraj Gorajia
  • 1,659
  • 1
  • 13
  • 26
  • Yes that's a starting point. But I still have built-in columns after that. "Title" "_dlc_DocId" "_dlc_DocIdUrl" "_dlc_DocIdPersistId" "Combine" "RepairDocument" – frenchone Nov 23 '16 at 10:57
  • Try with this condition like if(!field.FromBaseType && field.ShowInEditForm == null) – Viraj Gorajia Nov 23 '16 at 11:10
  • There's no ShowInEditForm property but only a SetShowInEditForm(bool) (which is quite strange by the way) – frenchone Nov 23 '16 at 11:27
  • Try this one !field.Sealed – Viraj Gorajia Nov 23 '16 at 11:35
  • this doesn't filter "Combine" and "RepairDocument" – frenchone Nov 23 '16 at 13:29
  • "Combine" and "RepairDocument" both are actually inbuilt? – Viraj Gorajia Nov 23 '16 at 13:32
  • I think so. They don't follow our naming convention. They look special in the way their sourceid (in .SchemaXml) starts with "http://". And I can find reference of their ids e52012a0-51eb-4c0c-8dfb-9b8a0ebedcb6 , 5d36727b-bcb2-47d2-a231-1f0bc63b7439 (with same names) on the internet so they look like builtin columns to me. – frenchone Nov 23 '16 at 14:05
  • I don't find "Combine" and "RepairDocument" column in any list or library. If both are hidden, add !field.Hidden in codition. – Viraj Gorajia Nov 24 '16 at 06:01
  • I used !field .SchemaXml.Contains(" SourceID="http"). For reference see https://blogs.msdn.microsoft.com/michael_yeager/2008/11/03/reference-list-for-sharepoint-internal-field-names/ – frenchone Nov 24 '16 at 08:26
3

Right now this is my best answer

bool thisColumnWasCreatedByUser = 
field .FromBaseType == false &&
(!field .SchemaXml.Contains(" SourceID=\"http")) &&
(!field .EntityPropertyName.StartsWith("OData__"))

!field .SchemaXml.Contains(" SourceID=\"http") will remove "Title", "Combine", "RepairDocument"

!field .EntityPropertyName.StartsWith("OData__") will remove "_dlc_DocId", "_dlc_DocIdUrl", "_dlc_DocIdPersistId"

P S
  • 4,827
  • 4
  • 27
  • 50
frenchone
  • 217
  • 2
  • 13
0

Try this:

if (!field.SourceId.StartsWith("http://"))
{
   // do your action for completely custom fields
}

You will get Url, starting with "http://schemas.microsoft.com" for standard built-in fields.

Hope this will help you.

Dikesh Gandhi
  • 6,803
  • 4
  • 30
  • 55
  • 1
    This is for CSOM (Microsoft.sharepoint.client.Field and not Microsoft.Sharepoint.SPField) . You don't have SourceId on Field. I managed to get one by parsing SchemaXml property. But the filter is still incomplete => this filters out "Title" column (and "RepairDocument" and "Combine") but not _dlc_DocId, _dlc_DocIdUrl, _dlc_DocIdPersistId – frenchone Nov 23 '16 at 13:33