Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
JSRequest, Get parameters from querystring with javascript in SharePoint

Provides method to parse query string, filename, and pathname from URL

  1. // Initialize first
  2. JSRequest.EnsureSetup();
  3.  
  4. // Get the current file name
  5. JSRequest.FileName: "qa.aspx"
  6. // Get the current path name
  7. JSRequest.PathName: "/sites/BruceDev/SitePages/qa.aspx"
  8. // Get request query string parameter
  9. JSRequest.QueryString["dy"];
JSRequest
  1. JSRequest = {
  2.     QueryString: null,
  3.     FileName: null,
  4.     PathName: null,
  5.     EnsureSetup: function () {
  6.         if(JSRequest.QueryString != null) return;
  7.         JSRequest.QueryString = new Array();
  8.         var queryString = window.location.search.substring(1);
  9.         var pairs = queryString.split("&");
  10.         for(var i = 0; i < pairs.length; i++) {
  11.             var p = pairs[i].indexOf("=");
  12.             if(> -1) {
  13.                 var key = pairs[i].substring(0, p);
  14.                 var value = pairs[i].substring(+ 1);
  15.                 JSRequest.QueryString[key] = value;
  16.             }
  17.         }
  18.         var path = JSRequest.PathName = window.location.pathname;
  19.         var p = path.lastIndexOf("/");
  20.         if(> -1) {
  21.             JSRequest.FileName = path.substring(+ 1);
  22.         } else {
  23.             JSRequest.PageName = path;
  24.         }
  25.     }
  26. };
Fix for SharePoint 2010 scrolling problems for Chrome
Execute on page loaded
  1. function bodyOnLoadWrapperForChrome() {
  2.     //verify we have finished loading init.js
  3.     if (typeof (_spBodyOnLoadWrapper) != 'undefined') {
  4.         //verify we have not already initialized the onload wrapper
  5.         if (typeof (_spBodyOnloadCalled) == 'undefined' || !_spBodyOnloadCalled) {
  6.             //initialize onload functions
  7.             _spBodyOnLoadWrapper();
  8.         }
  9.     }
  10.     else {
  11.         //wait for 10ms and try again if init.js has not been loaded
  12.         setTimeout(bodyOnLoadWrapperForChrome, 100);
  13.     }
  14. }
CSS alpha
  1. .alpha50 {
  2.     filter: alpha(Opacity=50, Style=0); /*for ie*/
  3.     -moz-opacity: 0.5; /* Moz + FF */
  4.     opacity: 0.5; /* CSS3的標準語法,FOR支援CSS3的瀏覽器(FF 1.5)*/
  5. }
