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 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?

Thanks

share|improve this question
    
Can you paste the code here please? –  Stefano Paterno May 16 '12 at 16:25
add comment

1 Answer 1

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 
            myLibrary.Update();
        }
    }

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

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    // implementation of full code here
});
share|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
add comment

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.