Alright, the title is a bit over the top and not 100% accurate.  I'll be the first to tell you that I'm no SharePoint Guru, I frankly don't think that many ppl can make that claim either, including most SharePoint MVP's.  There is just SO many things that SharePoint can do and different ways you can do it and again, here is another thing that I've stumbled on.....AFTER the fact.

Recently on a client project I had to upload a huge number of documents to a document library but since I needed to set meta data about each item, WebDAV was out of the question.  What I ended up doing was writing a small GUI app (since I needed to hand it over for others to run later on) that would loop over the items, set the metadata fields and their value, and add them to the document library.  In the code, I would create a new SPListItem, set the fields based on the field name, add the file, and finally add the SPListItem to the collection.

I ran across the following method in my blog readings, SPFileCollection.Add(String,Stream,Hashtable), one of the many overloads of SPFileCollection.Add() where you can pass in the string url of the file, a Stream containing your file (actually, you could pass it in as a byte[] too), and finally a Hashtable of the metadata you want set.  The Hashtable should be created with the Key being the name of the metadata field (not the field GUID), and the Value should be the value you want set.

Now in my above example, this method would have worked, and might have been a few less lines of code, but it's not terribly more efficient than the way I was previously doing because the fields and values were sometimes different per file I was uploading.  I think the real benefit comes when you need to set the same N metadata fields with the same value.  So you would create your Hashtable, then in your file loop, just pass in that same Hashtable.

Posted in:   Tags:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017 Tony Testa's World