Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading file in sharepoint library throws an unhandled exception(hresult: 0x80020009, errorcode: -2147352567) with empty error message

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);
}
like image 390
Alex Avatar asked Dec 04 '25 06:12

Alex


1 Answers

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(); 
            }
        }
like image 107
Ashutosh Singh-MVP SharePoint Avatar answered Dec 05 '25 22:12

Ashutosh Singh-MVP SharePoint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!