2

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.

3
  • What type of permission does the user have that you are uploading the file with?
    – Robban1980
    Commented Nov 7, 2014 at 10:57
  • It's an O365 account (cloud only, not synchronized with on-premise AD), which has Contribution rights to corresponding site. As I had written in my post, with same script file can be uploaded as long as file extension is not .aspx.
    – maeni70
    Commented Nov 8, 2014 at 9:59
  • I have made a test with this account in SharePoint UI, uploading files via normal file upload functionality. When uploading file with .txt extension, it works, but when trying to do the same thing with file having .aspx extension I get following message: "Sorry, you don't have access to this page" And when being logged in with my own organizational O365 account, it works in both cases! This account is site collection administrator. So, there must be special permission level. It's just the question, which permission it is.
    – maeni70
    Commented Nov 8, 2014 at 10:28

1 Answer 1

2

I have added corresponding O365 account as member to site owner group. And now it works. Probably account needs at least permission level Design, even if it's just uploading an aspx page to a document library (independent from its content).

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.