SlideShare a Scribd company logo
1 of 27
Download to read offline
Alexey Sadomov, PhD, Sharepoint MVP
http://sadomovalex.blogspot.com/
   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
   Example: trace info about thread principal and
    currently logged Sharepoint user
   Example: trace info about thread principal and
    currently logged Sharepoint user
   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
   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
   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
   Real life example:
     Create instance of SPSite in
        SPSecurity.RunWithElevatedPrivileges() method
       Open SPWeb
       Code is executed under SHAREPOINTSystem account (app pool
        identity)
       Call standard API which in turns uses SPContext.Current.Site or
        SPContext.Current.Web
       Result: access denied page
   Analyzing code of SPContext:
   Analyzing code of SPContext:
   Use knowledge of Sharepoint code and reflection for solving the
    problem:
   ReflectionHelper code:
   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
   Example: dynamic method for calculating factorial
 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
   Where problem is coming from? RadHtmlListField class:
   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
   Example: create dynamic method for skipping one tier in virtual members calls
   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/
   Example of usage:




   Direct translator:
   Camlex 3.0 Reverse Engineering – creates expression trees based on strings




   Reverse Engineering translator:
   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
   Add conditions to existing string queries using lambda expressions:
   Expand ViewFields, GroupBy, OrderBy:
 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
   Example: enumerate sub-sites using C# code in PowerShell
   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

More Related Content

What's hot (18)

Microsoft C# programming basics
Microsoft C# programming basics  Microsoft C# programming basics
Microsoft C# programming basics
 
C sharp
C sharpC sharp
C sharp
 
Symfony 3.4
Symfony 3.4Symfony 3.4
Symfony 3.4
 
C programming
C programming C programming
C programming
 
Functional Groovy - Confess
Functional Groovy - ConfessFunctional Groovy - Confess
Functional Groovy - Confess
 
Revealing C# 5
Revealing C# 5Revealing C# 5
Revealing C# 5
 
Creating Windows Runtime Components
Creating Windows Runtime Components Creating Windows Runtime Components
Creating Windows Runtime Components
 
C++
C++C++
C++
 
Asp.net and .Net Framework ppt presentation
Asp.net and .Net Framework ppt presentationAsp.net and .Net Framework ppt presentation
Asp.net and .Net Framework ppt presentation
 
Overview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaOverview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company india
 
Introduction of c# day3
Introduction of c# day3Introduction of c# day3
Introduction of c# day3
 
Introduction to C# 3.0
Introduction to C# 3.0Introduction to C# 3.0
Introduction to C# 3.0
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
Intro To Asp Net And Web Forms
Intro To Asp Net And Web FormsIntro To Asp Net And Web Forms
Intro To Asp Net And Web Forms
 
JS digest. January 2018
JS digest. January 2018 JS digest. January 2018
JS digest. January 2018
 
ASP.NET Basics
ASP.NET Basics ASP.NET Basics
ASP.NET Basics
 
C++ ch1
C++ ch1C++ ch1
C++ ch1
 
Be project ppt asp.net
Be project ppt asp.netBe project ppt asp.net
Be project ppt asp.net
 

Viewers also liked

Использование деревьев выражений .Net для создания транслятора на языке C#
Использование деревьев выражений .Net для создания транслятора на языке C#Использование деревьев выражений .Net для создания транслятора на языке C#
Использование деревьев выражений .Net для создания транслятора на языке C#sadomovalex
 
PLDI 2010: Safe to the Last Instruction
PLDI 2010: Safe to the Last InstructionPLDI 2010: Safe to the Last Instruction
PLDI 2010: Safe to the Last Instructionjxyz
 
From Commodore 64 to the Cloud — Lessons from 30 years of programming
From Commodore 64 to the Cloud — Lessons from 30 years of programmingFrom Commodore 64 to the Cloud — Lessons from 30 years of programming
From Commodore 64 to the Cloud — Lessons from 30 years of programmingSteven Shaw
 
Advanced Components on Top of L4Re
Advanced Components on Top of L4ReAdvanced Components on Top of L4Re
Advanced Components on Top of L4ReVasily Sartakov
 
Caml intro
Caml introCaml intro
Caml introzan
 

Viewers also liked (6)

Использование деревьев выражений .Net для создания транслятора на языке C#
Использование деревьев выражений .Net для создания транслятора на языке C#Использование деревьев выражений .Net для создания транслятора на языке C#
Использование деревьев выражений .Net для создания транслятора на языке C#
 
PLDI 2010: Safe to the Last Instruction
PLDI 2010: Safe to the Last InstructionPLDI 2010: Safe to the Last Instruction
PLDI 2010: Safe to the Last Instruction
 
From Commodore 64 to the Cloud — Lessons from 30 years of programming
From Commodore 64 to the Cloud — Lessons from 30 years of programmingFrom Commodore 64 to the Cloud — Lessons from 30 years of programming
From Commodore 64 to the Cloud — Lessons from 30 years of programming
 
Advanced Components on Top of L4Re
Advanced Components on Top of L4ReAdvanced Components on Top of L4Re
Advanced Components on Top of L4Re
 
OS Mélange
OS MélangeOS Mélange
OS Mélange
 
Caml intro
Caml introCaml intro
Caml intro
 

Similar to Using advanced C# features in Sharepoint development

