msgraph-sdk-powershell icon indicating copy to clipboard operation
msgraph-sdk-powershell copied to clipboard

Need documentation for New-MgDriveItem

Open mattgrif opened this issue 5 years ago • 3 comments
trafficstars

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

mattgrif avatar Mar 16 '20 14:03 mattgrif

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.

darrelmiller avatar Mar 17 '20 14:03 darrelmiller

Related to #54

darrelmiller avatar Aug 12 '20 18:08 darrelmiller

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

Spence10873 avatar Apr 11 '22 13:04 Spence10873

same issue here, is there a solution or workaround?

vandeyv avatar Oct 04 '22 04:10 vandeyv

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"?

joshtransient avatar Nov 15 '22 17:11 joshtransient

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

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

See https://github.com/microsoftgraph/msgraph-sdk-powershell/pull/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 - https://github.com/microsoftgraph/msgraph-metadata/issues/238.

peombwa avatar Dec 21 '22 17:12 peombwa

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.

robinmalik avatar Jan 16 '23 13:01 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 🙏🏼

robinmalik avatar Jan 16 '23 13:01 robinmalik

@robinmalik @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.

JeremyTBradshaw avatar Mar 27 '23 14:03 JeremyTBradshaw

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

mikew3432 avatar Jun 11 '23 22:06 mikew3432

@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.

JeremyTBradshaw avatar Jun 11 '23 22:06 JeremyTBradshaw

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

mikew3432 avatar Jun 14 '23 05:06 mikew3432

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.

JeremyTBradshaw avatar Jun 14 '23 17:06 JeremyTBradshaw

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"

mozziemozz avatar Nov 30 '23 15:11 mozziemozz