using advanced c# features in sharepoint development
TRANSCRIPT
USING OF ADVANCED C# FEATURES IN SHAREPOINT
DEVELOPMENT
Alexey Sadomov, PhD, Sharepoint MVPhttp://sadomovalex.blogspot.com/
Episode 1. Application _layouts pages with embedded server code
Efficient way to troubleshoot issues, which are not reproduced on dev environment
Page is opened in context of living site, e.g. http://example.com/_layouts/test.aspx
Do not require updates of wsp packages Do not require installations of dlls into the GAC Can be easily removed from production after
troubleshooting without leaving security holes C# 2.0 is available by default for server scripts
Episode 1. Application _layouts pages with embedded server code
Example: trace info about thread principal and currently logged Sharepoint user
Episode 1. Application _layouts pages with embedded server code
Example: trace info about thread principal and currently logged Sharepoint user
Episode 1. Application _layouts pages with embedded server code
Enable features of C# 3.0 in _layouts pages via web.config:
All features of C# 3.0 become available (e.g. lambdas, query expressions, etc.): see Overview of C# 3.0
Episode 1. Application _layouts pages with embedded server code
Sharepoint 2013 preview: Web sites are created with .Net 4.0 app pools Default compiler is set to version 4.0 It is possible to use features of C# 4 (e.g. dynamic
types) in embedded server scripts without additional configurations
Episode 2. Reflection: the last defense line
Use when can’t solve problem with standard tools
Requires preliminary investigation of Sharepoint code: JetBrains dotPeek Telerik JustCompile Reflector
Where to search Sharepoint dlls: GAC 14/ISAPI inetpub\...\mywebapp\_app_bin
Episode 2. Reflection: the last defense line
Real life example: Create instance of SPSite in
SPSecurity.RunWithElevatedPrivileges() method Open SPWeb Code is executed under SHAREPOINT\System account (app
pool identity) Call standard API which in turns uses
SPContext.Current.Site or SPContext.Current.Web Result: access denied page
Episode 2. Reflection: the last defense line
Analyzing code of SPContext:
Episode 2. Reflection: the last defense line
Analyzing code of SPContext:
Episode 2. Reflection: the last defense line
Use knowledge of Sharepoint code and reflection for solving the problem:
Episode 2. Reflection: the last defense line
ReflectionHelper code:
Episode 3. Dynamic methods: the dark side
Can be used for generating and executing a method at run time, without having to generate a dynamic assembly and a dynamic type to contain the method
The executable code created by the just-in-time (JIT) compiler is reclaimed when the DynamicMethod object is reclaimed
If the dynamic method is associated with specific type, it has access to all members of the type, regardless of access level
Dynamic methods and their parameters do not have to be named
Allows to avoid some limitations of C#: e.g. although dynamic methods are static methods the relaxed rules for delegate binding allow a dynamic method to be bound to an object, so that it acts like an instance method when called using that delegate instance
Episode 3. Dynamic methods: the dark side
Example: dynamic method for calculating factorial
Episode 3. Dynamic methods: the dark side
How it can be used in Sharepoint? Example: use Telerik RadEditor in all sub sites in web
application without feature activations RadEditorFeature – overrides rendering template for RichTextField
with Telerik control RadEditorFeatureIE – “flag” feature, depends on RadEditorFeature
Technically it is enough to activate RadEditorFeature once For new sites:
Activate features manually Use feature stappling for OTB site definitions Modify onet.xml file for custom site definitions
Episode 3. Dynamic methods: the dark side
Where problem is coming from? RadHtmlListField class:
Episode 3. Dynamic methods: the dark side
Solution: Define new class inheritor of RadHtmlListField Define OnLoad() by the same way as in RadHtmlListField using reflection Always return true from ShowEditorOnPage() method
Inheritance chain: AllBrowsersHtmlListField –> RadHtmlListField –> MOSSRadEditor
The problem: how to call MOSSRadEditor.OnLoad() from AllBrowsersHtmlListField.OnLoad() without caling RadHtmlListField .OnLoad()? I.e. we need to skip one tier in the inheritance chain and call virtual method
OnLoad(), but not from base class, but from base of base class Answer: no how in standard C# Can dynamic method help here? Yes
Episode 3. Dynamic methods: the dark side
Example: create dynamic method for skipping one tier in virtual members calls
Episode 4. Code generation, expression trees, CAML reverse engineering, Camlex online
Camlex – open source project which simplifies creating of CAML queries for SharePoint by using expression trees
Uses expression trees and lambda expressions for generating CAML Simplifies creation of dynamic CAML queries when conditions are
calculated in runtime Doesn’t require knowledge of CAML from developers Supports native .Net types and Sharepoint-specific types http://camlex.codeplex.com/
Episode 4. Code generation, expression trees, CAML reverse engineering, Camlex online
Example of usage:
Direct translator:
Episode 4. Code generation, expression trees, CAML reverse engineering, Camlex online
Camlex 3.0 Reverse Engineering – creates expression trees based on strings
Reverse Engineering translator:
Episode 4. Code generation, expression trees, CAML reverse engineering, Camlex online
Camlex Re + ExpressionToCode = Camlex Online: http://camlex-online.org/
Simplifies usage of Camlex for developers who didn’t have experience with it yet
Quick refactoring of existing hard-coded CAML queries to Camlex Fully reversible CAML query schema opens many interesting
applications in future Example of combining of several non-trivial OSS projects
Episode 4. Code generation, expression trees, CAML reverse engineering, Camlex online
Add conditions to existing string queries using lambda expressions:
Episode 4. Code generation, expression trees, CAML reverse engineering, Camlex online
Expand ViewFields, GroupBy, OrderBy:
Episode 5. Write C# code in PowerShell
Built-in to OS, doesn’t need VS for writing and running C# programs
May be useful: if you already have a code which want to reuse have existing C# code which is hard to move to PowerShell for developers who know C#, but only started work with PowerShell if you want to run quick test, but don’t want to run VS and compile
dll C# 3.0 can be used: -Language CSharpVersion3 Can specify existing assemblies or source code files
Episode 5. Write C# code in powershell
Example: enumerate sub-sites using C# code in PowerShell
Additional materials http://sadomovalex.blogspot.com/ Use C# 3.0 features in application layout pages in Sharepoint:
http://sadomovalex.blogspot.fi/2010/12/use-c-30-features-in-application-layout.html
Use Telerik Rad Editor Lite without features activation: http://sadomovalex.blogspot.fi/2011/12/use-telerik-rad-editor-lite-without.html
Generating Dynamic Methods with Expression Trees in Visual Studio 2010: http://blogs.msdn.com/b/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx
Camlex project site: http://camlex.codeplex.com/ Camlex online: http://camlex-online.org/ ExpressionToCode: http://code.google.com/p/expressiontocode/ Materials from followings sites are used for presentation:
http://newswallpapers1.blogspot.com, http://arts-wallpapers.com, http://www.fanpop.com, http://wallpaperswide.com, http://starwars.wikia.com