1

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? Commented Sep 21, 2016 at 9:53

2 Answers 2

6

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

2
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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.