Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camlquery returns user local datetime instead of UTC datetime #309

Open
kevinlaucw opened this issue Nov 14, 2022 · 0 comments
Open

Camlquery returns user local datetime instead of UTC datetime #309

kevinlaucw opened this issue Nov 14, 2022 · 0 comments
Labels

Comments

@kevinlaucw
Copy link

kevinlaucw commented Nov 14, 2022

Hi,

Camlquery returns user local datetime instead of UTC datetime when retrieving a datetime (date only format) field from SharePoint.

SharePoint field name: my_date
Date and Time format: Date only

PHP 8.1.10

Camlquery returns user local datetime instead of UTC datetime

$view_xml = "";
$view_xml = $view_xml."<View>";
$view_xml = $view_xml."  <Query>";
$view_xml = $view_xml."    <Where>";
$view_xml = $view_xml."		 <Eq><FieldRef Name='ID' /><Value Type='Counter'>".$id."</Value></Eq>";
$view_xml = $view_xml."    </Where>";
$view_xml = $view_xml."  </Query>";
$view_xml = $view_xml."</View>";

$qry = new CamlQuery();
$qry->ViewXml = $view_xml;
$list = $client->getWeb()->getLists()->getByTitle($list_title);
$items = $list->getItems($qry);
$client->executeQuery();
foreach ($items as $index => $item) {
	echo $item->getProperty('my_date');
}

getItemById returns UTC datetime

$list = $client->getWeb()->getLists()->getByTitle($list_title);
$item = $list->getItemById($id);
$client->load($item);
$client->executeQuery();
echo $item->getProperty('my_date');

I have tested both scenarios in C# and they both returned UTC datetime:

using (var context = new ClientContext(list_url))
{
    var securePassword = new SecureString();

    foreach (char c in SharepointPassword)
        securePassword.AppendChar(c);

    context.Credentials = new SharePointOnlineCredentials(SharepointLogin, securePassword);

    int sid = 29;
    List targetList = context.Web.Lists.GetByTitle(list_title);
    ListItem targetListItem = targetList.GetItemById(sid);
    context.Load(targetListItem);
    context.ExecuteQuery();
    Console.WriteLine("Retrieved item is: {0}", targetListItem["my_date"]);

    CamlQuery query = new CamlQuery();
    query.ViewXml =
        "<View>" +
        "   <Query>" +
        "      <Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + sid + "</Value></Eq></Where>" +
        "   </Query>" +
        "</View>";

    ListItemCollection collListItem = targetList.GetItems(query);
    context.Load(collListItem);
    context.ExecuteQuery();

    foreach (ListItem myListItem in collListItem)
        Console.WriteLine("Retrieved item is: {0}", myListItem["my_date"]);
}

Please advice. Thank you.

@kevinlaucw kevinlaucw changed the title getItemById returns UTC datetime instead of user local datetime Camlquery returns user local datetime instead of UTC datetime Nov 15, 2022
@vgrem vgrem added the question label Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants