locked
Save Output directly to SharePoint Online RRS feed

  • Question

  • I

    I

    Ihave a script that retrieves some data from its source and outputs this to a csv file (currently locally), with the following commands:

    $datestring = $endDate.ToString("yyyyMMdd")
    $fileName = ("c:\temp\" + $datestring + ".csv") 
    Write-Host (" writing to file {0}" -f $fileName)
    $data | Export-csv $fileName -NoTypeInformation

    It's worth noting this is intended to be a script that runs automatically (either Task Scheduler or Azure Automation). Instead of sending the output to the C: drive, I want to send the output directly to SharePoint Online. I found the following code below from this URL (https://www.c-sharpcorner.com/article/sharepoint-online-automation-o365-sharepoint-online-how-to-upload-your-files-r/) which takes a local file and uploads it to SharePoint Online. I'm not sure how to adapt the code below to output the file directly to SharePoint Online, rather than locally:

    $User = "username@tenant.ac.uk"
    $Password = Read-Host -assecurestring "Please enter your password"
    $SiteURL = "https://tenant.sharepoint.com/sites/sitecollection/subsite"  
    $Folder = "C:\temp\"  
    #Path where you want to Copy  
    $DocLibName = "Test"  
    #Docs library  
    # Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM  
    Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"#  Bind to site collection  
    $Context = New - Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
    $Creds = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo - SecureString $Password - AsPlainText - Force))  
    
    $Context.Credentials = $Creds  
    Retrieve list  
    $List = $Context.Web.Lists.GetByTitle($DocLibName)  
    $Context.Load($List)  
    $Context.ExecuteQuery()  

    I'm aware I'm prompting for a password, meaning my script isn't ready to run in an automated way - but I want to test it first manually and will then adapt the credentials aspect later.

    Does anybody know how I can adapt the two scripts above so I can take $data and output it directly onto SharePoint Online?

    PS. apologies if this is the wrong forum - I'm considering this to be a Powershell query and I thought this forum was the best fit.

    Tuesday, October 22, 2019 2:53 PM

All replies

  • You are asking for an advanced solution and not asking a question about scripting.

    To learn how to use SharePoint through PowerShell and the SharePoint API or module I will move you the the SharePoint forum.


    \_(ツ)_/

    Tuesday, October 22, 2019 3:30 PM
  • I can see this has bene moved to the SharePoint 2013 forum - so just wanted to confirm my question is relating to outputting my data to SharePoint Online
    Thursday, October 24, 2019 8:37 AM
  • You can use Pn to upload files to SharePoint Online. Before trying the code given below just install pnp for powershell using the below command.

    Install-Module SharePointPnPPowerShellOnline

    Use the below code. You can adjust the code for your requirements.

    $datestring = $endDate.ToString("yyyyMMdd")
    $fileName = ("C:\Windows\temp\" + $datestring + ".csv") 
    Write-Host (" writing to file {0}" -f $fileName)
    $data | Export-csv $fileName -NoTypeInformation
    
    Connect-PnPOnline -Url 'https://spdev.sharepoint.com/sites/SPTeam'
    $splib = 'Upload'
    Add-PnPFile -Path $fileName -Folder $splib
    Remove-Item $fileName
    You can find many ways to connect to your site here (as far as your automation is concerned).

    I have tried the above code and this works.

    Thanks



    Please remember to up-vote & mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    • Edited by HarshalGite Thursday, October 24, 2019 12:08 PM
    Thursday, October 24, 2019 12:06 PM