I am trying to create a "Link to a Document" programmatically, similar as explained here: Create a 'Link to a Document' in a SharePoint Shared Document List using PowerShell
Document library is located in SharePoint Online site therefore Client Script Object Model (CSOM) is used (in PowerShell).
But I am running into a permission problem. Part of the solution is to upload an aspx file, which plays the role of a redirect page. And independent from actual content of file, if extension is .aspx, SharePoint Online always rejects the upload!
For better understanding here some code with 2 approaches for uploading file (with exception):
# Create document link
$fileTemplate = "<%@ Assembly Name='{0}' %>\r\n <%@ Register TagPrefix='SharePoint' Namespace='Microsoft.SharePoint.WebControls' Assembly='Microsoft.SharePoint' %>\r\n <%@ Import Namespace='System.IO' %>\r\n <%@ Import Namespace='Microsoft.SharePoint' %>\r\n <%@ Import Namespace='Microsoft.SharePoint.Utilities' %>\r\n <%@ Import Namespace='Microsoft.SharePoint.WebControls' %>\r\n <html>\r\n <head> <meta name='progid' content='SharePoint.Link' /> </head>\r\n <body>\r\n <form id='Form1' runat='server'>\r\n <SharePoint:UrlRedirector id='Redirector1' runat='server' />\r\n </form>\r\n </body>\r\n </html>"
$fileContent = [String]::Format($fileTemplate, "Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
$path = $fileRef.Replace($file.Name, "")
$targetFile = New-Object System.IO.FileInfo($targetPath)
$fileUrl = $path + $targetFile.Name + ".aspx"
$bytes = $encoding.GetBytes($fileContent.ToString())
# Approach 1
$memoryStream = New-Object System.IO.MemoryStream(@(,$bytes))
# Next statement: The remote server returned an error: (401) Unauthorized.
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($context, $fileUrl, $memoryStream, $true)
# Approach 2
$fileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fileCreationInfo.Overwrite = $true
$fileCreationInfo.Content = $bytes
$fileCreationInfo.Url = $fileUrl
$upload = $list.RootFolder.Files.Add($fileCreationInfo)
$context.Load($upload)
# Next statement: Access denied. You do not have permission to perform this action or access this resource.
$context.ExecuteQuery()
If I use similar code to upload file (as in approach 1 or 2) I can verify that it's really depending on file extension - .aspx raises error.
Is there any setting in SharePoint Online (O365) to allow such uploads?
Or is there better way to create "Link to a Document" programmatically without need to upload aspx file?
Thanks
--UPDATE--
I have tried another approach, using WebDAV upload instead (UploadFile of class System.Net.WebClient).
This approach first looked quite promising as I could create corresponding .aspx files manually in File Explorer. But when trying to do same approach programmatically in PowerShell I ran into another wall!
The upload request flooded my server with random numbers and CPU went up to 100%! This must be a "security feature" of Office 365. It is independent from file extension in this case. And in contrary DownLoadFile method worked like a charm (it's not an issue with provided credentials whatsoever - with other method I could upload files as long as file extension is not .aspx).
Time to open a ticket with Office 365 support.