Custom listview with attachments

June 23rd, 2009 | Posted by Jeffrey Paarhuis in Configuring

A few days ago I started making a custom NewForm.aspx for a list with Sharepoint Designer. I started hiding the default list form webpart and inserted a custom list form. I tweaked this the way I wanted and it worked like charm. But, with testing I found out that I couldn’t upload any attachments. When I clicked the “Attach File” button nothing happens.

I debugged the javascript that was behind the “Attach File” button. There I found the following piece of code:

function ShowPartAttachment()
{
 if (document.getElementById("part1")==null ||
 typeof(document.getElementById("part1"))=="undefined")
 {
 var L_FormMissingPart1_Text= "This form was customized not working with attachement.";
 alert(L_FormMissingPart1_Text);
 return;
 }
 document.getElementById("part1").style.display="none";
 document.getElementById("partAttachment").style.display="block";
 GetAttachElement(FileuploadString+FileUploadIndex).focus();
}

You can see that it tries to hide part1 and shows partAttachment. In my source I found both spans, but the part1 span was holding the hidden listview webpart that was off course already hidden. This is just another bug in Sharepoint Designer.

I started searching the internet for a fix to this problem and came along a post that refers to the following hotfix for sharepoint designer http://support.microsoft.com/kb/960311 and WSS update http://support.microsoft.com/kb/951695. After applying these fixes, I recreated the custom listview. You can also manually edit an existing custom listview by applying a few lines of code that is given here http://support.microsoft.com/default.aspx?scid=kb;en-us;953271&sd=rss&spid=11373, but I recommend deleting and recreating the listview.

This however didn’t solve anything. When I looked a bit closer at the code I saw that the “part1” span was in place and it theoratically should work. After spending some hours to solve this problem I tried to delete the hidden listview, cause I thought it might interfere, and it did! It’s quite obvious because two spans with the id “part1” are on the page and it hides the first one.

We are working with Sharepoint and one typical Sharepoint issue is that when one problem is solved the next pops up. And indeed this is the case. The form worked fine when I did go to AllItems.aspx, but it now throws an error when clicking “New” in the list overview saying “Invalid page URL” and some users getting “An unexpected error has occurred”. The solution is fairly simple: just don’t delete the List Form Web Part, but I did already =|. So I copied the List Form Web Part from a backup back to the site. If you ran into the same situation but don’t have a backup, don’t bother inserting a new List Form Web Part but just copy it from your EditForm.aspx and doing some small modifications. Here are the instructions for doing this: http://blogs.msdn.com/dszabo/archive/2007/02/20/custom-list-newform-aspx-ruined-in-wss-3-0.aspx

Now when I was back where I started I just moved the custom Listview to the top by dragging and dropping in Sharepoint Designer making it the first webpart on the screen. This did solve allmost everything, I could open the new form normally and upload attachments to it.

However as I said before, when one problem is solved the next pops up. Now, when I fill the form and save it I get a brand new error:

“Failed to get value of the “Attachments” column from the “Attachments” field type control. See details in log. Exception message: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)”

The attachments of the two webparts are conflicting so I made the compiler convince that the hidden List Form isn’t a ListForm by edit the following line in the NewForm.aspx in sharepoint designer:

<TemplateName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">ListForm</TemplateName>

to

<TemplateName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">NoListForm</TemplateName>

this way the hidden List Form is still there but doesn’t conflict. The form works perfectly fine for me now.

update:

I found out that for the first new item in the list I cannot attach any files, because there is no “attach file” button. For any other new item it worked, but simply not the first one =S

However, there is a solution to this also. Just change the following in the NewForm.aspx and everything will work fine:

<ControlMode xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">Edit</ControlMode>

to

<ControlMode xmlns=”http://schemas.microsoft.com/WebPart/v2/ListForm”>Display</ControlMode>

Another update:

Some readers had a problem which is described as “When I try to add attachments to an existing list entry (Edit form) the file attachment dialog doesn’t close when hitting OK.”or “When i click the button to “Attach File”, I now get to the page where I can attach a file, however, after selecting a file when I click “Ok”, nothing happens and I get an “Error On Page” ”

Now one of my readers came with a solution to this problem. When you click on the “OK” button of the attach dialouge after selecting a file, a javascript action occurs, where it takes your selected file and adds it to a table with ID= “idAttachmentsRow”. Now, if this table does not exist, then you will get a javascript error on the page. The reason this table doesnt exist for you is because you have modified your custom new/edit form in the past and deleted your attachments form field. Without the attachments form field you cannot complete the attachment use case. I think the following code will help you (it is tailored for edit forms, but a few small changes it will work for new forms as well)

var elm = document.getElementById(“idAttachmentsTable”);
if (elm == null || elm.rows.length == 0)
document.getElementById(“idAttachmentsRow”).style.display=’none’;

or

Just make a new page. in the new page, add a custom list form. Look at the code generated for the custom list form. At the bottom, it will have your attachments field that you are probably missing. Copy and paste that attachments field into your problem form and you should be good to go.

Credits go to pete w for these solutions.

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

