0

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
1

1 Answer 1

0

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);
5
  • its giving me System.NullReferenceException on var fileContentAsBytes = aLibraryItem.File.OpenBinary(); Commented Feb 23, 2016 at 11:11
  • May be your aLibraryItem is null. Can you update your question with code Commented Feb 23, 2016 at 11:18
  • i just used your code now , i did nothing before that to show you Commented Feb 23, 2016 at 11:24
  • oppppps ............!!! Change library name and Item Id Commented Feb 23, 2016 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 . Commented Feb 23, 2016 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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