How to get datetime field from new item form for CAML Query.

Dec 2, 2014 at 3:22 PM
I have a list that I want to use the SPDisplayRelatedInfo function to show information on the new item form page based on a DateTime field that is picked. I seem to be stuck, because I can't figure out how to get the datetime (including the time) from the form to include in my CAML Query.

Has anybody tackled this? Maybe I am making it too hard?
Coordinator
Dec 2, 2014 at 3:24 PM
SPDisplayRelatedInfo is built to work with dropdowns, not date/time columns. If you want to use it for that purpose, you'll need to adapt it.

M.
Dec 2, 2014 at 4:05 PM
Sorry, I was misleading in my description. I am calling the function when a drop down is selected, but I want to include the selected datetime in a CAML Query. I just for the life of me don't understand how to get the datetime field from the form to include in the CAML Query. I assumed I would just use the jQuery functions like $().find("select:[Title='DateTimeField']").text() but the datetime field have 3 different fields. I was curious if SPServices included a way to grab all of the pieces?
Coordinator
Dec 2, 2014 at 4:11 PM
I'm still not sure what you mean. The lookup into the relatedList is based solely on the value in the dropdown. How are you trying to apply the date/time? If it's variable, then the function doesn't support that, though you can filter the results in the completefunc.

M.
Dec 24, 2014 at 6:41 PM
I was finally able to get the dates from the form to compose the CAML query for SPDisplayRelatedInfo. I found that SharePoint has a functions called g_strDateTimeControlIDs that helps to get dates from a form. See my function below:

function getStartDate(){
var d = new Date(document.getElementById(g_strDateTimeControlIDs["SPStart_x0020_Date"]).value);
var h = document.getElementById(g_strDateTimeControlIDs["SPStart_x0020_Date"]+"Hours").value;
var h2 = 0;
if (h.substring(h.length-2,h.length-1) == "P")
    h2 = parseInt(h.substring(0, h.length-3)) + 12;
else
    h2 = parseInt(h.substring(0, h.length-3));
var m = parseInt(document.getElementById(g_strDateTimeControlIDs["SPStart_x0020_Date"]+"Minutes").value);
var d2 = new Date(d.getFullYear(), d.getMonth(), d.getDate(), h2, m);           
return d2;
}

I am able to use this function to create a CAML Query that filters the related list based on a chosen date. So my SPDisplayRelatedInfo call looks something like this:

$().SPServices.SPDisplayRelatedInfo({
    columnName: "VIN",
    relatedList: "Fleet Vehicle Reservations",
    relatedListColumn: "VIN",
    relatedColumns: ["Title", "Start_x0020_Date", "End_x0020_Date"],
    CAMLQuery: "<Eq><FieldRef Name='Start_x0020_Date' IncludeTimeValue='True'/><Value Type='DateTime'>"+getStartDate()+"</Value></Eq>",
    displayFormat: "list"
});

Note that getStartDate returns a javascript date to make this useful in a CAML Query, it needs to be converted to the correct ISO format.

I am posting this here for future reference and on the slight chance that someone else is doing something as crazy as me, maybe it will help them too.

Nick