Linq: Loop extension
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace System.Linq
  6. {
  7.     public static class GenericEachExtension
  8.     {
  9.         public static IEnumerable<T> Each<T>(this IEnumerable<T> source, Action<T> action)
  10.         {
  11.             foreach (T t in source)
  12.             {
  13.                 action.Invoke(t);
  14.             }
  15.             return source;
  16.         }
  17.  
  18.         public static IEnumerable<T> Each<T>(this IEnumerable<T> source, Func<T, bool> action)
  19.         {
  20.             foreach (T t in source)
  21.             {
  22.                 if (!action.Invoke(t)) break;
  23.             }
  24.             return source;
  25.         }
  26.  
  27.         public static IEnumerable<T> Each<T>(this IEnumerable<T> source, Action<T, int> action)
  28.         {
  29.             int i = 0;
  30.             foreach (T t in source)
  31.             {
  32.                 action.Invoke(t, i);
  33.                 i++;
  34.             }
  35.             return source;
  36.         }
  37.  
  38.         public static IEnumerable<T> Each<T>(this IEnumerable<T> source, Func<T, int, bool> action)
  39.         {
  40.             int i = 0;
  41.             foreach (T t in source)
  42.             {
  43.                 if (!action.Invoke(t, i)) break;
  44.                 i++;
  45.             }
  46.             return source;
  47.         }
  48.     }
  49. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace System.Collections
  7. {
  8.     public static class EachExtension
  9.     {
  10.         public static IEnumerable Each<T>(this IEnumerable source, Action<T> action)
  11.         {
  12.             foreach (T t in source)
  13.             {
  14.                 action.Invoke(t);
  15.             }
  16.             return source;
  17.         }
  18.  
  19.         public static IEnumerable Each<T>(this IEnumerable source, Func<T, bool> action)
  20.         {
  21.             foreach (T t in source)
  22.             {
  23.                 if (!action.Invoke(t)) break;
  24.             }
  25.             return source;
  26.         }
  27.  
  28.         public static IEnumerable Each<T>(this IEnumerable source, Action<T, int> action)
  29.         {
  30.             int i = 0;
  31.             foreach (T t in source)
  32.             {
  33.                 action.Invoke(t, i);
  34.                 i++;
  35.             }
  36.             return source;
  37.         }
  38.  
  39.         public static IEnumerable Each<T>(this IEnumerable source, Func<T, int, bool> action)
  40.         {
  41.             int i = 0;
  42.             foreach (T t in source)
  43.             {
  44.                 if (!action.Invoke(t, i)) break;
  45.                 i++;
  46.             }
  47.             return source;
  48.         }
  49.  
  50.         public static IEnumerable Each(this IEnumerable source, Action<object> action)
  51.         {
  52.             foreach (object t in source)
  53.             {
  54.                 action.Invoke(t);
  55.             }
  56.             return source;
  57.         }
  58.  
  59.         public static IEnumerable Each(this IEnumerable source, Func<object, bool> action)
  60.         {
  61.             foreach (object t in source)
  62.             {
  63.                 if (!action.Invoke(t)) break;
  64.             }
  65.             return source;
  66.         }
  67.  
  68.         public static IEnumerable Each(this IEnumerable source, Action<object, int> action)
  69.         {
  70.             int i = 0;
  71.             foreach (object t in source)
  72.             {
  73.                 action.Invoke(t, i);
  74.                 i++;
  75.             }
  76.             return source;
  77.         }
  78.  
  79.         public static IEnumerable Each(this IEnumerable source, Func<object, int, bool> action)
  80.         {
  81.             int i = 0;
  82.             foreach (object t in source)
  83.             {
  84.                 if (!action.Invoke(t, i)) break;
  85.                 i++;
  86.             }
  87.             return source;
  88.         }
  89.     }
  90. }
executeOrDelayUntilScriptLoaded
Executes the specified function if the file containing it is loaded; otherwise, adds it to the pending job queue.
ExecuteOrDelayUntilScriptLoaded(func, depScriptFileName)
or
SP.SOD.executeOrDelayUntilScriptLoaded(func, depScriptFileName)
  1. <script type="text/javascript">
  2.     ExecuteOrDelayUntilScriptLoaded(function() {
  3.         alert('sp.js loaded');
  4.     }, 'sp.js');
  5.  
  6.     SP.SOD.executeOrDelayUntilScriptLoaded(function() {
  7.         alert('jquery.js loaded');
  8.     }, 'jquery.js');
  9. </script>
Page load event handler on sharepoint
  1. <script type="text/javascript">
  2.     _spBodyOnLoadFunctionNames.push("onPageLoadAction");
  3.  
  4.     function onPageLoadAction() {
  5.         // code here
  6.     }
  7. </script>
new operator/modifier/constraint

new Operator Used to create objects and invoke constructors.

  1. // Create objects and invoke constructors
  2. Class1 obj  = new Class1();
  3.  
  4. // Create instances of anonymous types
  5. var employee = new { EmpID = 3, Name = "Bruce", Gender = Gender.Male};
  6.  
  7. // invoke the default constructor for value types
  8. int i = new int();
  9.  
  10. // invoke the default constructor for value types with default value
  11. int i = 0;

new Modifier Used to hide an inherited member from a base class member.

  1. class ParentClass
  2. {
  3.     public int x = 5;
  4.     public void Work()
  5.     {
  6.         for (int i = 0; i < x; i++) Console.WriteLine("Parent work : " + i);
  7.     }
  8.     public decimal Caculate()
  9.     {
  10.         return x + x;
  11.     }
  12. }
  13.  
  14. class ChildClass : ParentClass
  15. {
  16.     public new int x;
  17.     public new void Work()
  18.     {
  19.         for (int i = 0; i < x; i++) Console.WriteLine("Child work : " + i);
  20.     }
  21.     public new decimal Caculate()
  22.     {
  23.         return base.+ this.x;
  24.     }
  25. }
  26.  
  27. class Program
  28. {
  29.     static void Main(string[] args)
  30.     {
  31.         ParentClass p = new ParentClass();
  32.         p.Work();
  33.         // Parent work : 0
  34.         // Parent work : 1
  35.         // Parent work : 2
  36.         // Parent work : 3
  37.         // Parent work : 4
  38.         Console.WriteLine(p.Caculate());
  39.         // 10
  40.  
  41.         Console.WriteLine("================");
  42.  
  43.         ChildClass c = new ChildClass() { x = 3 };
  44.         c.Work();
  45.         // Child work : 0
  46.         // Child work : 1
  47.         // Child work : 2
  48.         Console.WriteLine(c.Caculate());
  49.         // 8
  50.     }
  51. }

new Constraint Used to restrict types that might be used as arguments for a type parameter in a generic declaration.

  1. class MyCollection<T> where T : new()
  2. {
  3.     // Type T must have a public parameterless constructor
  4. }
  5.  
  6. class MyCollection2<T> : IEnumerable<T> where T : IDisposable, new()
  7. {
  8.     // Type T implements IDisposable and must have a public parameterless constructor,
  9.     // the new() constraint must at last
  10. }
Redirect webpart in sanboxed solution
  1. using System;
  2. using System.ComponentModel;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using System.Web.UI.WebControls.WebParts;
  7. using Microsoft.SharePoint;
  8. using Microsoft.SharePoint.WebControls;
  9.  
  10. namespace MyWebpart
  11. {
  12.     [ToolboxItemAttribute(false)]
  13.     public class ConsentsApprover : WebPart
  14.     {
  15.         protected override void CreateChildControls()
  16.         {
  17.             Controls.Add(new Literal()
  18.             {
  19.                 Text = @"<script type=""text/javascript"">location='your_redirect_url';</script>"
  20.             });
  21.         }
  22.     }
  23. }