Automatic Sharepoint Document Uploading

The place of my employment has me working on various Sharepoint projects recently. The task was to obtain sensor data from a cell phone enabled sensor array in a remote clock tower and then display the data on the on a Sharepoint site for users to access. The data came at a rate of one hour. While not extremely often, it was too often to update the Sharepoint site manually every time a new set of data was received.

There was no short cut solution to uploading files to the Sharepoint site, however like most things with computers if you spend enough time learning about your situation often you can find the solution. The process was documented infrequently and incompletely on the sources that I used, so I decided to write this article for anyone who may need to complete this task.

There are a few parts to accomplish this task, these are the things required to make this happen:

- Batch File containing the command to run the PowerShell script.
- The PowerShell Script that does the Uploading
- Task Scheduler to run the batch file on a regular basis
- Active Directory account to handle the uploading
- The account on the Sharepoint site that is tied to the AD account
- The file that you want to upload

Batch File

This is the portion which is called by the task scheduler which in turn initiates the PowerShell script. All configuration for the upload of the file will take place here. Simply edit the batch file within Notepad.

POWERSHELL –file “location of PowerShell script” –file “location of file to upload” –url “address of sharepoint site” –libpath “library to upload to (Eg: Shared Documents)” –sourceaction “keep” –user "domain/uploader account" –password “password for uploader account”

This is the basic syntax of operating the PowerShell script; replace the parts in italic to customize the upload as required for your situation.

Powershell Script

This is the actual script that will do the uploading to the sharepoint site running this script in PowerShell using the correct syntax will place a file on the desired sharepoint site and within the desired library. The PowerShell script will employ a user which was specified in the batch file (above) which created in an active directory account (below). All files will appear in sharepoint as coming from this user. Keep in mind that changing the password to the user account doing the uploading will require a reconfiguration of all the dependent instances of this program. The PowerShell script has been written by Microsoft and is available here.

Task Scheduler

This is the part that will handle the timing of the upload. This should call the batch file to run the upload script. To set the task scheduler to run at a specified time, see the instructions below.

To launch the task scheduler go to Start > All Programs > Accessories > System Tools > Task Scheduler.

On the left hand side select task scheduler library, a list of the current scheduled tasks will appear. On the right hand side of the screen select Create Task, as shown below.

Create Task Scheduler Task

This will bring up the create task window. From here enter in a name for the task that is descriptive of what the task does and what files it is uploading. It also may be helpful to enter some info in the description area of the window. Next you will want to select run whether the user is logged on or not, and then run with highest privileges. Next hit the triggers tab.

Create Scheduled Task Window

The triggers tab will allow you to set the schedule on which the task runs. Hit new to create a new trigger. Keep in mind that there can be multiple triggers which start the task for different situations that may be required for what you are trying to accomplish.

To run the task every hour select a daily task and then under advanced settings select repeat task every 1 hour. Increase the duration to indefinitely to accomplish your goal.

Create Scheduled Task Trigger

After hitting okay and then adding more triggers as required, move onto the actions tab and select new action.

New Scheduled Task Action

At this screen select start a program and then and find the batch file that was mentioned in the above sections using the browse button. You can also add an argument in the “add arguments” section. Keep in mind that adding arguments will affect the batch file and not the actual sharepoint upload PowerShell script.

Other Details

You will need to create a new user in active directory to allow the script to upload the files to the Sharepoint site. Best practice security says that you should never use an domain administrator account if it is not required. Create the new user in the active directory organizational unit of your choice and and give the user domain user permissions, which will be sufficient for what you are try to accomplish.

On the sharepoint site you are uploading the documents to you will need to add the Active Directory uploader account as a contributing member to the sharepoint site itself. On the site you need to log in as a farm administrator. Now go to Site Actions and hit Site Permissions > Site Members.

Add the Active Directory uploader account to this list by clicking NEW > Add User and then typing the user name into the text box. Hit the person with the check mark button to confirm the user from AD.

Now you have created a Automatic Sharepoint Uploader!

                                                        T3rr0rByte13@hobbyware.org

                                                                                                     ©2020 All Rights Reserved. All contents of this site are copyright to Hobbyware.org