Monday, 13 February 2012

Uploading document programmatically


 static void Main(string[] args)
        {
            string filepath = @"D:\KA_AllFiles\Training Materials\Wipro Training\Infopath Form.docx";
            string libraryname = "Shared Documents";
            using (SPSite osite = new SPSite("http://admin-pc:2345"))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    if (System.IO.File.Exists(filepath))
                    {  
                        SPFolder myLibrary = oweb.Folders[libraryname];
                        String fileName = System.IO.Path.GetFileName(filepath);
                        FileStream fileStream = File.OpenRead(filepath);
                        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, true);
                        myLibrary.Update();
                    }
                    else
                    {
                        throw new FileNotFoundException("file not found");
                    }
                }
            }

        }

No comments:

Post a Comment