Friday, April 27, 2012

How to Programmatically Upload File to SharePoint Document Library?

Code snippet to programmatically upload Files to SharePoint List or Library:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using System.Collections;

namespace UploadFiles
{
    class Program
    {
      static void Main(string[] args)
        {
          //Get the Site collection
            using (SPSite site = new SPSite("http://sharepoint.com"))
            {
                //Open the Root web
                using(SPWeb web=site.OpenWeb())
               {
                /*** Simple File Upload ********/
                // Read the file from the stream into the byte array
                FileStream fs= File.OpenRead(@"c:\MyDoc.doc");
                byte[] FileContent= new byte[fs.Length];
                fs.Read(FileContent, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                //Get the documents Librarym named "Documents"
                SPList DocLib = web.Lists["Documents"];
                //Add the file
                web.Files.Add(DocLib.RootFolder + "/MyDoc.doc", FileContent, true);

                /*** If you want to add inside a folder: say "Sales" *******/
                // Get the folder called "Sales"
                SPFolder SubFolder = DocLib.RootFolder.SubFolders["Sales"];
                //Add the file to the sub-folder
                SPFile file = SubFolder.Files.Add(SubFolder.Url + "/MyFile.doc", FileContent, true);
                SubFolder.Update();

                //IF you want to Update the Meta data column say "Country"
                SPListItem item = DocLib.Items[file.UniqueId];
                item["Country"] = "Denmark";
                item.Update();
                // OR We can use the Hash Table
                var Metadata = new Hashtable { { "Country", "India" } };
                // Add the file
                web.Files.Add(DocLib.RootFolder + "/MyDoc2.doc", FileContent, Metadata, true);

                /**** Check-in the file, If the Library has mandatory Columns or Explicit Check-out Enabled *******/
                if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                {
                    file.CheckIn("File uploaded Programmatically !");
                }

                /**** Publish the file using File.publish() method If Minor versions enabled! ****/
                //file.Publish("File published Programmatically !");

              }
            }

        }
    }
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

6 comments :

  1. HI Salaudeen,
    Nice blog!
    Could you please tell me how can i publish InfoPath form in Form library by C# code as same as we publish by publishing wizard, I have seen the methods for uploading the InfoPath form in Library but I want to publish my InfoPath form in Form library by programmatically instead of uploading it.

    Thanks

    ReplyDelete
    Replies
    1. Hi Avanish,

      You can use STSADM command line to Deploy InfoPath Forms.
      More info: http://walisystems.com/articles/SPS/publishusingcmdline/publishing_infopath_web_form_usi.htm

      If you want to make it programmatically, just call stsadm from your code.

      There is a Utility in Codeplex, Along with Source code http://infopathformsinstall.codeplex.com, It does the same! calls STSADM programmatically!!

      Delete
  2. Thanks for reply and help,
    The stsadm command line is useful but can i publish my new InfoPath form in a particular form library which i have created by code on sharepoint site.
    Because I saw that with stsadm we are giving only the sie url and Infopath form's path.

    ReplyDelete
  3. Hi Avanish,

    How would you disable an item created alert if you wanted to bulk upload?

    ReplyDelete
  4. Can you also please tell how to download files from SharePoint library? Its very urgent.
    I have a code with me, but that all of a sudden has started a weird problem.
    The problem is, after downloding the file, its getting corrupt.
    Can you please help/ suggest any workaround?
    Thanks in advance.

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...