Quantcast
Channel: Mavention
Viewing all articles
Browse latest Browse all 715

Inconvenient updating a barcode field in Office 365

$
0
0

​​​​​​​​​Adding a barcode to your document has been a powerful feature of SharePoint for many years now. Unfortunately, it has always had one large deficiency: SharePoint generates the barcode for you and setting your own value requires custom code. Adding insult to injury, the code we used for this called methods marked as 'internal use only' in the Barcode class (in the Microsoft.Office.RecordsManagement.PolicyFeatures namespace). (https://msdn.microsoft.com/en-us/library/microsoft.office.recordsmanagement.policyfeatures.barcode_members.aspx). Quite a few blogs will show how to do this using managed code. This one for example. However, with the introduction of Add-ins for SharePoint, this is no longer the desired solution and it will simply not work for an Office 365 environment where managed code is not a option to begin with. 

So how can we set our own value in a barcode on Office 365? Because the barcode value is simply stored in the listitem properties, we can use CSOM to update it.​

​​​ ​// Set up a ClientContext ClientContext context = new ClientContext("https://my-tenant.sharepoint.com");            // Let's assume this specific listitem is available List list = context.Web.Lists.GetByTitle(​"Documents"); ListItem listItem = list.GetItemById(4);  // Write a new value to the to the barcode property listItem["_dlc_BarcodeValue"] = "1234567890";  // And reset the preview image (since it's cached) listItem["_dlc_BarcodeImage"] = null;  // Go! listItem.Update(); context.ExecuteQuery(); ​
One final note: SharePoint supports 10 characters in the barcode. If you provide less, SharePoint will pad​ the barcode with leading zero's. If you provide more, you'll get some unexpected characters in your barcode. Also, if you use anything but numbers in the barcode, SharePoint will not be able to render the barcode image.

Viewing all articles
Browse latest Browse all 715

Trending Articles