Renaming a file in SharePoint Online using PowerShell

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')
$ctx.Load($list)
$ctx.ExecuteQuery()
$list.ItemCount

# Pull back 5 documents
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml = "
      5
  "

$items = $list.GetItems($camlQuery)
$ctx.Load($items)
$ctx.ExecuteQuery()

# Foreach file...rename
foreach($file in $items)
{
	$file1 = $file.File
	$ctx.Load($file1)
	$ctx.ExecuteQuery()
		
	$docName = $file1.name
	$newDocName = [String]::Concat($docName, "-Test-")

	$filePath = "/Documents/" + $newDocName
	
	# Make use of MoveTo to rename the file...
	$file1.MoveTo($filePath, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)
	$ctx.ExecuteQuery()
}



Advertisements

Uploading large files to SharePoint Online

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:

function Add-FileToSpLib($file)
{
	$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)
	

	$ctx.Load($newFile)
	$ctx.ExecuteQuery()
}

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");
	$ctx.Load($CurrentList.RootFolder);
	$ctx.ExecuteQuery();


	$filePath = 'C:\Files\fileToUpload.txt'
	$stream  = New-Object IO.FileStream $filePath ,'Open'
	
	
	[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($ctx, $CurrentList.RootFolder.ServerRelativeUrl.ToString() + “/” + $filePath.Split(‘\\’)[2], $stream, $true);
	$stream111.Close()

More info on the SaveBinaryDirect method:

https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.file.savebinarydirect.aspx

 

Converting Times (UTC to Local) – SharePoint

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