This project has moved and is read-only. For the latest updates, please go here.

Dynamic ViewFields Expression

Jan 30, 2012 at 11:58 AM

Hi!

Does Camlex support building dynamic viewfields expression?

For a more generic approach it would be useful to be able to do something along the lines of:

var fieldIds = new Guid[] {SPBuiltInFieldId.Title, SPBuiltInFieldId.FileRef};
Expression<Func<SPListItem, object[]>> expr = i => fieldIds.Select(g => i[g]).ToArray();
var s = Camlex.Query().ViewFields(expr);

This throws a NonSupportedEpressionTypeException. "Expression type 'Call' is not supported".

Could the desired result be acceived using another approach?

 

Kind regards

/Martin

Jan 30, 2012 at 5:59 PM
Edited Jan 30, 2012 at 5:59 PM

Well, right now it doesn't work this way, unfortunately.

Camlex now supports only two following forms of syntax for ViewFields:

Camlex.Query().ViewFields(x => x["FieldName"]);
Camlex.Query().ViewFields(x => new [] { x["FieldName1"], ..., x["FieldNameN"] });

and nothing more (plus additional boolean flag not important for now).

It could be possible to dynamically create an expression for array constructor, but it wouldn't look sane :-(

In the next version, we will have the situation improved.

Jan 30, 2012 at 7:34 PM

That sounds promising! I am very much looking forward to that.

We have until now been using a pattern utilizing Linq2SP for data acces. However, due to its limitations, we are no moving away from Linq2SP. I have great hopes of creating a mapping framework utilizing Camlex.

Would you be interested in a collaboration, bringing further benefit to the community?

Kind regards

/Martin

Feb 1, 2012 at 8:38 AM

hi Martin,

yes this is useful feature. Will try to make it on this week (can not promise, but will try). If you have some code - you can attach path to the issue http://camlex.codeplex.com/workitem/13990, we will review it and add to the lib.

Vladimir, we don't reverse dynamic methods currently, so it will be one-way translation from caml to c#.

Feb 2, 2012 at 11:03 PM

ok, now upload new version 3.1. It contains several new methods:

public interface IQueryEx : IQuery
{
    // ...
    string ViewFields(IEnumerable<string> titles);
    string ViewFields(IEnumerable<string> titles, bool includeViewFieldsTag);
    string ViewFields(IEnumerable<Guid> ids);
    string ViewFields(IEnumerable<Guid> ids, bool includeViewFieldsTag);
}

it was not possible to make it as Martin suggested:

Expression<Func<SPListItem, object[]>> expr = i => fieldIds.Select(g => i[g]).ToArray();

because here we have lazy expression which contains outer lambda parameter "i". When we try to compile and invoke it in runtime - .net also tries to evaluate call to i[g]. So I just added methods as Vladimir suggested with IEnumerable parameters (I don't think that methods with changeable number of params is really needed for dynamic ViewFields).

Example of the usage:

[Test]
public void test_THAT_dynamic_view_fields_with_fields_ids_expression_IS_translated_sucessfully()
{
    var items = new[] { SPBuiltInFieldId.Title, SPBuiltInFieldId.FileRef };

    string caml =
        Camlex.Query().ViewFields(items);

    string expected = "<FieldRef ID=\"fa564e0f-0c70-4ab9-b863-0177e6ddd247\" /><FieldRef ID=\"94f89715-e097-4e8b-ba79-ea02aa8b7adb\" />";

    Assert.That(caml, Is.EqualTo(expected).Using(new CamlComparer()));
}