SharePoint - Client Side Object Model(CSOM) - Managed - Get All Non-System Fields Data

Problem Definition

Retrieve List Items details using Managed Code and Client Side Object Model(CSOM) for SharePoint 2013. If you try to write generic to get the user only fields it breaks due to some system fields that do not have any data and are not initialized.

Prerequisites

Solution

Present solution uses a list of Internal names that are system names except ID and Title. List is present here. In case link gets removed I will be passing the list int code example section.

Special List sytemFieldsTitles


List<string> systemFieldsTitles = new List<string>()
{          
    "ContentTypeId",
    "_ModerationComments",
    "File_x0020_Type",
    "ContentType",
    "Modified",
    "Created",
    "Author",
    "Editor",
    "_HasCopyDestinations",
    "_CopySource",
    "owshiddenversion",
    "WorkflowVersion",
    "_UIVersion",
    "_UIVersionString",
    //"Attachments", // I Commented this one
    "_ModerationStatus",
    "Edit",
    "LinkTitleNoMenu",
    "LinkTitle",
    "LinkTitle2",
    "SelectTitle",
    "InstanceID",
    "Order",
    "GUID",
    "WorkflowInstanceID",
    "FileRef",
    "FileDirRef",
    "Last_x0020_Modified",
    "Created_x0020_Date",
    "FSObjType",
    "SortBehavior",
    "PermMask",
    "FileLeafRef",
    "UniqueId",
    "SyncClientId",
    "ProgId",
    "ScopeId",
    "HTML_x0020_File_x0020_Type",
    "_EditMenuTableStart",
    "_EditMenuTableStart2",
    "_EditMenuTableEnd",
    "LinkFilenameNoMenu",
    "LinkFilename",
    "LinkFilename2",
    "DocIcon",
    "ServerUrl",
    "EncodedAbsUrl",
    "BaseName",
    "MetaInfo",
    "_Level",
    "_IsCurrentVersion",
    "ItemChildCount",
    "FolderChildCount",
    "Restricted",
    "OriginatorId",
    "NoExecute",
    "AppAuthor",
    "AppEditor",
    "SMTotalSize",
    "SMLastModifiedDate",
    "SMTotalFileStreamSize",
    "SMTotalFileCount",
};

Code Example



/// <summary>
/// Build expression to filter only user data
/// </summary>
/// <param name="fields">List of fields to incluse</param>
/// <returns>Expression of fields</returns>
public static Expression<Func<ListItem, object>>[] GetRequiredFieldsExpressions(IEnumerable<Field> fields)
{
            var expressionsList = new List<Expression<Func<ListItem, object>>>();
            foreach (var item in fields)
            {
                var internalName = item.InternalName;
                expressionsList.Add(x => x[internalName]);
            }
            return expressionsList.ToArray();
}

/// <summary>
/// Print list Items with all user fields
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetItemsDetails(string listTitle)
{
            //var spContext = new ClientContext("http://<your server>/<site>/");
            using (spContext)
            {

                // Client side SP Web Conext
                var web = spContext.Web;
                // Initilize web and load web object
                spContext.Load(web);
                // Get list details by list title
                var list = web.Lists.GetByTitle(listTitle);
                var fields = list.Fields;
                spContext.Load(list);
                spContext.Load(fields);
                spContext.ExecuteQuery();
                //Get data CAML query
                ListItemCollection collListItem = list.GetItems(new CamlQuery());
                //Get List of Field Internal Names that are non system
                var filteredFields = fields.ToList().Where(x => !systemFieldsTitles.Any(y => y == x.InternalName));
                //Fetch the expression for non system only fields data
                var expressions = GetRequiredFieldsExpressions(filteredFields);
                //Intilize and load the non system fields
                spContext.Load(collListItem, items => items.Include(expressions));
                //Execute query to load data
                spContext.ExecuteQuery();

                foreach (var item in collListItem)
                {
                    foreach (var field in filteredFields)
                    {
                        Console.WriteLine("{0} = {1}", field.Title, item[field.Title] ?? "");
                    }
                    Console.WriteLine("--------------------------------------------------------------------------------");
                }
            }
}

Code Result






Comments

Popular posts from this blog

Azure - Manage Blob Storage - Part #7 - Add Metadata to an Existing Container using C#

Azure - Manage Blob Storage - Part #5 - Create Folder Structure and upload a file to folder using an Existing Container using C#

Algorithm - Breadth First Search(BFS) - Python(3)