0

I noticed some differences in values when calling File obj Created and Modified properties from ListItemCollection and FileCollection. Details below:

Below is some old code that fetched files from folder in FileCollection where I initialize required properties:

using (var spClientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, _clientId, _clientSecret))
{
    if (spClientContext != null)
    {
        List list = spClientContext.Web.Lists.GetByTitle("Documents");
        spClientContext.Load(list);
        spClientContext.Load(list.RootFolder);
        spClientContext.Load(list.RootFolder.Folders);
        spClientContext.ExecuteQuery();
        FolderCollection fcol = list.RootFolder.Folders;

        if (fcol != null)
        {
            foreach (Folder f in fcol)
            {
                if (f.Name.Equals(folderName, StringComparison.InvariantCultureIgnoreCase))
                {
                    spClientContext.Load(f.Files,
                                         items => items.Include(
                                             item => item.Name,
                                             item => item.Author,
                                             item => item.ModifiedBy,
                                             item => item.ListItemAllFields["Created"],
                                             item => item.ListItemAllFields["Modified"],
                                             item => item.ListItemAllFields["FileRef"],
                                             item => item.Length));

                    spClientContext.ExecuteQuery();

                    FileCollection fileCol = f.Files;

                    foreach (File file in fileCol)
                    {
                        // Do Stuff
                    }
                }
            }
        }
    }
}

When I output the File Created and Modified date/time values I get the following:

  • Created: 6/17/2021 11:40:28 PM (Correct Date/Time)
  • Modified: 6/17/2021 11:41:12 PM (Correct Date/Time)

Below is my updated code which collects files in ListItemCollection and initializes File properties:

using (ClientContext spClientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, _clientId, _clientSecret))
{
    if (spClientContext != null)
    {

        CamlQuery camlQuery = new CamlQuery();

        camlQuery.FolderServerRelativeUrl = getSiteUrlAbsolutePath(siteUrl) + "/Shared Documents/" + folderPath;

        camlQuery.ViewXml = "<View Scope="RecursiveAll">" +
            "<Query>" +
            "<Where>" +
            "<Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>" +
            "</Where>" +
            "</Query>" +
            "</View>";

        List list = spClientContext.Web.Lists.GetByTitle("Documents");

        ListItemCollection listItems = list.GetItems(camlQuery);

        spClientContext.Load(listItems,
                             items => items.Include(
                                 item => item.DisplayName,
                                 item => item.FileSystemObjectType,
                                 item => item.File,
                                 item => item.File.Name,
                                 item => item.File.Author,
                                 item => item.File.ModifiedBy,
                                 item => item.File.ListItemAllFields["Created"],
                                 item => item.File.ListItemAllFields["Modified"],
                                 item => item.File.ListItemAllFields["FileRef"],
                                 item => item.File.Length));

        spClientContext.ExecuteQuery();

        if (listItems != null && listItems.Count > 0)
        {
            foreach (ListItem item in listItems)
            {
                if (item.FileSystemObjectType.Equals(FileSystemObjectType.File))
                {
                    // Do Stuff
                }
            }
        }
    }
}   

When I output the File Created and Modified date/time values I get the following:

  • Created: 6/18/2021 6:40:28 AM (Incorrect Date/Time)
  • Modified: 6/18/2021 6:41:12 AM (Incorrect Date/Time)

All the other File properties in the new code appearing to be correct. From what I can tell in the new code Created and Modified are outputting UTC and not recognizing time zone setting in SharePoint site settings? Or am I initializing the File properties in the wrong spot?

Thanks for any feedback and help!

0

I posted my solution on StackOverflow which outlines two UTC to Local solutions:

https://stackoverflow.com/questions/68030618/file-object-created-and-modified-property-values-vary-from-listitemcollection-an

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.