3

I am trying to retrieve "Modified Date" and "Created Date" for folders when using GetFolderByServerRelativeUrl function, how can i do it?

I only able to get relativeUrl and folder Name out of it. below is what i did to retrieve the folder. Please help.

FolderCollection folderCollection = rootweb.GetFolderByServerRelativeUrl("/Shared Documents/test2").Folders;
spClientContext.Load(folderCollection);

foreach (Folder folder in folderCollection)
         {

         }
tang fire
  • 133
  • 2
  • 2
  • 8
  • 1
    Try looking at the [`ListItemAllFields`](http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.folder.listitemallfields.aspx) property on the `Folder` objects. I'm not very familiar with the client object model so you might have to do another `Load` to get at it, but it might contain the fields you're after. – Rawling Jun 05 '13 at 08:20
  • @Rawling Thanks for the reply. I am using GetFolderByServerRelativeUrl which pass me Folder instead of ListItem/List. I cant find ListItemAllFields in FolderCollection. Any thoughts where it might be in? – tang fire Jun 06 '13 at 04:14
  • I've put together the code to show you what I mean below. – Rawling Jun 06 '13 at 07:19

2 Answers2

7

By retrieving and accessing the ListItemAllFields property of your Folders, you can access the created and modified dates as follows:

using (ClientContext spClientContext = new ClientContext("http://whatever"))
{
    var rootweb = spClientContext.Web;

    FolderCollection folderCollection =
        rootweb.GetFolderByServerRelativeUrl("/Shared Documents/test2").Folders;

    // Don't just load the folder collection, but the property on each folder too
    spClientContext.Load(folderCollection, fs => fs.Include(f => f.ListItemAllFields));

    // Actually fetch the data
    spClientContext.ExecuteQuery();

    foreach (Folder folder in folderCollection)
    {
        // This property is now populated
        var item = folder.ListItemAllFields;

        // This is where the dates you want are stored
        var created = (DateTime)item["Created"];
        var modified = (DateTime)item["Modified"];
    }
}
Rawling
  • 49,248
  • 7
  • 89
  • 127
  • Thank you for your help and Sorry for the late reply. Do you mind to tell me any addition of assemblies you are using except of Microsoft.Sharepoint.Client?
    I am learning all these stuff using Vista client VM with VS 2010 to remote server. And VS keep prompting there is no extension of ListItemAllFields and method for it. I also got an warning prompt for "fs.Include(f => f.ListItemAllFields)); " == [cannot convert lambda expression to type 'system.linq.expressions.expression
    – tang fire Jun 10 '13 at 07:02
  • @tangfire I also have a reference to `Microsoft.SharePoint.Client.Runtime`; does adding this help? – Rawling Jun 10 '13 at 07:21
  • Nope. I added the reference but I still cant get the method. My Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll is located at "C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.SharePoint.Client.dll". Am I pointing to the right folder? – tang fire Jun 10 '13 at 08:32
  • Office12 corresponds to SharePoint 2007, which doesn't have these methods, so I'm afraid my answer won't be much help to you :( – Rawling Jun 10 '13 at 08:38
  • U mean that is 2007 file?? Hmm I forget where I got it from but apparently I been using for months and since no issue til now, I did not notice anything weird... Let me get hold of 2010 dll files first. Thanks!!! – tang fire Jun 10 '13 at 08:43
  • Oh if your server is running 2010 or 2013 then yes, update your client DLLs and give it a shot! I just assumed that if you had the 2007 client DLLs then your server would be running 2007 too. – Rawling Jun 10 '13 at 08:45
  • My server is 2010. Didnt use 2007 before. And I cant find those dlls from my server, believed I have grabbed that off from one of the downloaded sample projects online. – tang fire Jun 10 '13 at 08:48
  • Try [here](http://www.microsoft.com/en-us/download/details.aspx?id=21786), I think that's what you need. – Rawling Jun 10 '13 at 08:50
  • Still cant. I downloaded the msi from your link. Installed and Reboot vm, created new project, added in your code and it prompt me regards the method missing. Weird. – tang fire Jun 10 '13 at 09:19
  • I saw this remarks "For SharePoint 2013, they've apparently taken this into consideration and have added a Folder.ListItemAllFields property which would be exactly what I need here, but this property is unavailable to the 2010 CSOM." at http://sharepoint.stackexchange.com/questions/57188/csom-get-listitem-of-folder – tang fire Jun 10 '13 at 09:20
  • Damn, you're right. I thought it would work for at least 2010. Sorry :( – Rawling Jun 10 '13 at 10:06
  • Na. Is ok. Thanks lots for your help =) – tang fire Jun 11 '13 at 02:03
0

I don’t think there is any direct way to get the folder's modified data. But you can get the folder related item first, and then get the item’s modified data.

But you still cannot get the folder related item using JavaScript Object Model. You can try to get the items which content type is folder by caml query, then fetch the item whose related file's server relative url is equal to the folder's server relative url, then you can get the detailed information from the item.

var folder = rootweb.GetFolderByServerRelativeUrl("/Shared Documents/test2");

var query = new SP.CamlQuery();
query.set_folderServerRelativeUrl(“/Shared Documents”);
var items = list.getItems(query);
context.load(items, “Include(Title, FileSystemObjectType, File)”);
context.executeQueryAsync(function(){
    var itemEnum = items.getEnumerator();
    while(itemEnum.moveNext()){
       var item = itemEnum.get_current();
       if(item.get_fileSystemObjectType()==”1” && item.get_file().get_serverRelativeUrl()==”Shared Documents/test2”){
    //do something to get the modified data
}
}
}, function(){})
Douglas
  • 490
  • 3
  • 8
  • Thank you for your reply. Sorry, I read the post from bottom up. I think I will need to ask about the assemblies too. I just added Microsoft.SharePoint and Microsoft.SharePoint.Client.Runtime in but I still didnt get "set_folderServerRelativeUrl" method show in my VS 2010. – tang fire Jun 10 '13 at 07:21