0

I need to upload multiple documents into SharePoint periodically. So, the senario is like this: - documents will be dropped in a shared folder in network. The number is not know. It could be thousands at a time. - Once documents are dropped in the shared folder, they need to be uploaded to SharePoint. - Upload should be done on a regular bases. At the moment they suggest to use a Windows Scheduler which will run every 30 minutes.

First question is: is Windows Scheduler an efficient way? How about a triggering event in the folder itself? Whenever documents are dropped the trigger is fired.

Second question: is powershell a good choice to upload documents periodically?

I got the code below from this site, which I have not customized yet, but I was wondering if this is an efficient way?

Using this code and a windows scheduler, is that a good way? Any ideas and tips are much appreciated as well.

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 
#Function to Upload File 
function UploadAllFilesFromDir($WebURL, $DocLibName, $FolderPath) 
{ 
#Get the Web & Lists to upload the file 
$site = New-Object Microsoft.SharePoint.SPSite($WebURL) 
$web= $site.OpenWeb() 
#Get the Target Document Library to upload 
$List = $Web.GetFolder($DocLibName) 
#Get the Files from Local Folder 
$Files = Get-ChildItem $FolderPath 
#You can filter files by: -filter “*.pdf” 
#upload the files 
foreach ($File in $Files) { 
#Get the Contents of the file to FileStream 
$stream = (Get-Item $file.FullName).OpenRead() 
# Set Metadata Hashtable For the file - OPTIONAL 
$Metadata = @{"Country" = "United States"; "Domain" = "Sales"
} 
#upload the file 
$uploaded = $List.Files.Add($File.Name, $stream,$Metadata, $TRUE) 
#dispose FileStream Object $stream.Dispose() 
} 

#Dispose the site object $site.Dispose() 
} 
0

In my opinion PowerShell is most suitable here, but you can always create console application or even SharePoint Timer Job if you want. Your code sample is generally ok, but best practice is to dispose SPSite, SPWeb and Stream objects explicitly even in PowerShell. Windows Scheduler is good way to trigger script execution on regular basis, but if you want to execute on folder changes, you should see System.IO.FileSystemWatcher class. Here you can see example how to use it in PowerShell. It should be more efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.