RSS Feed Feed your read!

Bookmark and Share







Tag Cloud

ASP.NET Generic, Best Practices, Business Intelligence, Freeware Releases, InfoPath, Infrastructure, jQuery, Lunch & Learn Events, Project Server, Random, Reporting Services, Search, SharePoint Administration, SharePoint Business Analysis and Project Management, SharePoint Development, Silverlight, Social Networking, Speaking Events, White Paper Releases, Workflow Foundation,

Archives

June 2007 (3)
August 2007 (1)
November 2007 (2)
February 2008 (2)
April 2008 (5)
May 2008 (7)
June 2008 (8)
July 2008 (7)
August 2008 (3)
September 2008 (7)
October 2008 (1)
November 2008 (3)
December 2008 (3)
January 2009 (7)
February 2009 (5)
March 2009 (10)
April 2009 (2)
May 2009 (6)
June 2009 (3)
July 2009 (4)
August 2009 (6)
September 2009 (3)
October 2009 (9)
November 2009 (10)
December 2009 (1)
January 2010 (1)
February 2010 (3)
March 2010 (6)
April 2010 (2)
May 2010 (3)
June 2010 (4)
July 2010 (3)

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
5  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


vfmcommented onMonday, 2-Feb-2009
This was very helpful code. We've got it working on a server. I'm wondering if it is possible to run it from client. If so, how to access the microsoft.sharepoint.dll


commented onTuesday, 17-Feb-2009


commented onWednesday, 18-Feb-2009

Bookmark and Share

Note: Facebook no longer sends notifications for comments, so it may be a number of days before I see your post. For urgent matters, click "Contact Me" on the top nav. More info: Click Here.