Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I just dont understand, I need to move some files from one document library, to a document library in a subsite.

But the filescollection its always zero, but there are files there.

What am I missing?

using (SPSite oSPSite = new SPSite(SourceUrl))
                {
                    using (SPWeb oSPWeb = oSPSite.RootWeb)
                    {
                        #region Get existing website documents
                            SPFolder oFolder = oSPWeb.GetFolder(NameDocLibrary);
                            SPList docLibraryToDelete = oSPWeb.Lists[NameDocLibrary];
                            SPFileCollection collFile = oFolder.Files;

                            List<SPFile> listFiles = new List<SPFile>(collFile.Count);
                            foreach (SPFile oFile in collFile)
                            {
                                listFiles.Add(oFile);
                            }
                            Guid ID = new Guid();

                        #endregion


                        #region Move Document Library
                        using (SPSite oSPSiteArchive = new SPSite(SourceUrl))
                        {
                            using (SPWeb oSPWebArchive = oSPSite.OpenWeb("Archive"))
                            {
                                SPList newDocLibrary =oSPWebArchive.Lists.TryGetList(NameDocLibrary);
                                if (newDocLibrary == null)
                                {
                                    ID = oSPWebArchive.Lists.Add(NameDocLibrary, NameDocLibrary, SPListTemplateType.DocumentLibrary);
                                }
                                else
                                {
                                    ID = oSPWebArchive.Lists[NameDocLibrary].ID;
                                }

                                foreach (SPFile moveFile in listFiles)
                                {
                                    moveFile.MoveTo(oSPWebArchive.Url + "/" + NameDocLibrary + "/" + moveFile.Name, true);
                                }
                                oFolder.Delete();
                                oFolder.Update();
                                docLibraryToDelete.Delete();
                                //docLibraryToDelete.Update();
                            }
                        } 
share|improve this question
    
do you have access to a debugger to use break points? –  Zork Mar 21 '12 at 14:11
    
yes I always debug, there are 2 files, one gif and one png, and this line: SPFileCollection collFile = oFolder.Files; is always return item count to 0. –  Esteban V Mar 21 '12 at 16:06

2 Answers 2

up vote 2 down vote accepted

Try using the full address for the GetFolder function call (http://Server/Site/Library) to make sure it is pulling from the right place. I believe this particular function does not actually error on you, so you might not have successfully pulled the folder at all.

share|improve this answer
    
with the full url it works, with the library name only it doesnt, even if is in the context, I also changed it a little bit and i call the Spfolder.exist method first, apparently the SpFolder is never set to null, even if the parameter in the constructor is wrong. –  Esteban V Mar 22 '12 at 12:40

Are the document files at the root of the document library or are they all in subfolders?

If they are all in subfolders, then getting folder object at the root of the document library will return 0 for the number of files, since it does not directly contain any files, only folders. You will have to go into each subfolder by using the SubFolder property of the oFolder object.

share|improve this answer
    
the files are at the root of the document library, thats why I dont understand it –  Esteban V Mar 21 '12 at 14:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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