Frage

I have a Sharepoint 2010 Document Library with a range of subfolders and some files within each of them. The problem is, right now every subfolder has the same Modified timestamp, and I want each folder's Modified timestamp to be set to the latest Modified timestamp of the files inside.

My knowledge about the Object Model is pretty limited so far, and I'm having trouble writing a PowerShell script for this.

Here's my code so far:

$web = Get-SPWeb -Identity http://inside.site.be/Tools
$list = $web.GetList("http://inside.site.be/Tools/site_candidate/")
$folderquery =  New-Object Microsoft.SharePoint.SPQuery 
$foldercamlQuery = 
'<Where>
    <Eq>
        <FieldRef Name="ContentType" />
            <Value Type="text">Folder</Value>
    </Eq>
</Where>'
$folderquery.Query = $foldercamlQuery

$folders = $list.GetItems($folderquery)
foreach($folder in $folders)
{
    if($folder.ItemCount -gt 0){
        $oldest = null
        $filequery = New-Object Microsoft.SharePoint.SPQuery
        $filequery.Folder = $folder
        $files = SP.List.getItems($itemquery)
        foreach ($file in $files){
            if($file["Modified"] -ilt $oldest){$oldest = $file["Modified"]}
        }
        $folder["Modified"] = $oldest
        $folder["Created"] = $oldest
        $folder.Update()
        Write-Host "$folder now has date $oldest"
    } else {
        Write-Warning "$folder['Name'] is empty"
    }
}

I get the feeling I'm using the wrong types, but I've tried typecasting (for example, something like

foreach(Folder $folder in $folders){...}

But that doesn't seem to work. I've found pretty meager results about working with folders and files within in document libraries when searching for answers, maybe you guys can point me in the right direction?

Learning resources are also welcome.

War es hilfreich?

Lösung

Try this:

$web = Get-SPWeb -Identity http://inside.site.be/Tools 
$list = $web.GetList("http://inside.site.be/Tools/site_candidate/")
$folderquery =  New-Object Microsoft.SharePoint.SPQuery  
$foldercamlQuery =  
'<Where> 
    <Eq> 
        <FieldRef Name="ContentType" /> 
        <Value Type="text">Folder</Value> 
    </Eq> 
</Where>' 
$folderquery.Query = $foldercamlQuery 

$folders = $list.GetItems($folderquery) 
foreach($folderItem in $folders) 
{ 
    $folder = $folderItem.Folder
    if($folder.ItemCount -gt 0){ 
        $oldest = $null
        $files = $folder.Files
        foreach ($file in $files){ 
            if($file.Item["Modified"] -gt $oldest){$oldest = $file.Item["Modified"]} 
        } 
        $folderItem["Modified"] = $oldest 
        $folderItem["Created"] = $oldest 
        $folderItem.Update() 
        Write-Host "$folder now has date $oldest" 
    } else { 
        Write-Warning "$folder['Name'] is empty" 
    } 
} 

Andere Tipps

Have you considererd splitting your content into more document libraries and organizing the documents with metadata instead of using folders? You can at the list level query the SPList.LastItemModifiedDate property to get the modified date and time of the item that was last modified in the list. This piece of information is just not available at the folder level.

Looping through the folders and updating their timestamps is a pretty heavy and fragile solution. Moreover, it will also not be always give you true data as there is a lag from an item is updated until the change is synchronized to the folder.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top