Translate SharePoint Fields to SilverLight


So having the Silverlight Client Object Model in SharePoint 2010 is great. However, some of the most common requests after rich media with Silverlight and SharePoint regard managing data. However, the built in SharePoint 2010 field types can not directly translate to Silverlight controls. This is an example of how I managed to translate most of the common fields to and from SharePoint and Silverlight. Depending on the type of the SPField being passed the correct form field type gets added to the Silverlight form on the fly.

So without delay here’s the code. You can see I have a Grid for handling layout of the form generated.

public void ParseFieldForDisplay(Field field) 

{

SolidColorBrush brush = new SolidColorBrush(Colors.Black);

RowDefinition newRow = new RowDefinition();

newRow.Height = new GridLength(30);

int rowCounter = contentFields.RowDefinitions.Count;

Label fieldLbl = new Label();

fieldLbl.Foreground = brush;

fieldLbl.FontWeight = FontWeights.Bold;

fieldLbl.Margin = new Thickness(5);

ColumnDefinition columnInfo = contentFields.ColumnDefinitions[1];

double targetWidth = columnInfo.Width.Value;

switch (field.FieldTypeKind.ToString())

{

case “Lookup”:

FieldLookup lookup = (FieldLookup)field;

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

ComboBox fieldLookup = new ComboBox();

fieldLookup.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

Grid.SetRow(fieldLookup, rowCounter);

Grid.SetColumn(fieldLookup, 1);

fieldLookup.MinWidth = 200;

fieldLookup.Foreground = brush;

fieldLookup.Name = field.Title;

fieldLookup.Width = targetWidth;

///TODO: Add lookup values as children of field

//uiCollection.Add(fieldLookup);

//contentFields.Children.Add(fieldLookup);

break;

case “Microsoft.SharePoint.SPFieldBoolean”:

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

CheckBox checkBox = new CheckBox();

checkBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

checkBox.Foreground = brush;

checkBox.Width = 24;

Grid.SetRow(checkBox, rowCounter);

Grid.SetColumn(checkBox, 1);

checkBox.Name = field.Title;

uiCollection.Add(checkBox);

contentFields.Children.Add(checkBox);

break;

case “MultiChoice”:

FieldMultiChoice choice = (FieldMultiChoice)field;

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

//combobox choice

ComboBox fieldChoice = new ComboBox();

fieldChoice.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

fieldChoice.MinWidth = 200;

fieldChoice.Foreground = brush;

Grid.SetRow(fieldChoice, rowCounter);

Grid.SetColumn(fieldChoice, 1);

fieldChoice.Name = field.Title;

fieldChoice.Margin = new Thickness(5);

fieldChoice.Width = targetWidth;

//populate value’s within the choice drop down

foreach(string option in choice.Choices)

{

fieldChoice.Items.Add(option);

}

uiCollection.Add(fieldChoice);

//custom text field for free form input

TextBox customChoice = new TextBox();

customChoice.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;

customChoice.MinWidth = 175;

customChoice.Foreground = brush;

Grid.SetRow(customChoice, rowCounter);

Grid.SetColumn(customChoice, 1);

customChoice.Name = field.Title + “CUSTOM”;

//add fields to content fields

contentFields.Children.Add(fieldChoice);

contentFields.Children.Add(customChoice);

break;

case “Microsoft.SharePoint.SPFieldCurrency”:

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

TextBox fieldText = new TextBox();

fieldText.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

fieldText.MinWidth = 200;

fieldText.Foreground = brush;

Grid.SetRow(fieldText, rowCounter);

Grid.SetColumn(fieldText, 1);

fieldText.Name = field.Title;

fieldText.Text = “$0.00”;

uiCollection.Add(fieldText);

contentFields.Children.Add(fieldText);

break;

case “DateTime”:

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

DatePicker fieldDate = new DatePicker();

fieldDate.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

fieldDate.MinWidth = 200;

fieldDate.Foreground = brush;

Grid.SetRow(fieldDate, rowCounter);

Grid.SetColumn(fieldDate, 1);

fieldDate.Name = field.Title;

uiCollection.Add(fieldDate);

contentFields.Children.Add(fieldDate);

break;

case “Microsoft.SharePoint.SPFieldMultiChoice”:

break;

case “Microsoft.SharePoint.SPFieldMultiColumn”:

break;

case “MultiLineText”:

newRow.Height = new GridLength(40);

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

TextBox fieldTextBox = new TextBox();

fieldTextBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

fieldTextBox.MinWidth = 200;

fieldTextBox.Foreground = brush;

Grid.SetRow(fieldTextBox, rowCounter);

Grid.SetColumn(fieldTextBox, 1);

fieldTextBox.TextWrapping = TextWrapping.Wrap;

fieldTextBox.MaxHeight = 80;

fieldTextBox.MinHeight = 40;

fieldTextBox.Name = field.Title;

fieldTextBox.Width = targetWidth;

uiCollection.Add(fieldTextBox);

contentFields.Children.Add(fieldTextBox);

break;

case “Note”:

newRow.Height = new GridLength(40);

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

TextBox noteTextBox = new TextBox();

noteTextBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

noteTextBox.MinWidth = 200;

noteTextBox.Foreground = brush;

Grid.SetRow(noteTextBox, rowCounter);

Grid.SetColumn(noteTextBox, 1);

noteTextBox.TextWrapping = TextWrapping.Wrap;

noteTextBox.MaxHeight = 80;

noteTextBox.MinHeight = 40;

noteTextBox.Name = field.Title;

noteTextBox.Width = targetWidth;

uiCollection.Add(noteTextBox);

contentFields.Children.Add(noteTextBox);

break;

case “Number”:

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

TextBox fieldNumber = new TextBox();

fieldNumber.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

fieldNumber.Foreground = brush;

fieldNumber.MinWidth = 200;

Grid.SetRow(fieldNumber, rowCounter);

Grid.SetColumn(fieldNumber, 1);

fieldNumber.Name = field.Title;

fieldNumber.Text = “Enter Number…”;

uiCollection.Add(fieldNumber);

contentFields.Children.Add(fieldNumber);

break;

case “Microsoft.SharePoint.SPFieldRatingScale”:

break;

case “Url”:

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

TextBox fieldUrl = new TextBox();

fieldUrl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

fieldUrl.MinWidth = 200;

fieldUrl.Foreground = brush;

Grid.SetRow(fieldUrl, rowCounter);

Grid.SetColumn(fieldUrl, 1);

fieldUrl.Name = field.Title;

fieldUrl.Text = “Enter Web Address…”;

fieldUrl.Width = targetWidth;

uiCollection.Add(fieldUrl);

contentFields.Children.Add(fieldUrl);

break;

case “Text”:

contentFields.RowDefinitions.Add(newRow);

fieldLbl.Content = field.Title;

Grid.SetRow(fieldLbl, rowCounter);

Grid.SetColumn(fieldLbl, 0);

contentFields.Children.Add(fieldLbl);

TextBox fieldSingleLineText = new TextBox();

fieldSingleLineText.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

fieldSingleLineText.MinWidth = 200;

fieldSingleLineText.Foreground = brush;

Grid.SetRow(fieldSingleLineText, rowCounter);

Grid.SetColumn(fieldSingleLineText, 1);

fieldSingleLineText.Margin = new Thickness(5);

fieldSingleLineText.MaxLength = 200;

fieldSingleLineText.Name = field.Title;

fieldSingleLineText.Width = targetWidth;

uiCollection.Add(fieldSingleLineText);

contentFields.Children.Add(fieldSingleLineText);

break;

case “Microsoft.SharePoint.SPFieldUser”:

break;

}

}

  1. #1 by Guptha on March 11, 2013 - 1:38 am

    Its good
    I want to get the vales from a list which is having MultIChoice fields.I’m getting the results into a ListItemCollection using CamlQuery but the value related to the MultiChoice and Choice Fields are not showing as expected.
    Like instead of the value in the list i’m getting the values as ‘System.String’..

    Can u please help me to overcome this.

    • #2 by Maarten on March 11, 2013 - 10:25 am

      For SPFieldChoice and SPFieldMultiChoice what you’ll want to do is grab the set of choices for the field. You can see that their is already a control present called ‘fieldChoices’ that will let you select from different choice values. You can also use this as a check box list for multi-select.

      Now to get the value of the field use SPFieldMultiChoice/SPFieldChoice.GetFieldValueAsText for the current value of the field.

(will not be published)


%d bloggers like this: