0

I'm a new learner to Powershell and a bit new to SharePoint as well.

I'm learning powershell to create a script that uploads files to a sub-site library inside SharePoint. I managed to get it working and uploading .doc files to my library, however I also want fill in any metadata specified in additional columns with the file at the same time. I'm not using the SharePoint snap-in and instead using the webclient functions. Here is the simple powershell script

# create the Variable Path and Pass the source folder path

$path = "THE FILE"

# create the Variable destination and pass the URL of the SharePoint List
$destination = "SharePoint SubSite"

# Store the current user default credentials in the Variable Credentials
$credentials = [System.Net.CredentialCache]::DefaultCredentials;

# Create the object of the Webclient 

$webclient = New-Object System.Net.WebClient;

$webclient.Credentials = $credentials;

$webclient.UploadFile($destination + “/” + "Filename", “PUT”, TheFile)

This code works but there is a couple things I don't understand.

  1. Why do I have to add + "/" and what does PUT DO? Why doesn't this ($destination, $filename) just work?

  2. The file uploads perfectly with the + / syntax, however I don't know how to use the webclient functions to pass in metadata to specified columns in the document library when uploading the file to the library.

Much Thanks.

0

Put The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server.

"/" is used to create the complete URL for the File location. i.e.library path is http://portal.contoso.com/sites/stuff/Docs then file location will be http://portal.contoso.com/sites/stuff/Docs**/**filename

I am not sure about the updating the properties of file using the WebClient, may be somebody else comment on this.

  • Thanks for the explanation. I'm new to Powershell I just didn't know about nit picky things like that. I just try to read the function and see what parameters it takes. I hope I can figure out how to update columns in the library when uploading files through the webclient. I thought maybe it is parsed through the parameters in the string. Guess not. – Julien Popa-Liesz Sep 22 '14 at 13:44
0

Julien,

If you're new to PowerShell and want to upload documents to SharePoint, I can't recommend the PnP PowerShell cmdlets enough. It was built by top members of the SharePoint Development Community to simplify interacting with SharePoint using PowerShell.

The instructions to install the cmdlets are on the PnP PowerShell home page, but you would use a simple command like this to install the Office 365/SharePoint Online version:

Install-Module SharePointPnPPowerShellOnline

Once installed, you need to connect your PowerShell session using this command (in this example, using Office 365/SharePoint Online):

Connect-PnPOnline -Url https://yoursite.sharepoint.com -UseWebLogin

Once connected, you can upload files and set their metadata in one command:

Add-PnPFile -FileName sample.docx -Folder "Documents" -Values @{Modified="1/1/2016"; Created="1/1/2017"; Editor=23}

Check out the Add-PnPFile cmdlet reference page for more information.

The PnP cmdlets are not necessary, but they make things easier. If you'd prefer using PowerShell directly, let me know and I'll gladly amend my answer.

I hope this helps.

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.