Skip Ribbon Commands Skip to main content

SharePoint Happenings

Help (new window)
Sign In
Navigate Up
Get Microsoft Silverlight
Install Silverlight plugin for a richer experience...
Blog Home |  Freeware |  Speaking |  About me

Programmatically Uploading a Document into a Document Library with Meta-Data Modifications



Bookmark and Share

Top Tech Links










Top SharePoint Administrator Links









Top SharePoint Developer Links













NOTE: This blog has been moved to http://blog.philwicklund.com. If you'd like assistance, leave a comment on the copied post in the new location (hint, use search on the right to find the post).


Recently I was at a company who needed a system that deployed documents into their farm programmatically (Lotus to SharePoint conversion) against some business rules. In one of my teaches, the students and I prototyped this example of how to programmatically upload a document into a document library. The code also demonstrates modifying arbitrary meta data that is associated with that document. This concept is nothing that hasn't already been documented in the SDK, but it was a fun example to work through with my students so I figured I'd post our code. See below:

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

namespace TestUploadFile
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read a document off the c drive and open a stream
            FileStream stream = File.OpenRead(@"c:\test.doc");
            byte[] content = new byte[stream.Length];

            // Read the file from the stream into the byte array
            stream.Read(content, 0, (int)stream.Length);
            stream.Close();

            // Give the file a name, used as the name of the list
            // item once it gets into the document library
            string fileNameOnceInLibrary = "justUploaded.doc";

            SPSite mySiteCollection = new SPSite("http://intranet");
            SPWeb myWeb = mySiteCollection.RootWeb;

            // Get the folder that should store the document
            // In this case, there's a document library called "Docs"
            // within the Root Web of the Site Collection
            SPFolder parent = myWeb.Folders["Docs"];

            // Within the "Docs" library, add the document into
            // a folder called "Folder1"
            SPFolder child = parent.SubFolders["Folder1"];

            // Add the file to the Files collection and commit to database
            SPFile file = child.Files.Add(child.Url + "/" +
                  fileNameOnceInLibrary, content, true);
            child.Update();

            // This next portion demonstrates modifying meta data
            // on the document that was just uploaded

            SPDocumentLibrary docs = (SPDocumentLibrary)myWeb.Lists
                  [child.ContainingDocumentLibrary];

            SPListItem item = docs.Items[file.UniqueId];
            item["Department"] = "Marketing";
            item.Update();
        }
    }
}