Tuesday 7 June 2016

Use the SharePoint Web Controls



Use the SharePoint Web Controls

SharePoint brings its own controls, which can be used to display list items. In this article I want to show you how to use them in a Webpart. It took me a while to figure this out, because the documentation is kind of incomplete L
OK. Lets start. First lets find out which SharePoint Web Control belongs to which data type in SharePoint.





SharePoint Web Control

SharePoint data type
SharePoint Web Control
Single line of text
TextField
Multiple lines of text
 
PlainText
NoteField
Rich Text
RichTextField
Enhanced Rich Text
RichTextField
Choice
 
Dropdown
DropDownChoiceField
Radio Button
RadioButtonChoiceField
Number
NumberField
Currency
CurrencyField
Date and Time
DateTimeField
Lookup
 
Single Item
LookupField
Multiple Items
MultipleLookupField
Yes/No
BooleanField
Person or Group
UserField
Hyperlink or Picture
UrlField
Calculated
UrlField
Business data
 
How do we find which control belongs to the data type? We can simply look up this information on each field:
string siteUrl = "http://sharepoint";
string webUrl = "spscontrols";
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.AllWebs[webUrl])
{
SPList list = web.Lists["ControlTest"];
foreach (SPField field in list.Fields)
{
Console.WriteLine(field.Title + " – " + field.FieldRenderingControl);
}
}
}

SPControlMode

You can the controls in different Control Modes:
  • SPControlMode.Edit behaves like in an editform page of a list
  • SPControlMode.Display shows the data without the ability to change the values

Use the Controls

So how do we use these controls? The answer to this question is simple: Just use them like "normal" System.Web Controls.
RichTextField rtf = new RichTextField();
rtf.ID = "MultilineRichText";
rtf.ListId = list.ID;
rtf.ItemId = item.ID;
rtf.FieldName = "MultilineRichText";
rtf.ControlMode = SPControlMode.Edit;
this.Controls.Add(rtf);
In this case the RichTextField shows the content from the "MultilineRichText" field from our list, and our listitem in the Editmode. ID and FieldName are the Displayname from our field. You have to set the List, Item and FieldName for the Control, because usually the SharePoint Controls will use the SPContext content (remember: the controls are used in the editform, newform.. pages of every SharePoint List).
With some lines of code, you can display all fields e.g. from the DefaultView of a SharePoint List:
Table table = new Table();
TableRow row;
TableCell cell;
for (int i = 0; i < list.DefaultView.ViewFields.Count; i++)
{
string fieldName = list.DefaultView.ViewFields[i];
SPField field = list.Fields.GetField(fieldName);
row = new TableRow();
cell = new TableCell();
cell.Text = field.Title;
row.Cells.Add(cell);
cell = new TableCell();
// Add a control from RH.SharePoint.SharePointWebControls
Control cntrl = SharePointWebControls.GetSharePointControls(field, list, item, SPControlMode.Display);
// if the control is null (because it can not be rendered with a SharePoint Control) return if (cntrl == null) continue;
cell.Controls.Add(cntrl);
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(SharePointWebControls.GetSharePointControls(field, list, item, SPControlMode.Edit));
row.Controls.Add(cell);
table.Rows.Add(row);
}
this.Controls.Add(table);

Use a generic control

Instead of finding a specific control for each SPField, you can use the BaseFieldControl. The advantage is, that it doesn’t matter which field you want to render. The right control will be used.





 

No comments:

Post a Comment