I'm experiencing some problems adding documents to a library using the object model. I use the spfolder.files.add where I pass a filestream containing the document in. When I run my Internet Explorer as administrator, all works fine. When I run it normally it doesn't want to open a filestream to the documents. Any suggestions? I use Kerberos as security protocol. Any suggestions?


  • Can you paste the code here please? – Stefano Paterno May 16 '12 at 16:25

Using object model check this code to upload document to document library from local network.

 String fileToUpload = @"C:\YourFile.txt";
    String sharePointSite = "http://yoursite.com/sites/Research/";
    String documentLibraryName = "Shared Documents";

    using (SPSite oSite = new SPSite(sharePointSite))
        using (SPWeb oWeb = oSite.OpenWeb())
            if (!System.IO.File.Exists(fileToUpload))
                throw new FileNotFoundException("File not found.", fileToUpload);                    

            SPFolder myLibrary = oWeb.Folders[documentLibraryName];

            // Prepare to upload
            Boolean replaceExistingFiles = true;
            String fileName = System.IO.Path.GetFileName(fileToUpload);
            FileStream fileStream = File.OpenRead(fileToUpload);

            // Upload document
            SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

            // Commit 

If you want to upload without permission then put hole code in

    // implementation of full code here
|improve this answer|||||
  • Thanks for the reply. I used the same code as you did, but I'll give the RunWithElevatedPrivileges a try – user7531 May 21 '12 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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