SharePoint 2010 Products & Technologies

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(

“<FieldRef Name=’_dlc_BarcodePreview’ />”, “<FieldRef Name=’_dlc_BarcodeImage’ />”, “<FieldRef Name=’_dlc_BarcodeValue’ />”);

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.

Advertisements

2 thoughts on “Programmatically saving SharePoint Server 2010 barcode Images and values to a File System

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s