0

I have a caml query and I have a list item in SharePoint list named testb and it is of DateTime type. But I was wondering if I could use a local variable like one I declared testBdate. Can I use testBdate or how to use a variable like this in <FieldRef Name ='' ?

DateTime testBdate = DateTime.Now;

queryMuaj.Query = "<OrderBy>" +
                            "<FieldRef Name ='testb' Ascending = 'TRUE'/>" +
                      "</OrderBy>" +
                      "<Where>" +
                      "<And>" +
                      "<Eq><FieldRef Name='Kompania' /><Value Type='Text'>" + selvalcomp + "</Value></Eq>"+
                      "<And>" +
                            "<Geq>" +
                             "<FieldRef Name='testb' />" +
                                "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + startDateFx + "</Value>" +
                            "</Geq>" +
                            "<Leq>" +
                                "<FieldRef Name='testb' />" +
                                "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + endDatFx + "</Value>" +
                            "</Leq> " +
                          "</And>" +
                          "</And>" +
                       "</Where>";

Thanks

1
  • What are you trying to achieve exactly at a higher level? – Submits Sep 21 '16 at 9:53
5

Name Attribute is Optional Text that provides the internal name of the field that is referenced.

So try to create a new variable that will hold the internal name of your field and set it via String.Format

string FileReferenceInternalFieldName = "internal field name";


queryMuaj.Query =  String.Format("@<OrderBy>" +
                            "<FieldRef Name ='{0}' Ascending = 'TRUE'/>" +
                      "</OrderBy>" +
                      "<Where>" +
                      "<And>" +
                      "<Eq><FieldRef Name='Kompania' /><Value Type='Text'>" + selvalcomp + "</Value></Eq>"+
                      "<And>" +
                            "<Geq>" +
                             "<FieldRef Name='testb' />" +
                                "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + startDateFx + "</Value>" +
                            "</Geq>" +
                            "<Leq>" +
                                "<FieldRef Name='testb' />" +
                                "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + endDatFx + "</Value>" +
                            "</Leq> " +
                          "</And>" +
                          "</And>" +
                       "</Where>", FileReferenceInternalFieldName);

See also Create dynamic caml query in sharepoint

1
DateTime testBdate = DateTime.Now;
string testBdateISO = SPUtility.CreateISO8601DateTimeFromSystemDateTime(testBdate);

And in the CAML:

<FieldRef Name='yourfieldname'/><Value Type='DateTime'>" + testBdateISO + "</Value>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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