Programmatically saving SharePoint Server 2010 barcode Images and values to a File System
While working with Barcodes in SharePoint Server 2010, customer wants to utilize them bit more than just use them as part of list item. Initially they are looking for barcodes (from some 3rd parties) to do the job but when implemented via SharePoint using OTB barcodes policy it was nearly a perfect solution for them, except that they also want the barcodes to be saved (in form of a image) along with barcode number/value (which is essentially also acts as a file name) on a file system. Also they want this to be done externally from SharePoint Interface (although still on the server side) more of like a batch or console app. As it turns out it’s more straightforward than I initially thought it would be, as following code reveals. Basically end result was a console app which runs on demand and gets all the barcode from the specific list and simply save the barcode as image (jpg in this case but it can be in other formats) and give it same name as barcode number/value. Following fragment is actually the core of it.
SaveBarcodes(SPWeb web, string listName, System.Drawing.Imaging.ImageFormat imgFormat)
{
SPList list = web.Lists.TryGetList(listName);
SPQuery query = new SPQuery();
Image img;
//Just read the fields we need for this task
query.ViewFields = string.Concat(
“
”, “ ”, “ ”);
query.ViewFieldsOnly = true;
SPListItemCollection listCol = list.GetItems(query);
foreach (SPListItem item in listCol)
{
img = Barcode.GetBarcodeImage(item);
img.Save(string.Format(“{0}.{1}”, Barcode.GetBarcodeValue(item), imgFormat.ToString()), imgFormat);
}
}
Please note that you have to add references to “Microsoft.Office.Policy” and “System.Drawing” assemblies to compile the code. Also add namespaces “Microsoft.Office.RecordsManagement.PolicyFeatures” and “System.Drawing.Image” as they will be required.
Following is the final outcome, see the barcodes saved as images and name of the files are same as that of barcode number/value.

Originally published on WordPress on 2011-02-12.
Read next
-
MVP SharePoint Chat — Wednesday May 25th at 9am PDT
When: 9 AM PDT, 25th May 2011 (PDT? Convert to your time zone here) Where: Online | Enter Chat Room Do you have tough technical questions regarding SharePoint for which you're seeking answers? Do…
-
How to fix SharePoint Online (403) Forbidden Error while downloading files using Client Object Model
Recently I was working on a project that requires to programmatically access and download files from SharePoint Online (part of Office 365) document library. Currently remote authentication (SP…
-
Office 365 Session @ Microsoft NYC
As Office 365 goes into public beta, it's time to dig deeper into the features of Office 365. I am conducing a session on Office 365 with demo of its services @ Microsoft NYC on 27th April 2010…
Worth reading again?
Get the next one in your inbox.