Skip to main content

Thinking out aloud - Dave Hunter's SharePoint Blog

Go Search
Home
Blog
  

Locations of visitors to this page


 Useful Links

  Microsoft UK events
  SharePoint 2007 on CodePlex
  SharePoint Community Portal
  SharePoint User Group
  SharePoint Community Portal
  SharePoint Pedia
  SharePoint MSDN Forums
  SharePoint University
Home > Thinking out aloud - Dave Hunter's SharePoint Blog > Posts > Adding a file to a document library with meta data
My thoughts and findings on Microsoft Information worker technologies, including MCMS and SharePoint 2007 (MOSS).
Adding a file to a document library with meta data

There are many ways to upload a file using the API but finding which is the correct method takes time and effort.  You first start looking for a collection to add your SPFile to.  For example:  You can use the SPWeb.Files or Folder.Files.  The SDK use the latter

<snip>

if (oWebsite.GetFolder("Shared Documents").Exists) 
{ 
  SPFolder oFolder = oWebsite.GetFolder("Shared Documents"); 
  oFolder.Files.Add(strFilename, binFile, true); 
}

</snip>

from http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile.aspx

But how do you set meta data for the SPFile.  If you google this you will find people using a hashtable and passing into the SPFileCollection.Add() method.  This sounds like a feasible method, the code looks something like:

// add the metadata to File
Hashtable metaData 

metaData.Add("Title", "My Title"); 

// put on your destination file
SPFileCollection destFiles = web.GetFolder([Folder]).Files; 

destFiles.Add(Path.GetFileName(inputFile.PostedFile.FileName), inputFile.PostedFile.InputStream, metaData, true);

You would expect this to set the title field for the document.  This isn't the case.  The title field is actually called vti_Title if you look at the underlying names for the SPFile.Properties collection.  I wrote a simple app to check the values of an existing document in a SharePoint library.  The output was:

vti_canmaybeedit : true
_Author : Administrator
vti_timecreated : 08/12/2008 12:33:41
_Category :
_Status :
vti_timelastmodified : 08/12/2008 12:33:41
ContentType : Document
vti_title :
vti_replid : rid:{CE94E052-257E-4B95-946B-3B2B91CC0957}
vti_parserversion : 12.0.0.6219
PublishingStartDate :
vti_level : 1
vti_rtag : rt:CE94E052-257E-4B95-946B-3B2B91CC0957@00000000001
vti_sourcecontrolcookie : fp_internal
ContentTypeId : 0x0101003087515E5634704F98FD43AEB256E309
vti_docstoreversion : 1
vti_filesize : 17107
vti_sourcecontrolversion : V1.0
vti_docstoretype : 0
vti_author : DOMAIN\administrator
vti_modifiedby : DOMAIN\administrator
PublishingExpirationDate :

Because of the inconsistent naming I searched for a different approach.  The SPFile has a property called "Item" which contains the SPListItem which is linked to the SPFile.  You can update a SPListItem so how about if I add the file and then update the list item.  Here's the code:

// add the file   
SPFile file = docLib.RootFolder.Files.Add(newFileName, upload.PostedFile.InputStream);   

// get the list item for that file   
SPListItem item = file.Item;   

item["Title"] = "My Title";
item["Another Column"] = "My Value"; 

item.Update();

Hope this helps

By the way: VTI is an acronym of Vermeer Technologies Incorporated, which is the company Microsoft acquired in 1996.

  Copyright
This work is licenced under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 UK: England & Wales License
  Site Map

Comments

Nice and clean way to upate metadata

Very helpful post. It saved me a lot of time. Thanks
at 13/03/2009 15:58

item.Update() adds a new version

I tried same approach too. The problem with it is that item.Update()  adds a new version to the file. Just wondering how to set Title whithout creating multiple versions of a file ?
at 19/03/2009 16:04

 Dave Hunter

I'm currently a Senior Consultant at Netstore 2e2. I specialise in Microsoft Information Worker technologies (especially MOSS, MCMS and .NET), with over 8 years of experience within this area of specialism. 

I have gained two certifications for MOSS and WSS and look to complete the full certification track soon.

View Dave Hunter's profile on LinkedIn

  Subscribe in a reader


 Latest Posts

I’m attending the first ever SharePoint Saturday in the UK31/08/2010 19:43
I’ve re-joined CIBER UK31/08/2010 19:26
I’ve been Awarded a MVP for SharePoint01/04/2010 19:49
SharePoint 2010 Training on Microsoft E-Learning15/03/2010 17:56
SharePoint UK User Group - Thursday 27th August London Meeting25/08/2009 17:56
CAML Query that filters on the current user23/07/2009 17:50
Microsoft Ramp Up Free SharePoint Developer Training22/07/2009 20:59
How To: Change a SharePoint Application Pool Programmatically07/07/2009 18:08
SharePoint Forums Topping 100 Answers06/07/2009 12:29
Find out the SharePoint Internal Name for a Column or Site Column28/05/2009 09:56
1 - 10 Next