SOLVED

Using PnP PowerShell to attach a file to an item

%3CLINGO-SUB%20id%3D%22lingo-sub-77789%22%20slang%3D%22en-US%22%3EUsing%20PnP%20PowerShell%20to%20attach%20a%20file%20to%20an%20item%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-77789%22%20slang%3D%22en-US%22%3E%3CP%3ECan%20somepone%20provide%26nbsp%3Ban%20example%20of%20a%20successful%20upload%20of%20a%20file%20as%20an%20attachment%20to%20a%20list%20item%20using%20PnP%20PowerShell%3F%20I%20am%20struggling%20getting%20%24item.attachmentFiles.add()%20to%20work.%20I%20don't%20seem%20to%20be%20able%20to%20build%20a%20correct%20Microsoft.SharePoint.Client.AttachmentCreationInformation%20object.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-80320%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20PowerShell%20to%20attach%20a%20file%20to%20an%20item%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-80320%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%2C%20Manidurai!%20That%20worked!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-78645%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20PowerShell%20to%20attach%20a%20file%20to%20an%20item%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-78645%22%20slang%3D%22en-US%22%3E%3CP%3ETry%20this%20script%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Efunction%20writeItem(%0A%24itemTitle%2C%0A%24itemOther%2C%0A%24attachments%0A)%20%7B%0A%23%20check%20if%20file%20exists%20first%0A%24items%3DAdd-PnPListItem%20-List%20testlist%0A%0A%24newListItem%20%3D%20Set-PnPListItem%20-Identity%20%24Items.Id%20-List%20testlist%20-Values%20%40%7B%22Title%22%20%3D%20%24itemTitle%3B%20%22other%22%20%3D%20%24itemOther%7D%0A%0A%0Afor%20(%24a%3D0%3B%20%24a%20-lt%20%24attachments.length%3B%20%24a%2B%2B)%20%7B%0A%23Write-host%20%22%20%22%20%24attachments%5B%24a%5D%0AwriteAttachment%20-item%20%24items%20-fileWithPath%20%20%24attachments%5B%24a%5D%0A%7D%0A%0A%7D%0A%0Afunction%20writeAttachment(%24item%2C%20%24fileWithPath)%0A%7B%0A%0A%24ctx%3DGet-PnPContext%0A%24memoryStream%20%3D%20New-Object%20IO.FileStream(%24fileWithPath%2C%5BSystem.IO.FileMode%5D%3A%3AOpen)%0A%24fileName%20%3D%20Split-Path%20%24fileWithPath%20-Leaf%0A%24attachInfo%20%3D%20New-Object%20-TypeName%20Microsoft.SharePoint.Client.AttachmentCreationInformation%0A%24attachInfo.FileName%20%3D%20%24fileName%0A%24attachInfo.ContentStream%20%3D%20%24memoryStream%0A%24attFile%20%3D%20%24item.attachmentFiles.add(%24attachInfo)%0A%24ctx.load(%24attFile)%0A%24ctx.ExecuteQuery()%0A%7D%0A%0AConnect-PnPOnline%20-url%20https%3A%2F%2Ftenantname.sharepoint.com%2Fsites%2Fcontosobeta%0A%0A%24Att%3D%40(%22C%3A%5Ccopyfile.csv%22%2C%22C%3A%5CAllSubsitegrouppermission.csv%22%2C%22C%3A%5CAllUserWithLicenseType.csv%22)%0A%0AwriteItem%20-itemTitle%20%22test6789%22%20-itemOther%20%22efgh%22%20-attachments%20%24Att%20%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-77940%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20PowerShell%20to%20attach%20a%20file%20to%20an%20item%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-77940%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20function%20accepts%20a%20couple%20of%20string%20fields%20and%20an%20array%20of%20strings%20that%20list%20the%20full%20path%20to%20the%20files.%20The%20files%20are%20located%20locally.%20They%20are%20just%20small%2C%20but%20not%26nbsp%3Bempty%2C%26nbsp%3Btxt%20files.%20(ex%3A%20c%3A%5Ctest%5Crow1-att1.txt).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%2300008b%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3Efunction%3C%2FFONT%3E%20%3CFONT%20color%3D%22%238a2be2%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EwriteItem%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E(%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24itemTitle%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%2C%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24itemOther%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%2C%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24attachments%3C%2FFONT%3E%3C%2FP%3E%3CP%3E)%20%7B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23006400%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%23%20check%20if%20file%20exists%20first%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24newListItem%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EAdd-PnPListItem%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23000080%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E-List%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24spoList%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23000080%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E-Values%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%20%40%7B%3C%2FFONT%3E%3CFONT%20color%3D%22%238b0000%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%22Title%22%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24itemTitle%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3B%20%3C%2FFONT%3E%3CFONT%20color%3D%22%238b0000%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%22other%22%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24itemOther%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%7D%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%2300008b%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3Efor%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%20(%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24a%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%3CFONT%20color%3D%22%23800080%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E0%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3B%20%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24a%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E-lt%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24attachments%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E.%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3Elength%3B%20%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24a%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%2B%2B%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E)%20%7B%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23006400%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%23Write-host%20%22%20%22%20%24attachments%5B%24a%5D%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EwriteAttachment%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23000080%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E-item%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24newListItem%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23000080%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E-fileWithPath%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24attachments%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%5B%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24a%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%5D%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAll%20works%20until%20it%20calls%20this%20function%20that%20actually%20writes%20the%20attachment%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%2300008b%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3Efunction%3C%2FFONT%3E%20%3CFONT%20color%3D%22%238a2be2%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EwriteAttachment%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E(%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24item%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%2C%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24fileWithPath%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E)%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24memoryStream%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3ENew-Object%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23000080%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E-TypeName%3C%2FFONT%3E%20%3CFONT%20color%3D%22%238a2be2%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3ESystem.IO.MemoryStream%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%20(%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%5B%3C%2FFONT%3E%3CFONT%20color%3D%22%23008080%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3ESystem.IO.File%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%5D%3A%3A%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EReadAllBytes(%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24fileWithPath%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E))%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24fileName%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3ESplit-Path%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24fileWithPath%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23000080%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E-Leaf%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24attachInfo%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%20%3CFONT%20color%3D%22%230000ff%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3ENew-Object%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23000080%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E-TypeName%3C%2FFONT%3E%20%3CFONT%20color%3D%22%238a2be2%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EMicrosoft.SharePoint.Client.AttachmentCreationInformation%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24attachInfo%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E.%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EFileName%20%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24fileName%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24attachInfo%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E.%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EContentStream%20%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24MemoryStream%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24attFile%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%3D%3C%2FFONT%3E%20%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24item%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E.%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EattachmentFiles%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E.%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3Eadd(%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24attachInfo%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E)%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24siteCtx%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E.%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3Eload(%3C%2FFONT%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24attFile%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E)%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23ff4500%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E%24siteCtx%3C%2FFONT%3E%3CFONT%20color%3D%22%23a9a9a9%22%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3E.%3C%2FFONT%3E%3CFONT%20face%3D%22Lucida%20Console%22%20size%3D%221%22%3EExecuteQuery()%3C%2FFONT%3E%20%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-77794%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20PnP%20PowerShell%20to%20attach%20a%20file%20to%20an%20item%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-77794%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7835%22%20target%3D%22_blank%22%3E%40Scott%20Oxendine%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20should%20work%20(%3CA%20href%3D%22https%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F177832%2Fattaching-a-file-to-a-list-item-using-csom%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F177832%2Fattaching-a-file-to-a-list-item-using-csom%3C%2FA%3E%3A(%3C%2Fimg%3E%3C%2FP%3E%3CPRE%3Evar%20attInfo%20%3D%20new%20AttachmentCreationInformation()%3B%0AattInfo.FileName%20%3D%20StringFile.DestinationFileName%3B%0AattInfo.ContentStream%20%3D%20new%20MemoryStream(System.IO.File.ReadAllBytes(StringFile.FileAtDestinationPath))%3B%0AAttachment%20att%20%3D%20item.AttachmentFiles.Add(attInfo)%3B%0Actx.Load(att)%3B%0Actx.ExecuteQuery()%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20know%20this%20is%20the%20C%23%20equivalent%20but%20it%20shouldn't%20be%20too%20difficult%20to%20do%20this%20in%20Powershell%20the%20same%20way%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAre%20you%20able%20to%20share%20your%20code%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%20where%20does%20your%20attachment%20live%3F%20It%20need%20to%20exist%20on%20a%20local%20drive.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Can somepone provide an example of a successful upload of a file as an attachment to a list item using PnP PowerShell? I am struggling getting $item.attachmentFiles.add() to work. I don't seem to be able to build a correct Microsoft.SharePoint.Client.AttachmentCreationInformation object.

