How to Avoid Item addition when attachment is invalid.

Published May 01 2019 04:02 PM 98 Views
Microsoft
First published on TECHNET on Feb 18, 2015

This post is a contribution from Jaishree Thiyagrajan, an engineer with the SharePoint Developer Support team

When we add a files with name containing only symbols like "([{_$%]}).txt" as an attachment to a list, we will see the error "The file/folder name that contains characters that are not allowed" & the attachment will not be added. But the item will be added.

We need to create a ItemEventReceiver and use ItemAttachmentAdding method to achieve the required functionality

public override void ItemAttachmentAdding(SPItemEventProperties properties)

{

base.ItemAttachmentAdding(properties);

//write your logic to check for valid filename and cancel it out if you do not want to save the attachment.

}

We cannot use ItemAdding event for the above requirement as we would not able to get the attachment name because the ListItem is null.

%3CLINGO-SUB%20id%3D%22lingo-sub-509969%22%20slang%3D%22en-US%22%3EHow%20to%20Avoid%20Item%20addition%20when%20attachment%20is%20invalid.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-509969%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20TECHNET%20on%20Feb%2018%2C%202015%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EThis%20post%20is%20a%20contribution%20from%20Jaishree%20Thiyagrajan%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team%3C%2FP%3E%0A%20%20%3CP%3EWhen%20we%20add%20a%20files%20with%20name%20containing%20only%20symbols%20like%20%22(%5B%7B_%24%25%5D%7D).txt%22%20as%20an%20attachment%20to%20a%20list%2C%20we%20will%20see%20the%20error%20%22The%20file%2Ffolder%20name%20that%20contains%20characters%20that%20are%20not%20allowed%22%20%26amp%3B%20the%20attachment%20will%20not%20be%20added.%20But%20the%20item%20will%20be%20added.%3C%2FP%3E%0A%20%20%3CP%3EWe%20need%20to%20create%20a%20ItemEventReceiver%20and%20use%20ItemAttachmentAdding%20method%20to%20achieve%20the%20required%20functionality%3C%2FP%3E%0A%20%20%3CTABLE%3E%0A%20%20%20%3CTBODY%3E%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CP%3Epublic%20override%20void%20ItemAttachmentAdding(SPItemEventProperties%20properties)%3C%2FP%3E%0A%20%20%20%20%20%3CP%3E%7B%3C%2FP%3E%0A%20%20%20%20%20%3CP%3Ebase.ItemAttachmentAdding(properties)%3B%3C%2FP%3E%0A%20%20%20%20%20%3CP%3E%2F%2Fwrite%20your%20logic%20to%20check%20for%20valid%20filename%20and%20cancel%20it%20out%20if%20you%20do%20not%20want%20to%20save%20the%20attachment.%3C%2FP%3E%0A%20%20%20%20%20%3CP%3E%7D%3C%2FP%3E%0A%20%20%20%20%20%3CP%3E%3C%2FP%3E%0A%20%20%20%20%20%3CP%3E%3C%2FP%3E%0A%20%20%20%20%20%3CP%3E%3C%2FP%3E%0A%20%20%20%20%20%3CP%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%3C%2FTBODY%3E%3C%2FTABLE%3E%0A%20%20%3CP%3EWe%20cannot%20use%20ItemAdding%20event%20for%20the%20above%20requirement%20as%20we%20would%20not%20able%20to%20get%20the%20attachment%20name%20because%20the%20ListItem%20is%20null.%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-509969%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20TECHNET%20on%20Feb%2018%2C%202015%20This%20post%20is%20a%20contribution%20from%20Jaishree%20Thiyagrajan%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team%20When%20we%20add%20a%20files%20with%20name%20containing%20only%20symbols%20like%20%22(%5B%7B_%24%25%5D%7D).%3C%2FLINGO-TEASER%3E
Version history
Last update:
‎Aug 27 2020 03:36 PM
Updated by: