Sunday 5 January 2020

Only allowed number in textbox using jquery

Introduction
Only allowed number in textbox using jquery.  Add 'numberonly' class to text control then it will only allow number.
 
Code  

  1. $(document).ready(function () {          
  2.             $('.numberonly').keypress(function (e) {          
  3.                 var charCode = (e.which) ? e.which : event.keyCode          
  4.                 if (String.fromCharCode(charCode).match(/[^0-9]/g))         
  5.                     return false;                              
  6.             });          
  7.         });    

Validation using Javascript, jQuery using the class

Introduction
Validation using Javascript, jQuery using the class name.
You just need to add class 'mandatory' in control then this control will be required for submission of the form. if it is not filled then it will be red highlighted.

Code
  1. function Validation() {    
  2. var isValid = true;    
  3. var classname = 'mandatory';    
  4. $('.' + classname + '').each(function (i, obj) {    
  5. if (obj.value == '') {    
  6. isValid = false;    
  7. return isValid;    
  8. }    
  9. });  
  10.   
  11. if (!isValid) {    
  12. $('.' + classname + '').each(function (i, obj) {    
  13. if (obj.value == '') {    
  14. obj.style.border = '1px solid red';    
  15. }    
  16. else {    
  17. obj.style.border = '1px solid black';    
  18. }    
  19. });    
  20. alert('Please fill mandatory details');    
  21. }    
  22. if (isValid) {    
  23. return confirm('Are you sure you want to save information? Once information stored will not be updated.')    
  24. }    
  25. return isValid;    
  26. }    
  27.   
  28. for ex.    
  29.   
  30. ASP textbox : <asp:TextBox ID="txtlocation" runat="server" CssClass="form-control mandatory"></asp:TextBox>     
  31. html input text : <input class="mandatory" type="text">  

Saturday 4 January 2020

Useful commands for spfx development using Node.js command prompt, Spfx commands Cheat sheet

These are few commands which are very useful when developing Spfx web part. It requires installation fo several components which includes jQuery, Bootstrap, Datatable, Gulp, PnP js, jquery UI, Yeoman etc.





Here is the list of useful commands for Sharepoint Framework.


Install jQuery

            Npm install jquery -save

            Npm install @types/jquery --save
Install bootstrap
            npm install bootstrap –save
            npm install @types/bootstrap –save
Install datatable
            npm install datatables.net –save
            npm install @types/datatables.net --save
Build the solution
            Gulp bundle
Create package of the solution
             Gulp package-solution
Run the solution
             Gulp serve
Open the solution in the code editor of your choice
            Code.
install Yeoman and gulp
            npm install -g yo gulp
install the SharePoint Framework Yeoman generator globally
            npm install -g @microsoft/generator-sharepoint
trust the certificate
            gulp trust-dev-cert
Install spfx controls react controls
            npm install @pnp/spfx-controls-react --save --save-exact
Install PnP components
npm install node-pnp-js –save
Install Jquryui
            npm install @types/jqueryui –save
To kill the node process that is running the live server with Gulp
              taskkill /f /im node.exe
To check the version of the Yeoman
            yo –version
To check the version of the Gulp
            gulp –v


Saturday 30 July 2016

How to get all the Term from TermStore in SharePoint Using C#

Introduction
 
Retrieve all the Terms from TermStore in SharePoint Using C#, Term store management, Managed metadata.
We are fetching CustomTermset from CustomGroup.

Code
 
  1. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))      
  2.             {      
  3.                 using (SPWeb RootWeb = RootSite.OpenWeb())      
  4.                 {      
  5.                     TaxonomySession TaxonomySession = new TaxonomySession(RootSite);      
  6.                     TermStore termstore = TaxonomySession.TermStores["Managed Metadata Service"];      
  7.                     Group group = termstore.Groups["CustomGroup"];      
  8.                     TermSet termset = group.TermSets["CustomTermset"];     
  9.                     TermCollection termcoll = termset.GetAllTerms();      
  10.                     StringBuilder strbl = new StringBuilder();      
  11.                     foreach (Term term in termcoll)      
  12.                     {      
  13.                         strbl.Append(term.Name.ToString());                              
  14.                     }      
  15.                     Literal1.Text = strbl.ToString();      
  16.               }      
  17.             }    
 
 

CRUD Operation in SharePoint list using Server Side Object Model (SSOM) and C#

Introduction 

Create list item, Update list item, Read data from list, Delete list item in SharePoint using C#

