SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i have an InfoPath 2007 file which includes code to generate a signed PDF out of the current infopath form. Now i want to upload this PDF via C# code into a sharepoint 2010 library.

I tried

            SPSite objSite = new SPSite(spSite);
            SPFolder objFolder = objSite.OpenWeb().GetFolder(spLibraryName);
            string sourcePath = document;

            FileInfo fi = new FileInfo(sourcePath);
            int fileSize = (int)fi.Length;
            SPFileCollection objFiles = objFolder.Files;
            byte[] myFile = new byte[fileSize];
            FileStream mystream = new System.IO.FileStream(sourcePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            mystream.Read(myFile, 0, fileSize);
            string strFileUrl = document;
            SPFile spf = objFiles.Add(strFileUrl, myFile);

but it did not work because the SPSite could not be created /found.

Now i want to know if it is possible to use a dataConnection to put the PDF-file through.

How can i solve this?

thank you, el

share|improve this question

Call EnsureParentFolder(SPWeb parentSite, string destinUrl) first, before you add the file to the collection.

share|improve this answer
i'll give it a try. Previously i have to upgrade to Sharepoint 2010 standard edition because i am running SP Foundation :( I hope this will help me out too – elCapitano Mar 29 '12 at 9:25
nope, nothing helped :( I even can not create the SPWeb Object.. therefore the ensureParentFolder is not able to help – elCapitano Apr 1 '12 at 11:23
Are you sure your Uri for spSite is valid? Is the SPSite object being created successfully? – RJ Cuthbertson Apr 2 '12 at 12:27
I can access the URI from IE and it works well. I dont know what i can do – elCapitano Apr 3 '12 at 7:29
OK, so let's backtrack a second here. In your post it says you can not create SPSite. Is this still true? Then you said you can not create SPWeb. Is this still true? Have you used the debugger to step line by line verifying the data you are passing? Finally, the value of strFileUrl should be the destination Url, not the source Url. Concatenate the destination folder Url, a \, and the desired filename. See: – RJ Cuthbertson Apr 3 '12 at 18:25

Your Answer


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.