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

Tags: SharePoint Development

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();
        }
    }
}

 
Posted by Phillip S. Wicklund on 2-Apr-08
2  Comments  |  Trackback Url  | 0  Link to this post | Bookmark this post with:        
 
Failed to render control: Value does not fall within the expected range.

Comments


Can you modify the modify date and user via the metadata update?commented onWednesday, 28-May-2008
The reason I ask is that when we do mass uploads, I'd like to preserve the mod date and user.


(RE) Can you modify the modify date and user via the metadata update? commented onWednesday, 28-May-2008
I know a listitem has a "Created By" field you can try editing as well as a "Created" field that takes a DateTime object. Try those. If you get a NullReferenceException when you edit those fields, those are the wrong display names and you'll need to go by the internal name of the fields. Phil

Name:
URL:
Email:
Comments: