Reputation: 157
I got a custom sharepoint list, with a title column which contains Servernames and i got alot of .csv files that has the same name as the title
Is it somehow possible to upload em to the list item as an attachment with a script? Maybe Powershell? I got over 100 files so it would be a pain if i had to do it all manually.
The File and Title column looks like this:
Title
Server1.domain.com
.csv
Server1.domain.com.csv
Have been looking around for quite a while but havent been able to find anything i could use
Upvotes: 3
Views: 4104
Reputation: 14880
Approach:
SPListItem.Title
SPListItem
.Script:
$w = Get-SPWeb http://List/Location/Url
$l = $w.Lists["TheList"]
foreach ($item in $l.Items)
{
$file = Get-Item "$($item.Title).csv"
$stream = $file.OpenRead()
$buffer = New-Object byte[] $stream.length
[void]$stream.Read($buffer, 0, $stream.Length)
$item.Attachments.AddNow($file.Name, $buffer)
}
Considerations:
SPWeb
objectEdit
Two mistakes in first attempt:
SPAttachmentCollection.Add
expects a byte array.SPAttachmentCollection.AddNow
should be used to add the attachment directly (without update of the SPListItem
)Updated the code...
Upvotes: 4