Recently, I had to build the following application: pdf documents were generated automatically from an external application and associated with a set of text files containing their meta-data. The main goal was to upload those documents into a SharePoint document list, with there meta-data extracted from the text file. Then a workflow is invoked for approbation with several parameters depending on meta-data.
In this article I'll focus on importing files into the list with their meta-data.
The first thought that came to my mind was about uploading one document into the list, then adding meta-data one by one to their corresponding columns. But because I have to manage versions for this list, this solution seems not adequate: one update for the document and one update for the meta-data means two versions instead of one.
But I've found one overloading really interesting.
First, let's consider the list like a folder. And inside this folder we have a collection of files. If I add a file to this collection, this is the same as uploading a file, no ?
The folder name is the internal name of the list. The internal name is the name you first set for the list. For instance, if you have created a Purchase Order list with the name PurchaseOrder, then you have modified the name to Purchase Order, the folder name will be PurchaseOrder. You can check it with the url, i.e. http://dcmoss:50010/testgy/PurchaseOrder/ where PurchaseOrder is the name of the folder.
SPFolder folder = _web.GetFolder("internal list name");
SPFileCollection files = folder.Files;
Then I get the file itself, storing it in a memory stream
FileStream fStream = File.OpenRead("fileName");
And, at last, I create the URL of the file, with the file name
string url = "url web site" + "internal list name" + "/" + Path.GetFileName("fileName");
Meta-data have to be stored into a Hashtable
HashTable MetaDataTable = new HashTable();
MetaDataTable.Add("column name", "value");
In fact, I have a class which parse the text file and send me back a Hashtable. Then I add everything to the file collection
SPFile currentFile = files.Add(url, fStream, MetaDataTable, true);
The boolean value allow you to replace a file if it is already existing
In one line, we add the file with its meta-data ! Not bad.
Here is an example of code for adding a document with its meta-data
//Using required, especially System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
namespace DocImport
{
public class DocManagement {
//Some variables
private string _DestUrlPath = string.Empty;
private string _DestFolder = string.Empty;
private SPSite _site = null;
private SPWeb _web = null;
public DocManagment(string DestUrl, string DestFolder) {
_DestUrlPath = DestUrl;
_DestFolder = DestFolder;
_site = new SPSite(_DestUrlPath);
_web = _site.OpenWeb();
}
private bool _uploadDocument(string pdfFile, string txtFile) {
try {
//This is mandatory for avoiding an error
_web.AllowUnsafeUpdates = true;
//Use the list as a folder
SPFolder folder = _web.GetFolder(_DestFolder);
SPFileCollection files = folder.Files;
//Get the file
FileStream fStream = File.OpenRead(pdfFile);
HashTable MetaDataTable = new HashTable();
MetaDataTable.Add("nom de colonne", "valeur");
//Set the destination url for SharePoint
string url = _DestUrlPath + _DestFolder + "/" + Path.GetFileName(pdfFile);
//Add document to the list with metadata, and overwrite an existing document
with the same name
SPFile currentFile = files.Add(url, fStream, MetaDataTable, true);
fStream.Close();
return true;
} catch (Exception x) {
throw new Exception(x.Message);
}
}
}