In yet another flagrant display of “We won’t document it if it don’t work.”, the SPFileCollection.Add() overloads allowing you to specify file properties using a Hashtable.
For example,
Add(String, Byte[], Hashtable, Boolean)
would imply that specifying the file URL, file bytes, a Hashtable containing metadata, and a true (to over-write any existing file with the same file name) might be a useful way to both add a file to a document library and adorn it with metadata.
Except it doesn’t work real well.
You are supposed to be able to declare and use a Hashtable in the following way:
Hashtable archiveProperties = new Hashtable();
archiveProperties.Add("Supplier_x0020_Name", PostedFormValues[supplierName.ID]);
archiveProperties.Add("Supplier_x0020_Code", PostedFormValues[supplierNumber.ID]);
etc…
SPFile newZipFile = destFolder.Files.Add(destURL, zip, archiveProperties, true);
newZipFile.CheckIn();
What results is a file in a document library without any metadata. No exceptions, just no metadata.
Here’s how you work-around this crap.
SPFile newZipFile = destFolder.Files.Add(destURL, zip, true);
newZipFile.CheckIn();
// Get a SPListItem for the document
SPListItem item = newZipFile.Item;
item.File.CheckOut();
item["Supplier_x0020_Name"] = PostedFormValues[supplierName.ID];
item["Supplier_x0020_Code"] = PostedFormValues[supplierNumber.ID];
etc…
item.Update();
item.File.CheckIn("Application generated file metadata.");
By obtaining a reference to the SPListItem object for the file, you are able to Check Out the file, update it, and then do a Check In.
For example,
Add(String, Byte[], Hashtable, Boolean)
would imply that specifying the file URL, file bytes, a Hashtable containing metadata, and a true (to over-write any existing file with the same file name) might be a useful way to both add a file to a document library and adorn it with metadata.
Except it doesn’t work real well.
You are supposed to be able to declare and use a Hashtable in the following way:
Hashtable archiveProperties = new Hashtable();
archiveProperties.Add("Supplier_x0020_Name", PostedFormValues[supplierName.ID]);
archiveProperties.Add("Supplier_x0020_Code", PostedFormValues[supplierNumber.ID]);
etc…
SPFile newZipFile = destFolder.Files.Add(destURL, zip, archiveProperties, true);
newZipFile.CheckIn();
What results is a file in a document library without any metadata. No exceptions, just no metadata.
Here’s how you work-around this crap.
SPFile newZipFile = destFolder.Files.Add(destURL, zip, true);
newZipFile.CheckIn();
// Get a SPListItem for the document
SPListItem item = newZipFile.Item;
item.File.CheckOut();
item["Supplier_x0020_Name"] = PostedFormValues[supplierName.ID];
item["Supplier_x0020_Code"] = PostedFormValues[supplierNumber.ID];
etc…
item.Update();
item.File.CheckIn("Application generated file metadata.");
By obtaining a reference to the SPListItem object for the file, you are able to Check Out the file, update it, and then do a Check In.
This is a very old article but thought I'd let you know that this does work in SP 2010. You just have to use the Internal Name of the fields as the key.
ReplyDeleteGood luck!