Following PowerShell code iterates through SharePoint List Items and gets the details of the attachments associated with each list item:

$web = Get-SPWeb -Identity http://sp2010:90
$list = $web.Lists["Resources"]
foreach ($item in $list.Items)
{
    $attachmentCollection = $item.Attachments
    $folder = $web.GetFolder($attachmentCollection.UrlPrefix);
    foreach ($file in $folder.Files)
    {
        Write-Host $item.Title
        Write-Host $file.Name
        Write-Host $file.Author
        Write-Host $file.TimeCreated
    }
}

The C# equivalent of the PowerShell code is:

using (SPSite site = new SPSite("http://sp2010:90"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["Resources"];
        foreach (SPListItem item in list.Items)
        {
            SPAttachmentCollection attachmentCollection = item.Attachments;
            SPFolder folder = web.GetFolder(attachmentCollection.UrlPrefix);
            foreach (SPFile file in folder.Files)
            {
                Console.WriteLine(item.Title);
                Console.WriteLine(file.Author);
                Console.WriteLine(file.TimeCreated);
                Console.WriteLine(file.Name);
            }
        }
    }                
}


0 comments

My SharePoint CodePlex Projects

SharePoint 2010 Autocomplete Lookup Field

Export Version History Of SharePoint 2010 List Items to Microsoft Excel.