Code 
    1. .ascx page code with some basic fields  
    2.   
    3.   
    4.   
    5. <table>
    6.   
    7. <tr>  
    8.   
    9. <td> </td>  
    10.   
    11. <td>List CRUD</td>  
    12.   
    13. <td> </td>  
    14.   
    15. </tr>  
    16.   
    17. <tr>  
    18.   
    19. <td>Title</td>  
    20.   
    21. <td>  
    22.   
    23. <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>  
    24.   
    25. </td>  
    26.   
    27. <td> </td>  
    28.   
    29. </tr>  
    30.   
    31. <tr>  
    32.   
    33. <td>Name</td>  
    34.   
    35. <td>  
    36.   
    37. <asp:TextBox ID="txtName" runat="server"></asp:TextBox>  
    38.   
    39. </td>  
    40.   
    41. <td> </td>  
    42.   
    43. </tr>  
    44.   
    45. <tr>  
    46.   
    47. <td>Age</td>  
    48.   
    49. <td>  
    50.   
    51. <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>  
    52.   
    53. </td>  
    54.   
    55. <td> </td>  
    56.   
    57. </tr>  
    58.   
    59. <tr>  
    60.   
    61. <td>Update</td>  
    62.   
    63. <td>  
    64.   
    65. <asp:TextBox ID="txtUpdate" runat="server"></asp:TextBox>  
    66.   
    67. </td>  
    68.   
    69. <td> </td>  
    70.   
    71. </tr>  
    72.   
    73. <tr>  
    74.   
    75. <td> </td>  
    76.   
    77. <td>  
    78.   
    79. <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add" Width="75px" />  
    80.   
    81. <asp:Button ID="btnCancel" runat="server" Text="Cancel" Width="75px" />  
    82.   
    83. <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" Width="75px" />  
    84.   
    85. <asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" Width="75px" />  
    86.   
    87. <asp:Button ID="btnRetrieve" runat="server" OnClick="btnRetrieve_Click" Text="Retrieve" Width="75px" />  
    88.   
    89. </td>  
    90.   
    91. <td> </td>  
    92.   
    93. </tr>  
    94.   
    95. </table>  
    96.   
    97.   
    98. .ascx.cs Page code  
    99.   
    100.   
    101. protected void btnAdd_Click(object sender, EventArgs e)  
    102.   
    103. {  
    104.   
    105. //Create SPSite object  
    106.   
    107. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))  
    108.   
    109. {  
    110.   
    111. //create SPWeb object  
    112.   
    113. using (SPWeb RootWeb = RootSite.OpenWeb())  
    114.   
    115. {  
    116.   
    117. //create splist object  
    118.   
    119. SPList spList = RootWeb.Lists["ListCRUD"]; //ListCrud is a sharepoint list name  
    120.   
    121. SPListItem spListItem = spList.AddItem(); //create splistitem object to add item  
    122.   
    123. spListItem["Title"] = txtTitle.Text;  
    124.   
    125. spListItem["Name"] = txtName.Text;  
    126.   
    127. spListItem["Age"] = Convert.ToInt32(txtAge.Text);  
    128.   
    129. spListItem.Update(); //item update  
    130.   
    131. }  
    132.   
    133. }  
    134.   
    135. }  
    136.   
    137.   
    138. protected void btnUpdate_Click(object sender, EventArgs e)  
    139.   
    140. {  
    141.   
    142. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))  
    143.   
    144. {  
    145.   
    146. using (SPWeb RootWeb = RootSite.OpenWeb())  
    147.   
    148. {  
    149.   
    150. int id = Convert.ToInt32(txtUpdate.Text);  
    151.   
    152. SPList spList = RootWeb.Lists["ListCRUD"];  
    153.   
    154. SPListItem spListItem = spList.GetItemById(id);  
    155.   
    156. spListItem["Title"] = txtTitle.Text;  
    157.   
    158. spListItem["Name"] = txtName.Text;  
    159.   
    160. spListItem["Age"] = Convert.ToInt32(txtAge.Text);  
    161.   
    162. spListItem.Update();  
    163.   
    164. }  
    165.   
    166. }  
    167.   
    168. }  
    169.   
    170.   
    171. protected void btnRetrieve_Click(object sender, EventArgs e)  
    172.   
    173. {  
    174.   
    175. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))  
    176.   
    177. {  
    178.   
    179. using (SPWeb RootWeb = RootSite.OpenWeb())  
    180.   
    181. {  
    182.   
    183. int id = Convert.ToInt32(txtUpdate.Text);  
    184.   
    185. SPList spList = RootWeb.Lists["ListCRUD"];  
    186.   
    187. SPQuery spQuery = new SPQuery();  
    188.   
    189. spQuery.Query = string.Concat("<Where>",  
    190.   
    191. "<Eq>",  
    192.   
    193. "<FieldRef Name='ID' />",  
    194.   
    195. "<Value Type='Counter'>" + id + "</Value>",  
    196.   
    197. "</Eq>",  
    198.   
    199. "</Where>");  
    200.   
    201. spQuery.ViewFieldsOnly = true;  
    202.   
    203. spQuery.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='Name' /><FieldRef Name='Age' />";  
    204.   
    205. SPListItemCollection spListColl = spList.GetItems(spQuery);  
    206.   
    207. DataTable dt = new DataTable();  
    208.   
    209. dt = spListColl.GetDataTable();  
    210.   
    211.   
    212. txtTitle.Text = dt.Rows[0]["Title"].ToString();  
    213.   
    214. txtName.Text = dt.Rows[0]["Name"].ToString();  
    215.   
    216. txtAge.Text = dt.Rows[0]["Age"].ToString();  
    217.   
    218. }  
    219.   
    220. }  
    221.   
    222. }  
    223.   
    224.   
    225. protected void btnDelete_Click(object sender, EventArgs e)  
    226.   
    227. {  
    228.   
    229. using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))  
    230.   
    231. {  
    232.   
    233. using (SPWeb RootWeb = RootSite.OpenWeb())  
    234.   
    235. {  
    236.   
    237. int id = Convert.ToInt32(txtUpdate.Text);  
    238.   
    239. SPList spList = RootWeb.Lists["ListCRUD"];  
    240.   
    241. SPListItem spListItem = spList.GetItemById(id);  
    242.   
    243. spListItem.Delete();  
    244.   
    245. spList.Update();  
    246.   
    247. }  
    248.   
    249. }  
    250.   
    251. }