Add same attachment to List

Hi 

"Overwrite existing files" option available in library. But it is not there in list.

I tried to attach same file to a list item .but , i am getting.

Failed to get value of the "Attachments" column from the "Attachments" field type control.  See details in log. Exception message: A file with the name already exists."

i tried -->Enabling versioning .But no luck.

i tried -->Javascript also.

$(document).ready(function()
    {
    var form = $('form');
    if ( form.attr('action').indexOf('YOUR 36 character List ID - Without Braces') > -1 )
        {
        var checkbox = $('#ctl00_PlaceHolderMain_UploadDocumentSection_ctl03_OverwriteSingle');
        checkbox.prop('checked',false);
        checkbox.parent().hide();
        var multiple = $('#ctl00_PlaceHolderMain_UploadDocumentSection_ctl03_UploadMultipleLink');
        multiple.hide();
        }
    }
);
instead of this line

checkbox.prop('checked',false); i tried checkbox.removeattri('checked') option also. but no use.

What is the solution.?

 

 



  • Edited by saidireddy.v Tuesday, September 01, 2015 11:39 AM
September 1st, 2015 11:32am

Hello,

What is your sharepoint version?

By design Overwritean list attachment is not supported so you first need to delete then reattach file. See my reply in below thread:

https://social.msdn.microsoft.com/Forums/office/en-US/5c9851d3-50da-4dbc-ac11-b08fa8a55c8e/overwrite-attachmnts-on-moss-programatically?forum=sharepointdevelopmentlegacy

Hope it could help

Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 1:31am

 

Hi hemendra,

I tried what you suggested.I am using below code for deleting first and reattaching. PFB, code for Your reference.

even this code also giving same error.

public override void ItemAttachmentAdded(SPItemEventProperties properties)

{ base.ItemAttachmentAdded(properties); SPListItem item = properties.ListItem; string attachmentUrl = properties.AfterUrl; string filename = Path.GetFileName(attachmentUrl); Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate() { SPSite site = new SPSite("http://jack-7716f30e37:2012"); using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["MyList"]; SPListItem Item = list.Items[item.UniqueId]; SPAttachmentCollection attach = item.Attachments; foreach (string YourfileName in attach) { if (YourfileName == filename)// this is your last attachment name { Item.Attachments.Delete(filename); } } } }); }



September 10th, 2015 1:46am

 

Hi hemendra,

I tried what you suggested.I am using below code for deleting first and reattaching. PFB, code for Your reference.

even this code also giving same error.

public override void ItemAttachmentAdded(SPItemEventProperties properties)

{ base.ItemAttachmentAdded(properties); SPListItem item = properties.ListItem; string attachmentUrl = properties.AfterUrl; string filename = Path.GetFileName(attachmentUrl); Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate() { SPSite site = new SPSite("http://jack-7716f30e37:2012"); using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["MyList"]; SPListItem Item = list.Items[item.UniqueId]; SPAttachmentCollection attach = item.Attachments; foreach (string YourfileName in attach) { if (YourfileName == filename)// this is your last attachment name { Item.Attachments.Delete(filename); } } } }); }



  • Edited by saidireddy.v Thursday, September 10, 2015 6:03 AM
Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 5:44am

Hello,

I think you should try with "ItemUpdating" handler because "ItemAttachmentAdded" occur after file is attached to list item.

OR one more idea is, you can also create one document library to store file previous version of files and attach only latest version of file with list item. Benefit of this approach you wont loos file history and everything will be tracked. Look at below code for this.

http://www.nothingbutsharepoint.com/2011/05/10/versioning-attachments-in-a-sharepoint-list-aspx/

Hope it could help

September 11th, 2015 2:50am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics