Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

Weekend Scripter: Use PowerShell to Upload a New File Version to SharePoint

$
0
0

Summary: Microsoft PowerShell MVP, Niklas Goude, talks about using Windows PowerShell to upload a new version of a file to SharePoint.

Microsoft Scripting Guy, Ed Wilson, is here. Today Niklas Goude is our guest blogger. You can read more from Niklas in his past Hey, Scripting Guy! Blog posts.

Take it away Niklas…

In a previous post, Use PowerShell Cmdlets to Manage SharePoint Document Libraries, we talked about uploading files to a share in SharePoint 2010. Now we'll take this a step further and update a minor or a major version of an existing document.

Here’s a quick recap of the code used to upload the file:

# Add the Snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell

# Retrieve specific Site
$spWeb = Get-SPWeb http://SP01

# Create instance of Folder
$spFolder = $spWeb.GetFolder("Shared Documents")

# Get the file on Disk that we want to upload
$file = Get-Item C:\Documents\MyDoc.docx

# upload the file.
$spFolder.Files.Add("Shared Documents/MyDoc.docx",$file.OpenRead(),$false)

What we’ve done so far is to upload a single document to a document library in SharePoint 2010. The file used in this example is stored on drive C: 

Image of menu

  

The document contains a single line:

Image of menu

 

After we run the Windows PowerShell code, the document gets uploaded to a document library in SharePoint 2010.

Image of menu

 

We can check the versioning for the document by clicking Version History in SharePoint:

Image of menu

 

We’ll see that the version number is set to 0.1:

Image of menu

 

Now, let’s open the file that is stored on drive C and modify it:

Image of menu

Back in Windows PowerShell, we use SPFileCollection to pick up the document we just uploaded. In this example, we are going to filter out the document where the name is equal to MyDoc.docx by using the Where-Object cmdlet.

# Retrieve specific Site
$spWeb = Get-SPWeb http://SP01

# Create instance of Folder
$spFolder = $spWeb.GetFolder("Shared Documents")

# Retrieve a Specific File.
$spFile = $spFolder.Files | Where-Object { $_.Name -eq "MyDoc.docx" }

We pick up the file because we want to use some of its properties for the new version that we want to upload. When we upload a new version of the document, we still use the Add method that is provided by the Microsoft.SharePoint.SPFileCollection, but with a different overload definition. Here’s the definition on MSDN: SPFileCollection.Add method (String, Stream, SPUser, SPUser, DateTime, DateTime).

The parameters we want to use are: urlOfFile, File, CreatedBy, ModifiedBy, TimeCreated, and TimeLastModified.

We can get the following values from the existing document in SharePoint 2010: UrlOfFile, CreatedBy, ModifiedBy, and TimeCreated. We can get File by using Get-Item, and we can get TimeLastModified by simply using Get-Date.

First, let’s get the modified MyDoc.docx from drive C:

$file = Get-Item C:\Documents\MyDoc.docx

Next we run the method:

$newVersion = $spFolder.Files.Add($spFile.Name, $file.OpenRead(), $spFile.Author, $spFile.ModifiedBy, $spFile.TimeCreated, (Get-Date))

If we open the SharePoint document library again, we’ll see that we have a new minor version of our document, .02.

Image of menu

And finally, if we want to add the modified document as a major version, we use the Publish method:

$newVersion.Publish("")

Now we have a new major version, 1.0, instead.

Image of menu

~Niklas

Thank you, Niklas, for taking your time to share your knowledge.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>