Need help with Powershell script to move List Attachment to file share when new item is added to list

I was able to find this script below. As I want to run the script via a workflow and that it would be triggered when a new item is added to the list.

right now the powershell script is dependant on knowing the ID #

I'm giving credit to

Eli Van Eenwyk [MCM]

as it is his script

found here:
https://social.msdn.microsoft.com/Forums/sharepoint/en-US/3f707e4f-ce26-4237-85d6-5ed47caf5a80/powershell-script-to-copy-a-list-attachment-to-a-file?forum=sharepointdevelopmentlegacy&prof=required

[void][reflection.assembly]::Loadwithpartialname("microsoft.sharepoint")

$webUrl = "<your weburl>"    # the url to the site that has the list
$library = "<your library name>"   # Library Name
$tempLocation = "C:\Temp\"     # Path to dump files
$itemId =  1    #I need this be equal to the Current item ID               # Item Id to read attachments from

# Load the Site / Web
$s = new-object Microsoft.SharePoint.SPSite($webUrl)
$w = $s.OpenWeb()

# Grab the list and item that you want to download the attachments from
$l = $w.Lists[$library]
$i = $l.GetItemById($itemId)

# Loop thru each attachment
foreach ($attachment in $i.Attachments)
{
    # Get the attachment
    $file = $w.GetFile($i.Attachments.UrlPrefix + $attachment)
    $bytes = $file.OpenBinary()
   
    # Build the destination path
    $path = $tempLocation + $attachment
   
    Write "Saving $path"
   
    # Download the file to the path
    [System.IO.FileStream] $fs = new-object System.IO.FileStream($path, "OpenOrCreate")
    $fs.Write($bytes, 0 , $bytes.Length)
    $fs.Close()
}

Anyone have any ideas

June 22nd, 2015 4:51pm

Hi Cowboy,

From your description, my understanding is that you want to move list attachment file to share file when item is created.

You could accomplish your requirement via SharePoint Add Event Receiver with C#.

Please refer to this article about how to create Add Event Receiver:

https://msdn.microsoft.com/en-us/library/ff398052.aspx

And please refer to this code below to download your file to share file:

           using (SPSite site = new SPSite("http://sp/sites/sharepoint2013"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    var tempLocation = @"C:\Temp\";

                    var list = web.Lists["myList"];

                    //SPListItem item1 = list.GetItemById(1);

                    foreach (SPListItem item in list.Items)

                    {

                        var url = item.Attachments.UrlPrefix;

                        foreach (var att in item.Attachments)

                        {

                            SPFile file = web.GetFile(url + att);

                            byte[] fileContent = file.OpenBinary();


                            var path = tempLocation + att;


                            FileStream fs = new FileStream(path, FileMode.CreateNew);


                            using (fs)

                            {


                                BinaryWriter bw = new BinaryWriter(fs);


                                bw.Write(fileContent, 0, fileContent.Length);


                                bw.Close();


                            }


                        }

                    }

                }

            }
http://gopinathonline.com/post/2008/10/25/Download-Files-From-SharePoint-Library-To-Local-File-System.aspx

Best Regards,

Vincent Han

Free Windows Admin Tool Kit Click here and download it now
June 24th, 2015 1:41am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics