Friday, May 04, 2007

This form was customized not working with attachment

Update 31-03-2009

Folks, another update.. it’s fixed!! Get the hotfix for SharePoint Designer here KB960311 and make sure that your SharePoint environment is patched with the infrastructure updates.

Update 09-07-2007

Folks, the solution given below does not work! Also there is no fix for it yet, please look in the following forum http://forums.microsoft.com/TechNet/showpost.aspx?postid=1826359&siteid=17 for more details. I am very sorry if you found this blogpost via Google hoping you get an answer.. :(

Well until recently I did not know how to fix this (and I wasn't the only one if you check out this forum post on TechNet and these comments on a post of Jan Tielens's blog) until I received an alert from the forum that there was a new post with the solution. And here is the solution :

This is the code you need for attachments to work:

<SharePoint:AttachmentUpload ControlMode="Edit" runat="server"
ID="fileupload{$Pos}">
</SharePoint:AttachmentUpload>
<SharePoint:AttachmentsField ControlMode="Edit"
FieldName="Attachments" runat="server" Visible="true">
</SharePoint:AttachmentsField>

This will display the input field and the buttons as well as what you're trying to upload.

1) First make sure your field and test case works (Edit form) without the attachments.

2) What I did was put the attachments in a separate xsl template:

<xsl:template name="attachments">
<xsl:param name="Pos" />
  <tr><td>
    <SharePoint:AttachmentUpload ControlMode="Edit" runat="server"
ID="fileupload{$Pos}">
    </SharePoint:AttachmentUpload>
    <SharePoint:AttachmentsField ControlMode="Edit"
FieldName="Attachments" runat="server" Visible="true">
    </SharePoint:AttachmentsField>
  </td></tr>
</xsl:template>

3) and then I changed the dvt_1 template:

<xsl:template name="dvt_1">
  <xsl:variable name="dvt_StyleName">ListForm</xsl:variable>
  <xsl:variable name="Rows"
select="/dsQueryResponse/Rows/Row"/>
  <table border="0" width="100%" id="part1{$noop}">
    <xsl:call-template name="dvt_1.body">
      <xsl:with-param name="Rows" select="$Rows"/>
    </xsl:call-template>
  </table>
  <table border="0" width="100%">
    <xsl:call-template name="attachments">
      <xsl:with-param name="Pos" select="concat('_',
position())" />
    </xsl:call-template>
  </table>
</xsl:template>

A few things that are important to remember:

The JavaScript in SharePoint will look for  the object with id part1 in the final HTML and hide the object (display:hidden css style) and the object id partAttachment and display that object. The partAttachment object (span) is automatically generated when you introduce the SharePoint:AttachmentUpload code. The part1 id however, you need to introduce yourself.

Now, if you don't want that anything hides, just put in an empty element somewhere with the id part1{$noop}, otherwise, just use my example for the dvt_1 template (it's neater).

The $noop is to fool SharePoint Designer since it will introduce {generate-id()} after the id if you don't specify anything there (to guarantee unique identifiers or something, I don't know) and thus it would fail the javascript since it can't find id=part1, but there will be a part1KLV532.

You can specify the noop parameter to be empty in the beginning of your xsl stylesheet:

<xsl:param name=noop></xsl:param>

Evi

So thanks Evi! :)

16 comments:

jeckerlin said...

Hi, I am receiving a javascript error when uploading an attachment. once I click OK to upload an error is thrown no line 5775, CHar: 3, with the error description of Object Required. Do you have an idea what I did wrong?

Thanks in advance

jeckerlin said...

The error seems to be generating from the okAttach() function of the form.js include.

The error occurs when the following line is run:
document.getElementById("idAttachmentsRow").style.display="";

With this line commented out I do not get an error but the attachment does not save when the item is saved.

Marcelou said...

I'm having exactly the same error message and the attachements are not being saved. Did anyone find a workaround?

Anonymous said...

Hi,

Can u tell step by step to fix this issue.Should i create a new list and add this code?

Thanks in advance.

Robin Meuré said...

