Skip to content

Need documentation for New-MgDriveItem #136

Closed

Description

Trying to upload files to OneDrive Useing New-MgDriveItem -DriveId "" -Name "test.csv" -FileMimeType "text/csv" -ContentInputFile "C:\example.csv" and receive following error

The property 'content' does not exist on type 'oneDrive.item'. Make sure to only use property names that are
defined by the type.
AB#7438

Activity

  1. added this to the 1.0 GA milestone on Mar 17, 2020
  2. darrelmiller commented on Mar 17, 2020

    @darrelmiller
    Contributor

    Unfortunately this is one of those commands that is a little "different" and the autogeneration fails to do the right thing. It will need some love.

  3. darrelmiller commented on Aug 12, 2020

    @darrelmiller
    Contributor

    Related to #54

  4. Spence10873 commented on Apr 11, 2022

    @Spence10873

    I'm trying to upload a CSV to OneDrive using New-MgDriveItem -DriveId "b!lHY...ZZ" -Name "test.csv" -ContentInputFile "C:\Temp\test.csv" and I'm seeing the error:
    image

  5. vandeyv commented on Oct 4, 2022

    @vandeyv

    same issue here, is there a solution or workaround?

  6. joshtransient commented on Nov 15, 2022

    @joshtransient

    Workaround for small files under 4MB:

    Invoke-GraphRequest -Method PUT -Uri '/v1.0/drives/{drive-id}/items/root:/{name-of-file.ext}:/content' -InputFilePath c:\path\to\name-of-file.ext
    

    For a SPO site's default document library:

    Invoke-GraphRequest -Method PUT -Uri '/v1.0/sites/{site-id}/drive/items/root:/{name-of-file.ext}:/content' -InputFilePath c:\path\to\name-of-file.ext
    

    Large upload session is a different story. Maybe after 2.5 years, finally time to show these cmdlets "some love"?

  7. peombwa commented on Dec 21, 2022

    @peombwa
    Member

    Uploading new/existing files to OneDrive is already supported via Set-MgDriveItemContent:

    Set-MgDriveItemContent -DriveId $DriveId -DriveItemId "root:/myFile.txt:" -InFile "PathToFile.txt"

    See #433.

    As for /sites/{site-id}/drive/items/root:/{name-of-file.ext}:/content, the path is currently missing in the OpenAPI document used to generate the module. I've opened an issue in the metadata repo for a fix - microsoftgraph/msgraph-metadata#238.

  8. robinmalik commented on Jan 16, 2023

    @robinmalik

    Two things to report

    1. Failure: Set-MgDriveItemContent : Entity only allows writes with a JSON Content-Type header.
      ... when using:
    $Site = Get-MGSite -SiteId "mydomain.sharepoint.com:/sites/sitetest-20210825"
    $DefaultDrive = Get-MgSiteDefaultDrive -SiteId $Site.id
    Set-MgDriveItemContent -DriveId $DefaultDrive.Id -DriveItemId "root:/myfile.txt" -InFile "C:\Users\Robin\Downloads\upload.txt"

    Though I could absolutely be doing something wrong here. I've failed to find anything online to help with this, so clutching at straws.

    1. The page for New-MgDriveItem is so big that it basically hung my browser for at least 30 seconds 😶

    I'm using Microsoft.Graph.Files version 1.20.

  9. robinmalik commented on Jan 16, 2023

    @robinmalik

    Ah, it was a missing :
    ❌ -DriveItemId "root:/myfile.txt"
    ✅ -DriveItemId "root:/myfile.txt:"

    Super easy to miss. FWIW, for a time I was confused by the parameter name (-DriveItemId as it'd suggest needing the id of something in existence) but I appreciate the difficulty in naming things when it comes to determining file names, file paths, and complete paths.

    [DriveItemId <String>]: key: id of driveItem on the docs doesn't aid understanding. Examples would be nice too 🙏🏼

  10. JeremyTBradshaw commented on Mar 27, 2023

    @JeremyTBradshaw

    robinmalik Peter Ombwa (@peombwa) Hi guys. I don't suppose you could share some layman's terms level tips for using the large/resumable upload sessions? I find New-MgDriveItemUploadSession, but nothing else that obviously goes with it.

    I really can't tell if Set-MgDriveItemContent is meant for use with New-MgDriveItemUploadSession, or if they are each their own entire thing. The parameters for New-MgDriveItemUploadSession have me wishfully thinking that it is a one-stop-shop cmdlet and that I can provide my item to the -Item parameter.

    Any guidance will be much appreciated.

  11. mikew3432 commented on Jun 11, 2023

    @mikew3432

    JeremyTBradshaw are we meant to use Invoke-WebRequest? this is how I see it done here... (ref. the last several lines of code)

  12. JeremyTBradshaw commented on Jun 11, 2023

    @JeremyTBradshaw

    @mikewoodd3432 thanks for that and yes, exactly that. I just so happened to be looking into this this week and came to realize that's the way to 'feed' the data. That link will come in handy for reference for sure, thanks again.

  13. mikew3432 commented on Jun 14, 2023

    @mikew3432

    I got a file uploaded to SharePoint JeremyTBradshaw (throughput was slow....)

    I used New-MgDriveItemChild to create an empty item in a drive folder (New-MgDriveItem would be similar)
    # tell the method I want a file item, by providing an empty IMicrosoftGraphFile structure for the -File parameter
    $IMicrosoftGraphFile = @{
    hashes = ""
    mimeType = ""
    }
    # create the new file item
    $newitem = New-MgDriveItemChild -DriveId $drive.Id -DriveItemId $drivefolder.Id -Name $uploadFilePath -File $IMicrosoftGraphFile

    Then used $driveUploadSession as per the Invoke-WebRequest method as per web link I mentioned previously.
    # create the upload session
    $driveUploadSession = New-MgDriveItemUploadSession -DriveId $drive.Id -DriveItemId $newitem.Id

  14. JeremyTBradshaw commented on Jun 14, 2023

    @JeremyTBradshaw

    Thanks @mikewoodd3432 . Glad to see live human confirmation that it works. I plan to use this stuff in this script: https://github.com/JeremyTBradshaw/PowerShell/blob/main/Export-MSGraphMailbox.ps1

    Just waiting for the right level of business in my day to day to really tackle it, hopefully soon.

  15. mozziemozz commented on Nov 30, 2023

    @mozziemozz

    That's what worked for me. Courtesy of GitHub Copilot. 😊

    # Connect to Microsoft Graph
    Connect-MgGraph -TenantId $tenantId -ClientId $appId -Scopes "Files.ReadWrite.All"
    
    # SharePoint Drive Id (Document Library)
    $driveId = ""
    
    # File to upload
    $filePath = "C:\Temp\test.csv"
    $fileProperties = Get-ChildItem -Path $filePath
    
    # Read the file content as a byte array
    $fileContent = [System.IO.File]::ReadAllBytes($filePath)
    
    # Destination file name
    $destinationName = "$($fileProperties.BaseName)-$(Get-Date -Format "yyyy-MM-dd HH-mm-ss")$($fileProperties.Extension)"
    
    # Upload the file to SharePoint
    Invoke-MgGraphRequest -Method PUT -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/root:/IntuneHashes/$destinationName`:/content" -Body $fileContent -ContentType "application/octet-stream"
  16. thanhgp-oucru commented on Nov 28, 2024

    @thanhgp-oucru

    That's what worked for me. Courtesy of GitHub Copilot. 😊

    # Connect to Microsoft Graph
    Connect-MgGraph -TenantId $tenantId -ClientId $appId -Scopes "Files.ReadWrite.All"
    
    # SharePoint Drive Id (Document Library)
    $driveId = ""
    
    # File to upload
    $filePath = "C:\Temp\test.csv"
    $fileProperties = Get-ChildItem -Path $filePath
    
    # Read the file content as a byte array
    $fileContent = [System.IO.File]::ReadAllBytes($filePath)
    
    # Destination file name
    $destinationName = "$($fileProperties.BaseName)-$(Get-Date -Format "yyyy-MM-dd HH-mm-ss")$($fileProperties.Extension)"
    
    # Upload the file to SharePoint
    Invoke-MgGraphRequest -Method PUT -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/root:/IntuneHashes/$destinationName`:/content" -Body $fileContent -ContentType "application/octet-stream"

    Thanks a lot for your script. It works for my case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions