Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

copy files from document library to another doc lib in the same site

$
0
0

hi,

 i am  trying to  copy the files from  one  doc lib to another through code. but it throws me  object ref. error.

can anyone help me what i was doing wrong.

i am having 2 folders within source doc lib and  target doc  lib DOES NOT contain folders.so i need to iterate through two folders within the source doclib and copy the same into target doclib

below is my code:

 try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                  {
                      using (SPSite _CurrentSelectedSite = new SPSite(strcurrentSelectedURLSiteNameList))
                      {
                          using (SPWeb _CurrentSelectedWeb = _CurrentSelectedSite.OpenWeb())
                          {
                              _CurrentSelectedWeb.AllowUnsafeUpdates = true;

                              string mmcurrSelURL = string.Empty;

                              SPList  splistdocdemo1 = _CurrentSelectedWeb.Lists.TryGetList("Demo1");

                              SPDocumentLibrary spdocdemo1 = splistdocdemo1 as SPDocumentLibrary;

                              SPList splistdocdemo2 = _CurrentSelectedWeb.Lists.TryGetList("Demo2");

                              SPDocumentLibrary spdocdemo2 = splistdocdemo2 as SPDocumentLibrary;

 

 

                              if ((spdocdemo1.Items.Count > 0 && spdocdemo1 != null))
                              {


                                
                                  SPQuery mquery = new SPQuery();
                                  mquery.ViewAttributes = "Scope=\"RecursiveAll\"";
                                  mquery.Query = "<Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Integer\">1</Value></Eq></Where>";

                                  SPListItemCollection spdocdemo1itemcollec = spdocdemo1.GetItems(mquery);

                                  //SPListItemCollection spdocdemo1itemcollec = spdocdemo1.GetItems(objnodeQueryCType);

                                  if (spdocdemo1itemcollec.Count > 0)
                                  {
                                      foreach (SPListItem singleListItem in spdocdemo1itemcollec)
                                      {
                                         
                                              SPFolder doclibfolderTarget = spdocdemo2.RootFolder;  //_CurrentSelectedWeb.GetFolder("Demo2");

                                              doclibfolderTarget.Files.Add(singleListItem.File.Name, singleListItem.File.OpenBinary());
                                          }
                                      }
                                  }
                              }
                          }
                      } 

 


Viewing all articles
Browse latest Browse all 7589

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>