3

I'm trying to save file from network location to SharePoint document library.

This is code of my method:

//parameters: string documentLibraryUrl, string filePath

FileInfo fi = new FileInfo(filePath);
string fileName = fi.Name.Trim();       
using (SPSite oSite = new SPSite(documentLibraryUrl))
{
    using (SPWeb oWeb = oSite.OpenWeb())
    {
        SPFolder myLibrary = oWeb.GetFolder(documentLibraryUrl);

        // Prepare to upload
        using (FileStream fileStream = File.OpenRead(filePath))
        {
            // Upload document

            SPFile spFile = oWeb.GetFile(myLibrary.Url + "/" + fileName);
            filePathInLibrary = spFile.ParentFolder.ServerRelativeUrl + "/" + fileName;
            spFile.SaveBinary(fileStream);      //this line throws exception

            // Commit 
            myLibrary.Update();

        }
    }
}   

When I call SPFile.SaveBinary, I get following DirectoryNotFoundException:

<nativehr>0x80070003</nativehr><nativestack></nativestack>There is no file with URL "http://localhost:9999/Lists/Documents/testfile.pdf" in this Website.

The fileStream variable is not null and has Length > 0. The list exists.

What could be wrong?

So after Amit's reply, I changed the code from line:

spFile.SaveBinary(fileStream);

to this block:

if (spFile.Exists)
{
    //do some logging here
}
else
{
    spFile = myLibrary.Files.Add(myLibrary.Url + "/" + fileName, fileStream);
}

In fact I get exactly the same error message on myLibrary.Files.Add.

What else could be the reason of this error? The code is probably correct, because it worked on different server. I'm thinking about permissions - can it be, that I need extra permissions on SharePoint or on SQL server?
I'm able to add file to the list in UI (in browser) without problems. I'm using Claims Based Authentication. My user (with Windows identity) has Full Control permissions on Web Application.

2
  • For posterity, the line if (spFile.Exists) should not be used / depended upon for an existence check if you use SPWeb.GetFile() as the results of this property are inconsistent at best: blog.mastykarz.nl/inconvenient-spwebgetfilestring Commented Dec 26, 2012 at 18:52
  • I'm getting this error using SPWeb.GetFile and the file definitely exists. Any luck on resolving? Commented Jul 22, 2015 at 17:32

3 Answers 3

3

In above code SPWeb.GetFile don't give you a file as it does not exist (because you are copying a new file)

SPFile.SaveBinary is normally used to update exisitng file. To add a new file you should use SPFolder.Files.Add

1
  • I changed my code as you suggested, but unfortunately, I get the same error. See my update.
    – Tschareck
    Commented Apr 16, 2012 at 11:34
2

please check this link it may helpfull to you

https://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint

thanks

1

Thank you all for your answers. You wouldn't believe how silly and hard to find this error was.

The myLibrary's Url was:

/Library/Documents

while the siteUrl, that was passed to my method was

http://localhost:9999/

This resulted in double slash between server name and list relative path.

1

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.