4 Replies
Highlighted

 

Hi @Scott Oxendine,

 

This should work (https://sharepoint.stackexchange.com/questions/177832/attaching-a-file-to-a-list-item-using-csom:(

var attInfo = new AttachmentCreationInformation();
attInfo.FileName = StringFile.DestinationFileName;
attInfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(StringFile.FileAtDestinationPath));
Attachment att = item.AttachmentFiles.Add(attInfo);
ctx.Load(att);
ctx.ExecuteQuery();

 

I know this is the C# equivalent but it shouldn't be too difficult to do this in Powershell the same way?

 

Are you able to share your code?

 

Also where does your attachment live? It need to exist on a local drive.

Highlighted

This function accepts a couple of string fields and an array of strings that list the full path to the files. The files are located locally. They are just small, but not empty, txt files. (ex: c:\test\row1-att1.txt).

 

function writeItem(

$itemTitle,

$itemOther,

$attachments

) {

# check if file exists first

$newListItem = Add-PnPListItem -List $spoList -Values @{"Title" = $itemTitle; "other" = $itemOther}

for ($a=0; $a -lt $attachments.length; $a++) {

#Write-host " " $attachments[$a]

writeAttachment -item $newListItem -fileWithPath $attachments[$a]

}

 

All works until it calls this function that actually writes the attachment:

 

function writeAttachment($item, $fileWithPath)

{

$memoryStream = New-Object -TypeName System.IO.MemoryStream ([System.IO.File]::ReadAllBytes($fileWithPath))

$fileName = Split-Path $fileWithPath -Leaf

$attachInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation

$attachInfo.FileName = $fileName

$attachInfo.ContentStream = $MemoryStream

$attFile = $item.attachmentFiles.add($attachInfo)

$siteCtx.load($attFile)

$siteCtx.ExecuteQuery()

}

 

Best Response confirmed by Scott Oxendine (Occasional Contributor)
Solution

Try this script

 

function writeItem(
$itemTitle,
$itemOther,
$attachments
) {
# check if file exists first
$items=Add-PnPListItem -List testlist

$newListItem = Set-PnPListItem -Identity $Items.Id -List testlist -Values @{"Title" = $itemTitle; "other" = $itemOther}


for ($a=0; $a -lt $attachments.length; $a++) {
#Write-host " " $attachments[$a]
writeAttachment -item $items -fileWithPath  $attachments[$a]
}

}

function writeAttachment($item, $fileWithPath)
{

$ctx=Get-PnPContext
$memoryStream = New-Object IO.FileStream($fileWithPath,[System.IO.FileMode]::Open)
$fileName = Split-Path $fileWithPath -Leaf
$attachInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
$attachInfo.FileName = $fileName
$attachInfo.ContentStream = $memoryStream
$attFile = $item.attachmentFiles.add($attachInfo)
$ctx.load($attFile)
$ctx.ExecuteQuery()
}

Connect-PnPOnline -url https://tenantname.sharepoint.com/sites/contosobeta

$Att=@("C:\copyfile.csv","C:\AllSubsitegrouppermission.csv","C:\AllUserWithLicenseType.csv")

writeItem -itemTitle "test6789" -itemOther "efgh" -attachments $Att 
Highlighted

Thank you, Manidurai! That worked!