This project has moved. For the latest updates, please go here.

OffsetDays

Aug 26, 2011 at 7:03 AM
Edited Aug 26, 2011 at 7:22 AM

Hi, guys!

First of all, I'd like to say big thank you for such a great contribution and really professional work! Camlex.Net is purely marvelous :)

But, I have a little problem. I would like to generate a CAML query with a fragment, similar to the following:

<Today OffsetDays="-7" /> 

Is that possible to perform this with Camlex.Net? I expect some extension method for Camlex.Today, but there is no any, as Camlex.Today is not more than a string constant...

P.S. I had sifted through the Camlex.Net sources recently, and didn't find any sign of "OffsetDays" attribute, at least in unit tests module :-(

Coordinator
Aug 27, 2011 at 8:26 PM
Edited Aug 27, 2011 at 8:28 PM

hi omlin,

yes, in the current version this feature is not supported - I will think about most organic way to add this feature. There are several workarounds which you can use now. In order to get "Today" element in the CAML you should use string-based syntax (it is already supported):

var caml =
    Camlex.Query()
        .Where(x => x["Modified"] == (DataTypes.DateTime)Camlex.Today).ToString();
Console.WriteLine(caml.ToString()

It will generate the following CAML:

<Where>
  <Eq>
    <FieldRef Name="Modified" />
    <Value Type="DateTime">
      <Today />
    </Value>
  </Eq>
</Where>

In order to add OffsetDays attribute to the Today element, the simplest workaround is just replace it:

var caml =
    Camlex.Query()
        .Where(x => x["Modified"] == (DataTypes.DateTime)Camlex.Today).ToString();
caml = caml.Replace("<Today />", "<Today OffsetDays=\"-7\">");
Console.WriteLine(caml);

Another workaround is to use LINQ 2 XML and modify output XML:

var caml =
    Camlex.Query()
        .Where(x => x["Modified"] == (DataTypes.DateTime)Camlex.Today).ToCaml(false);
caml.Descendants().First(x => x.Name == "Today").SetAttributeValue("OffsetDays", -7);
Console.WriteLine(caml[0].ToString());

Both workarounds will give the following CAML:

<Where>
  <Eq>
    <FieldRef Name="Modified" />
    <Value Type="DateTime">
      <Today OffsetDays="-7">
    </Value>
  </Eq>
</Where>

Of course they are not so fancy, but like any workaround it is better than nothing. At least it won't stop you until the feature will be added to the Camlex.

Aug 28, 2011 at 8:53 AM

Thanks, the second one is acceptable. And, looking forward to updates of your great project!