3

I m using following code sample to upload multiple files (using sharepoint Object Model, no webservice) in a document library, but some times it throws exception hresult: 0x80020009, with error code -2147352567 and error message is empty (empty string) while file is upload successfully to the document library. And mostly it occurs only first time mean it occurs when uploading first document after that all process goes smoothly no exception occurs after first time occured. If i eat that exception it works fine. Can any one help me to trace the problem, I can't understand why it throws exception while file has been uploaded to document library. I want to know What's the actual reason and what should I do to avoid that problem.

Code: .....

SPFolder folder = web.GetFolder(folderUrl);
foreach(.....)
{
folder.Files.Add(folderUrl + "/" + fileName, file.Data, true);
}
Alex
  • 31
  • 1
  • 3
  • Can you post the full formatted code ? – Madhur Ahuja Jan 03 '11 at 07:04
  • using (var site = new SPSite(folderUrl)) using (SPWeb web = site.RootWeb) { SPFolder folder = web.GetFolder(folderUrl); foreach (File file in fileSource.GetFiles()) { folder.Files.Add(folderUrl + "/" + fileName, file.Data, true); } } – Alex Jan 03 '11 at 07:12
  • I have exactly the same problem ax Alex - has anybody found some solution? – Marek Apr 20 '12 at 14:09

1 Answers1

1

try using the code provided below that will help you out

using (SPSite spsite = new SPSite("http://SPS01"))
        {
            using (SPWeb spweb = spsite.OpenWeb())
            {
                spweb.AllowUnsafeUpdates = true;

                SPFolder spfolder = spweb.Folders[Site + "/Shared Documents/"];
                byte[] content = null;
                using (FileStream filestream = new FileStream("C:/Sample.docx", System.IO.FileMode.Open))
                {
                    content = new byte[(int)filestream.Length];
                    filestream.Read(content, 0, (int)filestream.Length);
                    filestream.Close();
                }

                SPFile spfile = spfolder.Files.Add("Sample.docx", content, true);

                //Upload file in subfolder.
                //SPFile spfile = spfolder.SubFolders["Demonstration Folder"].Files.Add("Sample.docx", content, true);   
            spfile.Update(); 
            }
        }
  • 1
    still facing same problem. it throws exception at line: spfolder.Files.Add("Sample.docx", content, true); – Alex Jan 03 '11 at 07:18
  • Microsoft.SharePoint.SPException was caught Message="" Source=Microsoft.SharePoint ErrorCode=-2147352567 NativeErrorMessage=FAILED hr detected (hr = 0x80020009) – Alex Jan 04 '11 at 09:09
  • Check SPFileCollection for validity, my guess is that the exception is being thrown because you're trying to call a method on an invalid object.(spfolder.Files) – Ashutosh Singh-MVP SharePoint Jan 04 '11 at 09:58
  • Ashutosh Singh - I experience the same problem as Alex. How can I "check SPFileCollection for validity"? I've looked on SPFileCollection and related SPFolder object properties (in debugger) - they looks the same on each file upload (except files count and collection in folder of course) but SP throws an exception (native code) on the first upload. – Marek Apr 20 '12 at 14:33