@Jerckerlin&Marcelou,

I don't have any idea since I did not have time yet to test this solution out. I only saw the solution posted and wanted to share this with you

@anonymous,

you must edit this for every customized newform/editform/dispform of any list. You can (only?) do this in Sharepoint Designer.

Anonymous said...

I have created custom list for my project. I wann to add fileupload in the same custom list page.

Thanks

Anonymous said...

This is not a working solution. Please don't waste your time on this.

farid dehar said...

hola a todos

Escribo en castellano

-**** I Write en spanish ***-

para resolver el tema de adjuntar ficheros en un formulario personalizado lo he reglado de esta forma

1- tengo una lista con las seguientes columnas :
- Titulo incidencia
- Descripcion incidencia
- Tipo incidencia
- Technic assignado
- Dias de resolución
- Acciones realizadas


En el formulario de creacion NewForm.aspx necesito ver solo los campos

- Descripcion incidencia
- Tipo incidencia
en el formulario de edicion necesito esditar los campos :


- Technic assignado
- Dias de resolución
- Acciones realizadas

y visualizar los campos

- Titulo incidencia
- Descripcion incidencia
- Tipo incidencia



Los campos a ocultar en el NewForm le he puesto con la opcion " OCULTO"
de manera a tener en el formulario NewForm.aspx solo los campos que necesito

En el formulario EditForm.aspx he creado un formulario personalizado y he puesto el webpart " ListFormPart " en IsVisible=False IsVisible = false


y he selecionado el control WebPartPages:DataFormWebPart con la opcion " IncludeHidden = True "


De esta manera en el NewForm.aspx me funciona el " Attach File " y muestro solo los campos que necesito

y en el EditForm.aspx muestro todo los campos

Anonymous said...

Robin,
What do you tell your clients about this issue? It worked in 2003 and now it doesn't. Puts everyone in a sticky situation. I'd like to hear the stance everyone is taking.
Thanks.

Robin Meuré said...

Hi,

well I certainly always try to stay away from modifying the .aspx's to I always recommend the customer NOT TO DO any customizations on lists.

But I thought there was a working solution available on the msdn forum ;)

omenof said...

An unsupported solution to this problem has been found. It is located at http://cid-6d5649bcab6a7f93.spaces.live.com/blog/cns!6D5649BCAB6A7F93!130.entry

Kevin van Gelderen said...

Hi all,

Maybe a bit late but I have found a solution to this problem. I also struggled with uploading attachments to a document library. In my case I wanted users to upload an attachment together with their post on a sharepoint blog. I have googled for hours and finally I found this article(in German):

Site: http://www.konfabulieren.com/2007/
08/23/howto-add-add-attachment-
functionality-to-a-moss2007-blog/

For the ones out there who don't understand German:

1. Navigate to the page you want to be able to upload attachments
2. Click "edit page" under Site Actions
3. Add a content editor webpart where ever you want to have it on the page
4. Copy the source code as shown on the website I mentioned above
5. Paste the source code in the source editor of the CEWP.

The only thing I am still struggling with is when I turn on anonymous access. An anonymous user seems to be unable to upload an attachment. An exception will be thrown: "The security of this page is invalid".

I hope this helps you guys.

Cheers

Robin Meuré said...

Hi Kevin,

thanks for the update! Really appreciate it!

About the anonymous user thing, why do you want anonymous users to upload things in the first place? :)

Kevin van Gelderen said...

Hi Robin,

A client of mine wants to have some sort of guestbook for the leave of the managing director. So I worked that out with the OOTB functionality of the SharePoint Blog. But you are right, it is not very wise to allow anonymous users to upload files ;) Prob. that's the reason why it is not possible :)

Good luck.

Cheers.

Anonymous said...

THIS WAS SOLVED by MS Hotfix
See post

http://support.microsoft.com/kb/960311

Anchal said...

Hi,
i installed this patch on my machine but still getting same java script error "This form was customized not working with attachment". Any idea how to get it resolved?

Thanks in advance
-Anchal