Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

this should help you out. No need to cycle through all document library lists in the web objects! if you know which one to pass then that's what you need.

    public void createLibraryLocation(string documentLibraryName, string fileToUpload)
    {
        using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
        {
            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();
            }
        }
    }

code was taken from here but amended slightly so you can call it passing two variables:

http://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepointhttps://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint

to use the method above just do the following:

createLibraryLocation("Documents", @"C:\mydocument.txt");

"documents" is the name of the document library and "mydocument.txt" is the file name located at "c" drive.

hope it helps.

this should help you out. No need to cycle through all document library lists in the web objects! if you know which one to pass then that's what you need.

    public void createLibraryLocation(string documentLibraryName, string fileToUpload)
    {
        using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
        {
            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();
            }
        }
    }

code was taken from here but amended slightly so you can call it passing two variables:

http://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint

to use the method above just do the following:

createLibraryLocation("Documents", @"C:\mydocument.txt");

"documents" is the name of the document library and "mydocument.txt" is the file name located at "c" drive.

hope it helps.

this should help you out. No need to cycle through all document library lists in the web objects! if you know which one to pass then that's what you need.

    public void createLibraryLocation(string documentLibraryName, string fileToUpload)
    {
        using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
        {
            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();
            }
        }
    }

code was taken from here but amended slightly so you can call it passing two variables:

https://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint

to use the method above just do the following:

createLibraryLocation("Documents", @"C:\mydocument.txt");

"documents" is the name of the document library and "mydocument.txt" is the file name located at "c" drive.

hope it helps.

Source Link
Ali Jafer
  • 17.8k
  • 1
  • 28
  • 41

this should help you out. No need to cycle through all document library lists in the web objects! if you know which one to pass then that's what you need.

    public void createLibraryLocation(string documentLibraryName, string fileToUpload)
    {
        using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
        {
            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();
            }
        }
    }

code was taken from here but amended slightly so you can call it passing two variables:

http://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint

to use the method above just do the following:

createLibraryLocation("Documents", @"C:\mydocument.txt");

"documents" is the name of the document library and "mydocument.txt" is the file name located at "c" drive.

hope it helps.