i have A list and B document library . in the A list i have attachment field and once user adds an attachment it should go to the B document library . how can i do that . and i want to do it in my event receiver .

using (SPSite SPSite = new SPSite(Convert.ToString(ConfigurationManager.AppSettings["siteURL"])))
                      using (SPWeb SPWeb= SPSite.OpenWeb())

                          SPListItem ITPCItem = ITPCWeb.Lists[properties.ListId].GetItemById(properties.ListItem.ID);}}}

this way i am getting the SPList Item.

enter code here

Get File from Library A

var aLibrary = (SPDocumentLibrary)spWeb.Lists.TryGetList("A");
var aLibraryItem = aLibrary.GetItemById(ItemId);
var fileContentAsBytes = aLibraryItem.File.OpenBinary();

Now move the file in Library B

var bLibrary = (SPDocumentLibrary)spWeb.Lists.TryGetList("B");
var destUrl = bLibrary.RootFolder.Url + "/" + aLibraryItem.File.Name;
SPFile destFile = bLibrary.RootFolder.Files.Add(destUrl, fileContentAsBytes, true);
  • its giving me System.NullReferenceException on var fileContentAsBytes = aLibraryItem.File.OpenBinary(); – Revenant_01 Feb 23 '16 at 11:11
  • May be your aLibraryItem is null. Can you update your question with code – Atish Dipongkor - MVP Feb 23 '16 at 11:18
  • i just used your code now , i did nothing before that to show you – Revenant_01 Feb 23 '16 at 11:24
  • oppppps ............!!! Change library name and Item Id – Atish Dipongkor - MVP Feb 23 '16 at 11:38
  • haha . i did that man i was saying i dint use any logic before for getting attachments so i have nothing to show . – Revenant_01 Feb 23 '16 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.