Using the WBColumn class and WBx extension methods

A frustrating aspect of the standard SharePoint SPField object classes is that you can only get hold of them from existing SPFields (e.g. on a list or site or whatever). Within the WBF for various reasons there is a need to model ‘columns’ in a slightly more abstract way (for example before you know if the column exists or before you have created the column as well as after creating the column).

This is why the WBColumn class was created. It’s a long way from perfect, but it tries to model in a fairly useful many of the various different types of columns/fields that exist in SharePoint. The 'WBx' prefix is used by extension methods provided by the WBF in order to get things done more conveniently. The following are some examples:

The following factory method creates a ‘Single line of text’ WBColumn with ‘External URL’ as the display name and ‘ExternalURL’ as the internal name:
WBColumn   externalURL = WBColumn.TextColumn(“External URL”);    

The useful thing about the following method is that it ‘fails gracefully’ in the sense that if the column is null or if the column doesn’t exist on the item then it returns a blank string rather than throwing an error.
String value = item.WBxGetAsString(externalURL); 

If you need to test for the existence of a column you can call:
if (item.WBxExists(externalURL)) { …. } 

You can also check if a column has a value (which returns false if the column doesn't even exist)
if (item.WBxHasValue(externalURL)) { …. } 

This can be particularly useful if you need to check that a datetime column has a value:
WBColumn meetingDateColumn = WBColumn.DateTimeColumn("Meeting Date");

if (item.WBxHasValue(meetingDateColumn)) 
{
     DateTime meetingDateValue = (DateTime)item.WBxGet(meetingDateColumn); 

     .... do something with the date value ...
}

More documentation regarding these and other classes will follow.


Last edited Apr 24, 2013 at 11:49 AM by OliSharpe, version 7

Comments

No comments yet.