How can i make attachments working in custom SharePoint listform?

If you use an application page as listform you’ll might notice that the button in the ribbon to attach files to the listitem does not work. It throws javascript errors like listform was customized… or something like id attachmentpart is missing or anything else. It might be that it displays nothing. So i thought – what a awesome story for 2014.

As i started my post serie about custom listforms i only thought about writing maybe 2 or 3 posts. This one will be my fifth post about custom listforms using visual studio and the application page way. Maybe i should make a post about the different ways to customize those forms.

But as short introduction let me give you the links to my previous posts:

Part 1: Setting up a solution with a list definition, a list instance and deploy it.

Part 2: Creating a custom list form and connect it with the list definition.

Part 3: Necessary settings for custom listform in Visual Studio

Part 4: Override Save Button in listform

Well, in order to make attachments working in your custom application page respective the listform page we’ll need to add certain parts with special id’s. Let’s see what parts we need.

Important notice: This has only relevance if you customizing also the fields. Means that you don’t use the webpartzone. To make it more clear: If you make the following part hidden or comment it out

<WebPartPages:WebPartZone runat=”server” FrameType=”None” ID=”Main” Title=”loc:Main” ><ZoneTemplate></ZoneTemplate> </WebPartPages:WebPartZone>

Then you have to manage that the attachments working. Let me show you how to do that.

Part 1:  We need part 1 including the idAttachmentRow

[sourcecode language=”csharp”]




[/sourcecode]

In this example you see the span with the id part 1. This part will be hidden if the user clicks on the “Add Attachment” Ribbon button. In this part we normally put also the row with the id “idAttachmentsRow” which displays the attached files.

Part 2: The Attachment Area.

It’s the area in which the user can search for his file and upload it. If he clicks there on OK or Cancel he will see the normal form again.Important part is here the span “partAttachment” which will be searched by the javascript.

[sourcecode language=”csharp”]



[/sourcecode]

That are the most important parts. So at the end i’ll show you the whole aspx markup page.

[sourcecode language=”csharp”]














< %--

–%>





 






















.ms-bodyareaframe {
padding: 8px;
border: none;
}





[/sourcecode]

Hope it helps you.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. In his free time he tries to collect tipps and worthy experience in this blog.

5 Comments

  1. Hi Karsten

    I followed your example and managed to make attaching files work in my custom edit form. However, when I save the form, it display the following message:

    Sorry, there was a problem with File Attached Test (my app)
    The list item was saved, but one or more attachments could not be saved.
    Could not upload file c:\Users\Downloads\test\test.js.

    The file, however, was uploaded and stored in the list item correctly as I was able to retrieve it later.

    Any advise?

    Thank you.

  2. Hi Karsten

    I followed your example here and manage to make attaching file work in my custom edit form. However, when I saved the form with attachment, the following message was displayed:

    Sorry, there was a problem with
    The list item was saved, but one or more attachments could not be saved.
    Could not upload file c:\Users\test\test.js

    The files, however, appeared to be uploaded and stored correctly in the list item as I can retrieve it later.

    My environment is Sharepoint online 2013.

    Any advise?

    Thank you.

  3. Hi,

    Thank you for the post.

    I have exactly the same issue on my form as the previous person who commented the post.

    My form is a new form and is associated to a customed content type. If the form is associated directly to the list it works perfectly, no issue.

    Has anyone solved this?

    Thank you

  4. I had an savehandler in custom form and when I changed
    SaveButton.SaveItem(this.sbApprove.ItemContext, false, string.Empty) to SPContext.Current.ListItem.Update() the error dissapeared.

Leave a Reply to KL Ng Cancel reply

Your email address will not be published.


*