{"id":321,"date":"2011-05-16T18:18:44","date_gmt":"2011-05-16T10:18:44","guid":{"rendered":"http:\/\/www.sharepointboost.com\/blog\/?p=321"},"modified":"2023-07-31T11:41:02","modified_gmt":"2023-07-31T03:41:02","slug":"extend-sharepoint-object-model-with-c-3-0-extension-methods","status":"publish","type":"post","link":"https:\/\/www.boostsolutions.com\/blog\/extend-sharepoint-object-model-with-c-3-0-extension-methods\/","title":{"rendered":"Extend SharePoint Object Model with C# 3.0 Extension Methods"},"content":{"rendered":"<div>C# 3.0 and .Net Framework 3.5 in introduce several new features. One of them is extension methods. Extension methods enable you to &#8220;add&#8221; new methods to the existing types. Extension methods are a special kind of static methods, but they are called as same as the instance methods. In this blog I will show you how to extend SharePoint API by using extension methods.<\/div>\n<div style=\"padding-top: 20px;\">Here are my extension methods for &#8220;SPList&#8221; object.<\/div>\n<div style=\"padding-top: 10px;\">\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"561\" valign=\"top\"><code>using System;<br \/>\nusing Microsoft.SharePoint;<br \/>\nnamespace ConsoleApplication1.ExtensionMethods<br \/>\n{&nbsp;<\/p>\n<div style=\"padding-left: 20px;\">\/\/ Extension methods must be defined in a static class.<br \/>\npublic static class SPListExtension<br \/>\n{<\/div>\n<div style=\"padding-left: 40px;\">\/\/ This is the extension method.<br \/>\n\/\/ The first parameter takes the \"this\" modifier.<br \/>\npublic static string GetFullUrl(this SPList list)<br \/>\n{<\/div>\n<div style=\"padding-left: 60px;\">return string.Format(\"{0}\/{1}\", list.ParentWeb.Url, list.RootFolder.Url);<\/div>\n<div style=\"padding-left: 40px;\">}<br \/>\npublic static bool HasView(this SPList list, string viewName)<br \/>\n{<\/div>\n<div style=\"padding-left: 60px;\">if (string.IsNullOrEmpty(viewName))<br \/>\n{<\/div>\n<div style=\"padding-left: 80px;\">return false;<\/div>\n<div style=\"padding-left: 60px;\">}<br \/>\nforeach (SPView view in list.Views)<br \/>\n{<\/div>\n<div style=\"padding-left: 80px;\">if (view.Title.Equals(viewName, StringComparison.InvariantCultureIgnoreCase))<br \/>\n{<\/div>\n<div style=\"padding-left: 100px;\">return true;<\/div>\n<div style=\"padding-left: 80px;\">}<\/div>\n<div style=\"padding-left: 60px;\">}<br \/>\nreturn false;<\/div>\n<div style=\"padding-left: 40px;\">}<\/div>\n<div style=\"padding-left: 20px;\">}<\/div>\n<div>}<\/div>\n<p><\/code>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div style=\"padding-top: 10px;\"><!--more--><\/div>\n<div style=\"padding-top: 20px;\">Application with the extension methods<\/div>\n<div style=\"padding-top: 10px;\">\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"561\" valign=\"top\"><code>&nbsp;<\/p>\n<div>using System;<br \/>\nusing ConsoleApplication1.ExtensionMethods;<br \/>\nusing Microsoft.SharePoint;<br \/>\nnamespace ConsoleApplication1<br \/>\n{<\/div>\n<div style=\"padding-left: 20px;\">class Program<br \/>\n{<\/div>\n<div style=\"padding-left: 40px;\">static void Main(string[] args)<br \/>\n{<\/div>\n<div style=\"padding-left: 60px;\">using (SPSite site = new SPSite(\"http:\/\/localhost\"))<br \/>\n{<\/div>\n<div style=\"padding-left: 80px;\">using (SPWeb web = site.OpenWeb())<br \/>\n{<\/div>\n<div style=\"padding-left: 100px;\">SPList list = web.Lists[\"Tasks\"];<br \/>\nConsole.WriteLine(string.Format(\"The full url of Tasks is: {0}\", list.GetFullUrl()));\/\/ Call an extension mothod.<br \/>\nConsole.WriteLine();<br \/>\nstring viewName = \"My Tasks\";<br \/>\nConsole.WriteLine(\"View Name: My Tasks\");<br \/>\nConsole.WriteLine(list.HasView(viewName)); \/\/ Call an extension mothod.<br \/>\nConsole.WriteLine();<br \/>\nviewName = \"abc 123\";<br \/>\nConsole.WriteLine(\"View Name: abc 123\");<br \/>\nConsole.WriteLine(SPListExtension.HasView(list, viewName)); \/\/ Try calling the extension method as a static method.<\/div>\n<div style=\"padding-left: 80px;\">}<\/div>\n<div style=\"padding-left: 60px;\">}<br \/>\nConsole.Read();<\/div>\n<div style=\"padding-left: 40px;\">}<\/div>\n<div style=\"padding-left: 20px;\">}<\/div>\n<div>}<\/div>\n<p><\/code>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>You also can call the extension methods as static methods.<\/div>\n<\/div>\n<div style=\"padding-top: 20px;\">Output with the extension methods<\/div>\n<div style=\"padding-top: 10px;\"><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2011\/05\/Output11.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-470\" src=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2011\/05\/Output11.png\" alt=\"\" width=\"669\" height=\"214\" srcset=\"https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2011\/05\/Output11.png 669w, https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2011\/05\/Output11-300x95.png 300w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/a><\/div>\n<div style=\"padding-top: 10px;\"><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2011\/05\/Output1.png\"><\/a><\/div>\n<div><strong>Extension methods in C# 2.0<\/strong><\/div>\n<div>As I stated in the introduction to this article, the extension methods is a new feature of C# 3.0, and the SharePoint 2007 API is based on .NET Framework 2.0. If you try to use extension methods in a project targeting .NET Framework 2.0, you will get a compiler error when compiling your code, the error text is &#8220;Cannot define a new extension method because the compiler required type &#8216;System.Runtime.CompilerServices.ExtensionAttribute&#8217; cannot be found&#8221;. Seems the compiler just needs to resolve &#8216;System.Runtime.CompilerServices.ExtensionAttribute&#8217; to compile extension methods. So you can introduce your own &#8216;System.Runtime.CompilerServices.ExtensionAttribute&#8217; class, then it worked correctly!<\/div>\n<div style=\"padding-top: 10px;\">\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"561\" valign=\"top\"><code>&nbsp;<\/p>\n<div>namespace System.Runtime.CompilerServices<\/div>\n<div>{<\/div>\n<div style=\"padding-left: 20px;\">[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]<\/div>\n<div style=\"padding-left: 20px;\">public sealed class ExtensionAttribute : Attribute<\/div>\n<div style=\"padding-left: 20px;\">{<\/div>\n<div style=\"padding-left: 20px;\">}<\/div>\n<div>}<\/div>\n<p><\/code>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>The brilliant workaround explained by Daniel Moth.<\/div>\n<\/div>\n<div style=\"padding-top: 20px;\">The syntax with extension methods is much more streamline and a lot easier to understand. It easier for developers to approach the object model without having to divide their attention between the SharePoint API and custom class libraries.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>C# 3.0 and .Net Framework 3.5 in introduce several new features. One of them is extension methods. Extension methods enable you to &#8220;add&#8221; new methods to the existing types. Extension methods are a special kind of static methods, but they are called as same as the instance methods. In this blog I will show you [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[125],"tags":[62,64,63,486,65,66,27],"_links":{"self":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/321"}],"collection":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=321"}],"version-history":[{"count":38,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/321\/revisions"}],"predecessor-version":[{"id":525,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/321\/revisions\/525"}],"wp:attachment":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}