In this post, we will see the functions or the code snippets, which are very frequently required in SharePoint development.
1) Use JavaScript given below to disable field/column in SharePoint custom list.
Steps
Edit your Web part page/ form => add content editor Web part => copy and paste the code given below => change the field name =>save it => after saving, you will see Contact Number field gets disabled.
1) Use JavaScript given below to disable field/column in SharePoint custom list.
Steps
Edit your Web part page/ form => add content editor Web part => copy and paste the code given below => change the field name =>save it => after saving, you will see Contact Number field gets disabled.
- <script type="text/javascript">
- $(document).ready(function() {
- $("input[title='Contact Number']").attr("readonly", "true").css('background-color', '#f1f1f1');
- });
- </script>
2) Get current user and split login name into two i.e domain and name in C# SharePoint with the code given below.
- SPUser user = web.CurrentUser;
- string UserName = user.LoginName;
- str[] name = UserName.Split('\\');
- Label.Text = name[1]
3) The code given below is used to delete the list items.
- SPWeb site = SpContext.Current.Web;
- SPListItemCollection listitems = site.Lists["lstName"].Items;
- for (int i = 0; i < listitems.Count; i++) {
- SPListItem item = listitems[i];
- if (Write condition
- if u have any) {
- item.Delete(i);
- }
- }
4) Get the data of multichoice field of SharePoint custom list into DataTable.
- DataTable dt1 = new DataTable();
- SPFieldMultiChoiceValue choices = new SPFieldMultiChoiceValue();
- SPListItemCollection collListItems = list.Items;
- foreach(SPListItem item in collListItems) {
- choices = new SPFieldMultiChoiceValue(item["Name of Column"].ToString());
- }
- for (int i = 0; i < choices.Count; i++) {
- dt1.Rows.Add(choices[i], choices[i]);
- }
5) Using blocks, which is commonly required in all the Web parts.
- try {
- using(SPSite site = new SPSite(SpContext.Current.Site)) {
- using(SPWeb web = site.OpenWeb()) {
- // ur code
- }
- }
- } catch (Exception ex) {} catch (SPException ex1) {}
6) Bind SharePoint Custom list Title column to dropdown list.
Add the code given below in .ascx file.
Add the code given below in .ascx file.
- <div id="dropdown">
- <asp:DropDownList ID="ddlv" CssClass="ddl" runat="server"></asp:DropDownList>
- </div>
Add the code given below in .cx file.

Join the conversation! Your thoughts help the community grow.