5

I have a list named "Discussions List". I want to bring all columns from the list.

I want to know how to do it SharePoint Client Object Model.

Jigar
  • 544
  • 1
  • 8
  • 28
Roy Justin
  • 653
  • 3
  • 10
  • 23

2 Answers2

12

OK. Found the solution. Answer Here using a list I'm getting by title, but will work with any method:

// Get your ClientContext for your site  in 'clientContext'

SP.List oList = clientContext.Web.Lists.GetByTitle("List Title Here"); 
SP.FieldCollection fieldColl = oList.Fields;
clientContext.Load(fieldColl);
clientContext.ExecuteQuery();

foreach (SP.Field fieldTemp in fieldColl)
{
    MessageBox.Show(fieldTemp.InternalName.ToString()); //I used MessageBox to show, but you can do whatever you like with it here.
}
Brad Werth
  • 17,411
  • 10
  • 63
  • 88
Paul B
  • 121
  • 4
  • I was getting the "Invalid usage of query execution" error as I wanted to perform standard Linq operation on the Field Collection (rather than Linq to SharePoint). So I simply used `var fields = fieldColl.ToList();` to decouple it from the server if you will. – PeterX Mar 18 '13 at 00:57
10

Bingo. You're going to love this. Thank goodness for generics and Linq!

// return all rows and (selected) fields of a list--fields are included dynamically
private Dictionary<string,Dictionary<string,object>> getListData( ClientContext ctx )
{
    Log.LogMessage( "Fetching {0}{1}", ctx.Url, ListName );
    var list = ctx.Web.Lists.GetByTitle( ListName );

    // fetch the fields from this list
    FieldCollection fields = list.Fields;
    ctx.Load( fields );
    ctx.ExecuteQuery();

    // dynamically build a list of fields to get from this list
    var columns = new List<string> { "ID" }; // always include the ID field
    foreach( var f in fields )
    {
        // Log.LogMessage( "\t\t{0}: {1} of type {2}", f.Title, f.InternalName, f.FieldTypeKind );
        if( f.InternalName.StartsWith( "_" ) || f.InternalName.StartsWith( "ows" ) ) continue;  // skip these
        if( f.FieldTypeKind == FieldType.Text ) // get Text fields only... but you can get other types too by uncommenting below
                // || f.FieldTypeKind == FieldType.Counter
                // || f.FieldTypeKind == FieldType.User
                // || f.FieldTypeKind == FieldType.Integer
                // || f.FieldTypeKind == FieldType.Number
                // || f.FieldTypeKind == FieldType.DateTime
                // || f.FieldTypeKind == FieldType.Lookup
                // || f.FieldTypeKind == FieldType.Computed
                // || f.FieldTypeKind == FieldType.Boolean )
        {
            columns.Add( f.InternalName );
        }
    }

    // build the include expression of which fields to fetch
    List<Expression<Func<ListItemCollection, object>>> allIncludes = new List<Expression<Func<ListItemCollection, object>>>();
    foreach( var c in columns )
    {
        // Log.LogMessage( "Fetching column {0}", c );
        allIncludes.Add( items => items.Include( item => item[ c ] ) );
    }

    // get all the items in the list with the fields
    ListItemCollection listItems = list.GetItems( CamlQuery.CreateAllItemsQuery() );
    ctx.Load( listItems, allIncludes.ToArray() );

    ctx.ExecuteQuery();

    var sd = listItems.ToDictionary( k => k["Title"] as string, v => v.FieldValues );   // FieldValues is a Dictionary<string,object>

    // show the fields
    foreach( var i in sd.Keys )
    {
        Log.LogMessage( "\tItem: {0}", i );
        foreach( var c in columns )
        {
            Log.LogMessage( "\t\t{0}: {1}", c, sd[ i ][ c ] );
        }
    }

    return sd;
}
Brady
  • 101
  • 1
  • 4
  • 2
    I'm getting invalid request in ExecuteQuery(). But if I remove allIncudes it is working fine. Any idea? – NLV Mar 10 '15 at 16:04
  • how to get the duplicate values inside a splistiitem column. am having some 5300 items in my splist in my spo site. i wanna check for duplicate values for my Title field. how to achieve this using CSOM – samolpp2 Apr 27 '18 at 22:35