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'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.

share|improve this question
    
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 –  RJ Cuthbertson Dec 26 '12 at 18:52

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
You could use url combining to avoid these type of bugs: stackoverflow.com/questions/372865/path-combine-for-urls –  Janis Veinbergs May 14 '12 at 8:14

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

share|improve this answer
    
I changed my code as you suggested, but unfortunately, I get the same error. See my update. –  Tschareck Apr 16 '12 at 11:34

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.