6

I am working with the Client Side Object Model. In a console application I am retrieving all fields from a custom list. The problem is the Clientcontext fetches me a bunch of internal fields I do not want to be included. Also this causes some of the fields to appear more than once.

string siteURL = "http:XYZ";
ClientContext context = new ClientContext(siteURL);
Web oWebSite = context.Web;
context.Load(oWebSite);
context.ExecuteQuery();

//Get the list by title
List produktKatalogListe = spLists.GetByTitle("Produktkatalog");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View/>";
ListItemCollection listItems = produktKatalogListe.GetItems(camlQuery);
context.Load(produktKatalogListe);
context.Load(listItems);
context.Load(produktKatalogListe.Fields);
context.ExecuteQuery();

foreach(Field field in produktKatalogListe.Fields)
{
Console.WriteLine("{0} - {1} - {2} - {3} - {4}",field.Title,field.InternalName,field.Hidden,field.CanBeDeleted,field.FieldTypeKind);
}

Is there a way to print only custom fields? This would mean omitting fields like internalID, GUID...

I tried the following:

if(!field.Hidden)
{
Console.WriteLine("{0} - {1} - {2} - {3} - {4}",field.Title,field.InternalName,field.Hidden,field.CanBeDeleted,field.FieldTypeKind);
}

Unfortunately this not only does not solve the issue but is also not a very good solution for the case I do want to display custom but hidden fields.

2 Answers 2

8

In SSOM SPField.SourceId property allows to determine

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.

In CSOM SourceId property is not exposed for Field object, but it could be extracted from SchemaXml as demonstrated below:

    private static Dictionary<string, string> ParseSchemaXml(string schemaXml)
    {
        var properties = new Dictionary<string, string>();
        var xdoc = XDocument.Parse(schemaXml);
        var attributes = xdoc.Descendants("Field").Attributes();
        foreach (var attr in attributes)
        {
          if(!properties.ContainsKey(attr.Name.LocalName))
              properties.Add(attr.Name.LocalName,attr.Value);
        }
        return properties;
    }


    //Usage
    var properties = ParseSchemaXml(field.SchemaXml);
    var sourceId = properties["SourceID"]; 

The below example demonstrates how to retrieve and print custom fields for a List:

    public static void PrintCustomFields(string url, string listTitle)
    {
        const string fieldBuiltInNS = "http://schemas.microsoft.com/sharepoint/v3";
        using (var context = new ClientContext(url))
        {
            //context.Credentials = credentials;
            var web = context.Web;
            var list = web.Lists.GetByTitle(listTitle);
            web.Context.Load(list, l => l.Fields);
            web.Context.ExecuteQuery();


            foreach (var field in list.Fields)
            {
                var properties = ParseSchemaXml(field.SchemaXml);
                if (properties["SourceID"] != fieldBuiltInNS)
                {
                    Console.WriteLine("Field: {0}",field.InternalName);
                }

            }

        }
    }
5

The following approach seems to solve the issue. Instead for checking if the field is not Hidden I checked whether it is not FromBaseType.

if(!field.FromBaseType)
{
Console.WriteLine("{0} - {1} - {2} - {3} - {4}",field.Title,field.InternalName,field.Hidden,field.CanBeDeleted,field.FieldTypeKind);
}
1
  • this worked for me. in JSOM: oField.get_fromBaseType()
    – Mike
    Oct 4, 2017 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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