Posts

Showing posts from February, 2015

Setting maxlength to a textarea

The maxlength attribute is new for the <textarea> tag in HTML5 . But what if  you are not using html5 or what if your browser doesn't support this attribute. You might have faced this issue where you being not able to set maxlength property to a textarea. So here is the trick using the power of Jquery  :P. var maxLength = 50; //Set the length here $(document).ready(function(){ setMaxLenTexrArea(); }); function setMaxLenTexrArea() {     $("textarea").each(function () {         $(this).on('change keydown keyup keypress paste blur', function () {             restrictLength($(this).prop('id'));         });     }); } function restrictLength(ta) {       var txtVal = $("#" + ta).val();     if (txtVal.length > maxLength) {         txtVal = txtVal.substring(0, maxLength);         $("#" + ta).val(txtVal);     } } Explanation:   maxLength variable will hold the length. On document load we will be calling

LINQ: Dynamic Groupby, OrderBy, and Dynamic Aggregates.

How to dynamically achieve groupby, orderby and aggregates using LINQ ? huh.. what..? Is it is possible.. yes it is.. Here is the solution. Here are some of our helper methods: private static object GetPropertyValue(object obj, string propertyName)         {             return obj.GetType().GetProperty(propertyName).GetValue(obj, null);         } private static double GetPropertyValueDouble(object obj, string propertyName)         {             double ret = Convert.ToDouble(obj.GetType().GetProperty(propertyName).GetValue(obj, null));             return ret;         } Consider below sample example:  public class test     {         public string City { get; set; }         public string Name { get; set; }         public string State { get; set; }         public double Cost { get; set; }     }            List<test> xxx = new List<test>(){                             new test(){City="MNG",Name="Jhon",State="KNK",Cost=10},          

C#: How to dynamically invoke a method from a Class, when you know the ClassName and Method Name as string

How to invoke a method from a Class, when you know the ClassName and Method Name as string in C#. Here is the Solution. Consider below helper method: (change the namespace, version and public key tokens accordingly)  public static Type GetTypeFromClassName(string ClassName)         {             string ss = string.Format("Solutions.Common.{0}, Solutions.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3a6a3aa5a3aed1e3", ClassName);             Type typeYouWant = Type.GetType(ss);             if (typeYouWant != null)             {                 return typeYouWant;             }             return null;         } For explanation of above method, read my below post. Creating Object of a Class dynamically when you have name of the Class as string in C#? string ClassName="MyClass"; string MethodName="MyMethod"; string param1="IT"; Type ClassType = GetTypeFromClassName(ClassName); var myObj = Activator.CreateInstance(

Creating Object of a Class dynamically when you have name of the Class as string in C#?

How to create an object of a class dynamically when you have the name of class as string in c#. Here is the solution. First, get the type of the class. Below helper method Returns the Type of your class. Here Solutions.Common is the Namespace of your class. To get the Version and PublicKeyToken, go to run (window+r) , type assembly, search for your assembly , right click , properties and get these details.  public static Type GetTypeFromClassName(string ClassName)         {             string ss = string.Format("Solutions.Common.{0}, Solutions.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3a6a3aa5a3aed1e3", ClassName);             Type typeYouWant = Type.GetType(ss);             if (typeYouWant != null)             {                 return typeYouWant;             }             return null;         } So object of any class can be created by using above helper method as below: Type CustomClassType = Helpers.GetTypeFromStringFromCommon("Custo

Reading List and List Attributes of List using REST API SharePoint 2013

We can read a SharePoint List/Library and its all attributes by consuming the REST endpoint: http://server/_api/lists/getbytitle('listname') For example, below url retrieves all the attributes of the library (or list) Account . https://xxx.sharepoint.com/_api/lists/getbytitle(‘ Account’ ) Example JSON response (part of it) from the above REST call. Marked are some of the attributes required for us:   "AllowContentTypes": true,         "BaseTemplate": 101,         "BaseType": 1,         "ContentTypesEnabled": false,         "CrawlNonDefaultViews": false,         "Created": "2015-01-05T11:06:56Z",         "DefaultContentApprovalWorkflowId": "00000000-0000-0000-0000-000000000000",         "Description": "",         "Direction": "none",         "DocumentTemplateUrl": "/SalesForce Files/Forms/templat

Reading Fields for a List using REST API SharePoint 2013

We can read the all the fields in a SharePoint list by consuming the REST endpoint: http://server/_api/lists/getbytitle('listname') /fields for example, below url retrieves  all the fields in the library(or list) Accounts: https://xxx.sharepoint.com/_api/lists/getbytitle('Accounts')/fields Return value: (I have marked in yellow, some of the important  noticeable  properties). {     "d": {         "results": [             {                 {                 "__metadata": {                     "id": "https://xxx.sharepoint.com/_api/Web/Lists(guid'd5f89928-6c33-476d-a80c-54fcf0d57164')/Fields(guid'0953b0e6-65c1-41e8-98da-869f106b0669')",                     "uri": "https://xxx.sharepoint.com/_api/Web/Lists(guid'd5f89928-6c33-476d-a80c-54fcf0d57164')/Fields(guid'0953b0e6-65c1-41e8-98da-869f106b0669')",                     "type&q