Renaming a file in SharePoint Online using Powershell….
Wasn’t as simple as I initially thought, you need to make use of MoveTo …
# Call the SharePoint DLL
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
#ASSUMES CONNECTION TO SHAREPOINT ONLINE
# Get reference to the site / library
$web = Get-Web
$list = $web.Lists.GetByTitle('Documents')
# Pull back 5 documents
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml = "
$items = $list.GetItems($camlQuery)
# Foreach file...rename
foreach($file in $items)
$file1 = $file.File
$docName = $file1.name
$newDocName = [String]::Concat($docName, "-Test-")
$filePath = "/Documents/" + $newDocName
# Make use of MoveTo to rename the file...
I’ve uploaded many files to SharePoint 2013 Online over the last couple of years, all these files have been pretty small. These files have been uploaded via PowerShell scripts.
The other day I needed to upload a slightly larger file (more then 2MB) and got the following issue:
The request message is too large. The server does not allow messages that are larger than 2097152 bytes.
Previously I was uploading files in the following way:
$filePath = Get-ChildItem $file
$fileBytes = [System.IO.File]::ReadAllBytes($filePath.FullName)
$fileinfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fileinfo.Content = $fileBytes
$fileinfo.Url = $library.RootFolder.ServerRelativeUrl + "/" + $filePath.Name
$fileinfo.Overwrite = $true
$newFile = $library.RootFolder.Files.Add($fileinfo)
The above function caused issues when the file was larger than 2MB.
Using the method below I was able to workaround this issue:
$CurrentList = $ctx.Web.Lists.GetByTitle("My Library");
$filePath = 'C:\Files\fileToUpload.txt'
$stream = New-Object IO.FileStream $filePath ,'Open'
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($ctx, $CurrentList.RootFolder.ServerRelativeUrl.ToString() + “/” + $filePath.Split(‘\\’), $stream, $true);
More info on the SaveBinaryDirect method:
Recently came across a scenario using SharePoint 2010, Duet, and SAP. The scenario being when submitting data to SAP from SharePoint any Date/Time fields would be stored in SAP using the local time of the user whom submitted the data. However when the data is sent back to SharePoint, the Netweaver layer converts any Date/Time fields to UTC time.
After a little research found some very useful information on MSDN that handles the conversion of UTC time to local time based on the regional settings of the SPWeb.
Two useful methods included in the SPTimeZone class:
Examples of how to implement the methods are available on MSDN
SPTimeZone Class: http://msdn.microsoft.com/en-us/library/ms464064