Monorail presentation at WebDevelopersCommunity, Feb 1, 2009
Monorail presentation at WebDevelopersCommunity, Feb 1, 2009Monorail presentation at WebDevelopersCommunity, Feb 1, 2009
Monorail presentation at WebDevelopersCommunity, Feb 1, 2009ken.egozi
 
Lunch and learn as3_frameworks
Lunch and learn as3_frameworksLunch and learn as3_frameworks
Lunch and learn as3_frameworksYuri Visser
 
Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with PythonBrian Lyttle
 
Dot Net Fundamentals
Dot Net FundamentalsDot Net Fundamentals
Dot Net FundamentalsLiquidHub
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteorSapna Upreti
 
Asp.net With mvc handson
Asp.net With mvc handsonAsp.net With mvc handson
Asp.net With mvc handsonPrashant Kumar
 
Java programming concept
Java programming conceptJava programming concept
Java programming conceptSanjay Gunjal
 
Getting Started with Zend Framework
Getting Started with Zend FrameworkGetting Started with Zend Framework
Getting Started with Zend FrameworkJuan Antonio
 
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
Effizientere WordPress-Plugin-Entwicklung mit SoftwaretestsEffizientere WordPress-Plugin-Entwicklung mit Softwaretests
Effizientere WordPress-Plugin-Entwicklung mit SoftwaretestsDECK36
 
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPMCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPAli Shah
 
Introducing eclipse rap
Introducing eclipse rapIntroducing eclipse rap
Introducing eclipse rapOwen Ou
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]GDSC UofT Mississauga
 
Introduction To Code Igniter
Introduction To Code IgniterIntroduction To Code Igniter
Introduction To Code IgniterAmzad Hossain
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answersKrishnaov
 

Similar to Using advanced C# features in Sharepoint development (20)

Android crash course
Android crash courseAndroid crash course
Android crash course
 
Monorail presentation at WebDevelopersCommunity, Feb 1, 2009
Monorail presentation at WebDevelopersCommunity, Feb 1, 2009Monorail presentation at WebDevelopersCommunity, Feb 1, 2009
Monorail presentation at WebDevelopersCommunity, Feb 1, 2009
 
Lunch and learn as3_frameworks
Lunch and learn as3_frameworksLunch and learn as3_frameworks
Lunch and learn as3_frameworks
 
Introduction to Google App Engine with Python
Introduction to Google App Engine with PythonIntroduction to Google App Engine with Python
Introduction to Google App Engine with Python
 
Dot Net Fundamentals
Dot Net FundamentalsDot Net Fundamentals
Dot Net Fundamentals
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteor
 
Asp.net With mvc handson
Asp.net With mvc handsonAsp.net With mvc handson
Asp.net With mvc handson
 
Java programming concept
Java programming conceptJava programming concept
Java programming concept
 
Getting Started with Zend Framework
Getting Started with Zend FrameworkGetting Started with Zend Framework
Getting Started with Zend Framework
 
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
Effizientere WordPress-Plugin-Entwicklung mit SoftwaretestsEffizientere WordPress-Plugin-Entwicklung mit Softwaretests
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
 
Asp.Net MVC Intro
Asp.Net MVC IntroAsp.Net MVC Intro
Asp.Net MVC Intro
 
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPMCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
 
As pnet
As pnetAs pnet
As pnet
 
Introducing eclipse rap
Introducing eclipse rapIntroducing eclipse rap
Introducing eclipse rap
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
C# tutorial
C# tutorialC# tutorial
C# tutorial
 
Introduction To Code Igniter
Introduction To Code IgniterIntroduction To Code Igniter
Introduction To Code Igniter
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 
csharp.docx
csharp.docxcsharp.docx
csharp.docx
 
React hooks
React hooksReact hooks
React hooks
 

Using advanced C# features in Sharepoint development

  • 1. Alexey Sadomov, PhD, Sharepoint MVP http://sadomovalex.blogspot.com/
  • 2. 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
  • 3. Example: trace info about thread principal and currently logged Sharepoint user
  • 4. Example: trace info about thread principal and currently logged Sharepoint user
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. Real life example:  Create instance of SPSite in SPSecurity.RunWithElevatedPrivileges() method  Open SPWeb  Code is executed under SHAREPOINTSystem account (app pool identity)  Call standard API which in turns uses SPContext.Current.Site or SPContext.Current.Web  Result: access denied page
  • 9. Analyzing code of SPContext:
  • 10. Analyzing code of SPContext:
  • 11. Use knowledge of Sharepoint code and reflection for solving the problem:
  • 12. ReflectionHelper code:
  • 13. 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
  • 14. Example: dynamic method for calculating factorial
  • 15.  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
  • 16. Where problem is coming from? RadHtmlListField class:
  • 17. 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
  • 18. Example: create dynamic method for skipping one tier in virtual members calls
  • 19. 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/
  • 20. Example of usage:  Direct translator:
  • 21. Camlex 3.0 Reverse Engineering – creates expression trees based on strings  Reverse Engineering translator:
  • 22. 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
  • 23. Add conditions to existing string queries using lambda expressions:
  • 24. Expand ViewFields, GroupBy, OrderBy:
  • 25.  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
  • 26. Example: enumerate sub-sites using C# code in PowerShell
  • 27. 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