23 Responses

  • Karen Mibus says:

    Custom listview with attachments:
    Your blog cut your code change for the TemplateName. However, I copied the paragraph, pasted it in Notepad and then I could see the change you made.

    Thank you for posting this solution! You saved my day! 🙂

  • Matthew Crenshaw says:

    I followed your steps above (and placed the Custom List View WebPart to the top (above the hidden webpart). When i click the button to “Attach File”, I now get to the page where I can attach a file, however, after selecting a file when I click “Ok”, nothing happens and I get an “Error On Page” with the following:

    Line: 5775
    Char: 3
    Error: ‘document.getElementById(…)’ is null or not an object
    Code: 0

    I believe it has to do with these lines in the CustomNewForm.aspx file:

    document.getElementById(“onetidPageTitleAreaFrame”).className=”ms-areaseparator”;

    Any thoughts on this issue? Unfortunately I don’t have access to do anymore troubleshooting due to my security.

    • jeffreypaarhuis says:

      Thanks for following this tutorial. Can you check on you’re page source that there is a tag with the id ‘part1’ and ‘partAttachment’. I don’t think it has anything to do with that line you are referring to, unless there is an unclosed tag or semicolon or any other syntax related error. Please check that first.

    • Greg says:

      Matthew did you find any solution? Thanks in advance.

  • Pingback: Solution to Custom SharePoint 3 List Forms Attachments Feature Not Working « The Ramblings of Ben

  • Addis Abebayehu says:

    Is there any idea how to embed the dispform.aspx in outllook? The cleint wants to have a button in dispform.aspx page that opens up outlook and attach or embed the item data or the dispform column fileds in the body of the outlook. The subject field of outlook also be pulled from the cloumn fileds. Any help on this will be appreciated.

  • gwagner9 says:

    Thanks for this blog. I already found out most of it before (so I can confirm your findings) but the last bit (resolving the conflict between the parts) was the real breakthrough. Great work.

    I have one minor thing to add: The last change (changing the ControlMode) only applies to New forms but not to Edit forms!

    However there is another issue (… when one problem is solved the next pops up …). When I try to add attachments to an existing list entry (Edit form) the file attachment dialog doesn’t close when hitting OK. To upload a file I need to press OK and then Cancel. Is this a bug or feature? It allows multiple uploads but it’s quite confusing and certainly not the behaviour I see with the standard forms or the New form.

    Did anyone ever look into this issue?

    • jeffreypaarhuis says:

      Microsoft will say it´s a feature, so it´s a bug. Off course, it´s not the behavior anybody would expect. I did not have this before. Maybe some other readers can tell you more about this problem. Once I have the time for it I will try to reproduce and when you solve it yourself I will be happy to know.

      • gwagner9 says:

        It took a long time but finally I solved the issue described above: I can now make sure the Add Attachments form closes properly.

        The problem is related to a unique ID that has been added by SPD automatically. The issue is described in a Microsoft Knowledgebase article: http://support.microsoft.com/kb/953271 (already cited on this page). SPD adds {generate-id()} to the attachment code. After removing this bit of code manually the form closes properly.

        However, another problem pops up: I can no longer save the content – ‘An unexpected error occurred’! Fortunately, this is described in the same MS article: I had to move the Custom List Form out of the Web part zone.

        Now everything works fine!

  • pete w says:

    “I have one minor thing to add: The last change (changing the ControlMode) only applies to New forms but not to Edit forms!”

    No, it works for both new and edit forms if it is dialed in properly.

    “When I try to add attachments to an existing list entry (Edit form) the file attachment dialog doesn’t close when hitting OK.”
    [When i click the button to “Attach File”, I now get to the page where I can attach a file, however, after selecting a file when I click “Ok”, nothing happens and I get an “Error On Page”]

    You see those two problems are probably related. When you click on the “OK” button of the attach dialouge after selecting a file, a javascript action occurs, where it takes your selected file and adds it to a table with ID= “idAttachmentsRow”. Now, if this table does not exist, then you will get a javascript error on the page. The reason this table doesnt exist for you is because you have modified your custom new/edit form in the past and deleted your attachments form field. Without the attachments form field you cannot complete the attachment use case. I think the following code will help you (it is tailored for edit forms, but a few small changes it will work for new forms as well)

    var elm = document.getElementById(“idAttachmentsTable”);
    if (elm == null || elm.rows.length == 0)
    document.getElementById(“idAttachmentsRow”).style.display=’none’;

    • pete w says:

      well, it looks like I cannot paste the code on this forum, the HTML code has been stripped out of my post above.

      Just make a new page. in the new page, add a custom list form. Look at the code generated for the custom list form. At the bottom, it will have your attachments field that you are probably missing. Copy and paste that attachments field into your problem form and you should be good to go.

      • Sean says:

        This was a HUGE help to me, but it took me a few tries to realized that the stuff you copy out of a working form is everything contained in the “” tags. i.e. you need the whole row.

  • Pingback: Solution to Custom SharePoint 3 List Forms Attachments Feature Not Working – Benjamin Wong’s Posterous « The Ramblings of Ben

  • Pavel says:

    You saved me hours of time! Thank you!

  • Mehdi says:

    Hello,

    Post tres interessant qui m’a aidé à traiter ce problème spécifique des formulaires personnalisés qui plantent avec le GUID pour les pièces jointes!

    Merci 🙂

  • Scott says:

    This is a great post, however I am experiencing a problem. When I change the TemplateName from ListForm to NoListForm, I am not longer able to save changes to my item. I click save it looks like it’s going through the process, but when I reopen the item the changes were not saved. Am I missing something?

    • jeffreypaarhuis says:

      This is a long time ago for me. If I got the time, I would reproduce this, but don’t get your hopes up. Maybe you can tell me which version of SharePoint you are using. And are you sure you are changing the correct ListForm TemplateName?

      • Scott says:

        We are using SharePoint 2010 Standard. I only found one TemplateName tag. I’ve also posted my problem in the SharePoint Technologies forum. I understand if you don’t have time. Thanks for at least taking the time to respond to me. I really appreciate it!

  • IM says:

    I just did this and it worked for attachment
    NoListForm

    Thanks jeffrey

  • Geetha says:

    Thanks a ton!! that was a wonderful workaround. Has been breaking my head to solve this. Awesome post!!



Leave a Reply

%d bloggers like this: