SharePoint WebControls
In this post I’m going to tell you how to use
SharePoint’s web controls in our custom code. It is very easy to use and it gives
look and feel of SharePoint’s default forms. First we have to add the reference
for Microsoft.SharePoint assembly and add
using Microsoft.SharePoint.WebControls
using Microsoft.SharePoint.WebControls
Assembly: Microsoft.SharePoint (in
microsoft.sharepoint.dll)
Namespace: Microsoft.SharePoint.WebControls
Namespace: Microsoft.SharePoint.WebControls
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["ListName"];
SPList list = web.Lists["ListName"];
// Display Lookup field with Multiple selection
enabled
MultipleLookupField mlkpfield = new MultipleLookupField();
mlkpfield.ListId = list.ID;
mlkpfield.FieldName = "MultiLookup";
mlkpfield.ControlMode = SPControlMode.New;
this.Controls.Add(mlkpfield);
MultipleLookupField mlkpfield = new MultipleLookupField();
mlkpfield.ListId = list.ID;
mlkpfield.FieldName = "MultiLookup";
mlkpfield.ControlMode = SPControlMode.New;
this.Controls.Add(mlkpfield);
// Display List View Toolbar
ViewToolBar toolbar = new ViewToolBar();
SPContext context = SPContext.GetContext(this.Context, list.DefaultView.ID, list.ID, SPContext.Current.Web);
ViewToolBar toolbar = new ViewToolBar();
SPContext context = SPContext.GetContext(this.Context, list.DefaultView.ID, list.ID, SPContext.Current.Web);
toolbar.RenderContext = context;
this.Controls.Add(toolbar);
this.Controls.Add(toolbar);
// Display List View
ListView lv = new ListView();
lv.ListId = list.ID.ToString();
lv.ViewId = list.DefaultView.ID.ToString();
this.Controls.Add(lv);
ListView lv = new ListView();
lv.ListId = list.ID.ToString();
lv.ViewId = list.DefaultView.ID.ToString();
this.Controls.Add(lv);
// Display DateTime Picker
DateTimeControl dtc = new DateTimeControl();
dtc.DateOnly = true;
dtc.UseTimeZoneAdjustment = true;
this.Controls.Add(dtc);
// Display Rich Text Editor for
Multiline Text field
rText = new RichTextField();
rText.ControlMode = SPControlMode.New;
rText.ListId = list.ID;
rText.FieldName = "MText";
this.Controls.Add(rText);
// Display Lookup field with Multiple
selection disabled
LookupField lkpfield = new LookupField();
lkpfield.ListId = list.ID;
lkpfield.ieldName = "Demo";
lkpfield.ControlMode = SPControlMode.New;
lkpfield.DisplaySize = 30;
this.Controls.Add(lkpfield);
// Display Cancel button for list
MultiPageGoBackButton btnmpg = new MultiPageGoBackButton();
btnmpg.ControlMode = SPControlMode.New;
btnmpg.ListId = list.ID;
this.Controls.Add(btnmpg);
// Display ListView Selector dropdown
ListViewSelector lstViewSel
= new ListViewSelector();
lstViewSel.RenderContext = context;
this.Controls.Add(lstViewSel);
// Display ListView By Query
ListViewByQuery view = new ListViewByQuery();
SPList list1 =
web.Lists["AskedQuestions"];
view.List = list1;
SPQuery query = new SPQuery(view.List.DefaultView);
query.ViewFields = "<FieldRef
Name='Title'/><FieldRef Name='QuestionDescription'/>";
query.Query = "<Where><Contains><FieldRef
Name='Category'/><Value
Type='Text'>IT</Value></Contains></Where>";
view.Query = query;
view.DisableFilter = true;
view.DisableSort = true;
this.Controls.Add(view);
// Render Edit Item button
EditItemButton btnEdit = new EditItemButton();
btnEdit.ListId = list.ID;
btnEdit.ItemId = 2;
btnEdit.Text = "Edit";
btnEdit.ControlMode = SPControlMode.Edit;
this.Controls.Add(btnEdit);
// Render URL field
urlField = new UrlField();
urlField.ListId = lstLink.ID;
urlField.FieldName = "URL";
urlField.ControlMode = SPControlMode.Edit;
urlField.ItemId = 5;
this.Controls.Add(urlField);
urlField.DisplaySize=50;
//Displays Document Uploaded by Current
User on a Site as a Web Part
usrDocs = new UserDocsWebPart();
this.Controls.Add(usrDocs);
// Render User field
usrField = new UserField();
usrField.ControlMode = SPControlMode.Edit;
usrField.ListId = list.ID;
usrField.ItemId = 5;
usrField.FieldName = "Created
By";
this.Controls.Add(usrField);
usrField.IsValid ---------- to check if field
is required
// Render Save button on New/ Edit Form
but I'm not able so save Data Using this button
btnSave = new SaveButton();
btnSave.ListId = list.ID;
btnSave.ControlMode = SPControlMode.New;
btnSave.Text = "Save";
// Render Form Toolbar (with
attach file link and '* indicates a required field')
FormToolBar ftbar = new FormToolBar();
ftbar.ControlMode = SPControlMode.New;
ftbar.ListId = list.ID;
this.Controls.Add(ftbar);
//HTML = ftbar.GetDesignTimeHtml();
// Render Edit Document Link
EditDocumentLink edlink = new EditDocumentLink();
edlink.DocumentUrl = "http://indelplmcdr/Shared%20Documents/TickerTest.aspx";
edlink.Text = "Edit
Doc";
this.Controls.Add(edlink);
// Render Action Menu
ActionsMenu AcMenu = new ActionsMenu();
SPContext context = SPContext.GetContext(this.Context,
list.DefaultView.ID, list.ID, SPContext.Current.Web);
AcMenu.RenderContext = context;
// Render Recent Changes Menu
RecentChangesMenu rcmenu = new RecentChangesMenu();
rcmenu.RenderContext = context;
// Insert value in Lookup field
SPFieldLookupValue lkpvalue = new SPFieldLookupValue();
lkpvalue.LookupId= (int)n;
SPFieldUserValue usrVal = new SPFieldUserValue(web,list.Items[0]["Created By"].ToString());
usrVal.LookupId; // UserID
Comments
Post a Comment