Answered by:
How to get attachment in ItemAdded event receiver

Question
-
Hi,
I am trying to get filename of attachment in sharepoint library. I must used event receiver after item is added, so I am using code as below:
public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdded(properties); SPListItem listItem = properties.Web.Lists[properties.ListId].GetItemById(properties.ListItemId); if (listItem.Attachments != null) { //string t = listItem.Attachments.UrlPrefix; } }
but in line
if (listItem.Attachments != null)
I am getting exception with message: "Column 'Attachments' does not exist. It may have been deleted by another user."
This event is added to Document Library.
Please help if anybody has such error. Thanx for any info.
Friday, September 13, 2013 9:30 AM
Answers
-
>OK, but what about Sharepoint Library? It seems there is no attachment column or collection ;/
Initially i though you have created custom column in library that's why i said check the column in list. But it seems you are searching for "Attachments" column in library which does not supports. Attachments column is available in list only.
Try below code:
SPFieldUrlValue value = new SPFieldUrlValue(); value.Description = filePath; value.Url = "http://onet.pl"; properties.Listitem["Link"] = value;; properties.Listitem.update();
If still get same problem then try same code on ItemAdded event.
Hemendra:Yesterday is just a memory,Tomorrow we may never see
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Hemendra Agrawal Monday, September 23, 2013 4:45 AM
Friday, September 13, 2013 11:30 AM -
OK, but what about Sharepoint Library? It seems there is no attachment column or collection ;/
Another question, is it possible to set SPFieldUrlValue field in ItemAdding. I can set simple text field but there is a problem with SPFieldUrlValue field.
I use code as below:
SPFieldUrlValue value = new SPFieldUrlValue(); value.Description = filePath; value.Url = "http://onet.pl"; properties.AfterProperties["Link"] = value;
There is no any error but value is not set to that field. Please help.
- Edited by CaMeL023 Friday, September 13, 2013 11:09 AM
- Marked as answer by Hemendra Agrawal Monday, September 23, 2013 4:45 AM
Friday, September 13, 2013 11:09 AM -
Yes, SharePoint Library do not contain attachment. Only List contains, and for SPFieldUrlValue you use
SPFieldUrlValue value = new SPFieldUrlValue(); value.Description = filePath; value.Url = "http://onet.pl"; properties["Link"] = value;
properties.ListItem.Update();
Moderator Note: Never propose your own posts as answers. The "Propose as Answer" function is for people to propose the good answers of other people. It is not for self-proposing.
- Proposed as answer by Rashi_C Friday, September 13, 2013 11:44 AM
- Unproposed as answer by Hemendra Agrawal Friday, September 13, 2013 11:50 AM
- Edited by Hemendra Agrawal Friday, September 13, 2013 11:52 AM Moderator Note added
- Marked as answer by Hemendra Agrawal Monday, September 23, 2013 4:45 AM
Friday, September 13, 2013 11:17 AM
All replies
-
Hello,
Try below code:
if (listItem["Attachments"] != null)
"listItem.Attachments" indicates that you are finding attachment of item not column name. If attachments is your column name then try above code.
Hope it could help
Hemendra:Yesterday is just a memory,Tomorrow we may never see
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Edited by Hemendra Agrawal Friday, September 13, 2013 10:14 AM aaaa
Friday, September 13, 2013 10:13 AM -
Hi, this also doesn't work ;/ I checked fields in item object and I don't see such field: "Attachments".
Anybody can help with this issue?
Friday, September 13, 2013 10:38 AM -
Have you created attachments column in library? If no then create it.
Otherwise try to add new column with different name in library and see the result.
If you want to read file from library then try below link code:
http://williamvanstrien.blogspot.in/2011/10/read-content-of-uploaded-file-within.html
Hemendra:Yesterday is just a memory,Tomorrow we may never see
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Edited by Hemendra Agrawal Friday, September 13, 2013 11:12 AM link added
Friday, September 13, 2013 10:52 AM -
Hi, I have used the below code to get the attachment url in a field. Hope it may help you.
public override void ItemAttachmentAdded(SPItemEventProperties properties)
{
string attachmentURL = HttpUtility.UrlPathEncode(properties.ListItem.Attachments.UrlPrefix + properties.ListItem.Attachments[0]);
properties.ListItem["AttachmentURL"] = attachmentURL;
properties.ListItem.Update();
}- Proposed as answer by Rashi_C Friday, September 13, 2013 11:44 AM
- Unproposed as answer by Hemendra Agrawal Friday, September 13, 2013 11:50 AM
Friday, September 13, 2013 10:56 AM -
OK, but what about Sharepoint Library? It seems there is no attachment column or collection ;/
Another question, is it possible to set SPFieldUrlValue field in ItemAdding. I can set simple text field but there is a problem with SPFieldUrlValue field.
I use code as below:
SPFieldUrlValue value = new SPFieldUrlValue(); value.Description = filePath; value.Url = "http://onet.pl"; properties.AfterProperties["Link"] = value;
There is no any error but value is not set to that field. Please help.
- Edited by CaMeL023 Friday, September 13, 2013 11:09 AM
- Marked as answer by Hemendra Agrawal Monday, September 23, 2013 4:45 AM
Friday, September 13, 2013 11:09 AM -
Yes, SharePoint Library do not contain attachment. Only List contains, and for SPFieldUrlValue you use
SPFieldUrlValue value = new SPFieldUrlValue(); value.Description = filePath; value.Url = "http://onet.pl"; properties["Link"] = value;
properties.ListItem.Update();
Moderator Note: Never propose your own posts as answers. The "Propose as Answer" function is for people to propose the good answers of other people. It is not for self-proposing.
- Proposed as answer by Rashi_C Friday, September 13, 2013 11:44 AM
- Unproposed as answer by Hemendra Agrawal Friday, September 13, 2013 11:50 AM
- Edited by Hemendra Agrawal Friday, September 13, 2013 11:52 AM Moderator Note added
- Marked as answer by Hemendra Agrawal Monday, September 23, 2013 4:45 AM
Friday, September 13, 2013 11:17 AM -
>OK, but what about Sharepoint Library? It seems there is no attachment column or collection ;/
Initially i though you have created custom column in library that's why i said check the column in list. But it seems you are searching for "Attachments" column in library which does not supports. Attachments column is available in list only.
Try below code:
SPFieldUrlValue value = new SPFieldUrlValue(); value.Description = filePath; value.Url = "http://onet.pl"; properties.Listitem["Link"] = value;; properties.Listitem.update();
If still get same problem then try same code on ItemAdded event.
Hemendra:Yesterday is just a memory,Tomorrow we may never see
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Hemendra Agrawal Monday, September 23, 2013 4:45 AM
Friday, September 13, 2013 11:30 AM -
Ok, Hemendra. So I think "Propose as Answer should be disabled for our own post like u have done for "Vote", as many others are doing this, might be they also not aware of it.
Moderator Note: Never propose your own posts as answers. The "Propose as Answer" function is for people to propose the good answers of other people. It is not for self-proposing.
Friday, September 13, 2013 11:59 AM