• SharePoint Tips
  • BoostSolutions Products
  • SharePoint Tutorial
  • Events
BoostSolutions
SharePoint Tutorial, News and Products
You are here: Home ∼ Extend SharePoint Object Model with C# 3.0 Extension Methods

Extend SharePoint Object Model with C# 3.0 Extension Methods

Published by Claus on May 16, 2011
C# 3.0 and .Net Framework 3.5 in introduce several new features. One of them is extension methods. Extension methods enable you to “add” 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.
Here are my extension methods for “SPList” object.
using System;
using Microsoft.SharePoint;
namespace ConsoleApplication1.ExtensionMethods
{ 

// Extension methods must be defined in a static class.
public static class SPListExtension
{
// This is the extension method.
// The first parameter takes the "this" modifier.
public static string GetFullUrl(this SPList list)
{
return string.Format("{0}/{1}", list.ParentWeb.Url, list.RootFolder.Url);
}
public static bool HasView(this SPList list, string viewName)
{
if (string.IsNullOrEmpty(viewName))
{
return false;
}
foreach (SPView view in list.Views)
{
if (view.Title.Equals(viewName, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
return false;
}
}
}

 

Application with the extension methods
 

using System;
using ConsoleApplication1.ExtensionMethods;
using Microsoft.SharePoint;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Tasks"];
Console.WriteLine(string.Format("The full url of Tasks is: {0}", list.GetFullUrl()));// Call an extension mothod.
Console.WriteLine();
string viewName = "My Tasks";
Console.WriteLine("View Name: My Tasks");
Console.WriteLine(list.HasView(viewName)); // Call an extension mothod.
Console.WriteLine();
viewName = "abc 123";
Console.WriteLine("View Name: abc 123");
Console.WriteLine(SPListExtension.HasView(list, viewName)); // Try calling the extension method as a static method.
}
}
Console.Read();
}
}
}

 

You also can call the extension methods as static methods.
Output with the extension methods
Extension methods in C# 2.0
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 “Cannot define a new extension method because the compiler required type ‘System.Runtime.CompilerServices.ExtensionAttribute’ cannot be found”. Seems the compiler just needs to resolve ‘System.Runtime.CompilerServices.ExtensionAttribute’ to compile extension methods. So you can introduce your own ‘System.Runtime.CompilerServices.ExtensionAttribute’ class, then it worked correctly!
 

namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute
{
}
}

 

The brilliant workaround explained by Daniel Moth.
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.
Posted in SharePoint Tips and Tricks Tagged c#3.0, development, extension methods, SharePoint, SharePoint API, SharePoint Object Model, sharepointboost
← Previous Next →

Copyright © 2025 BoostSolutions. Email: inquiry@boostsolutions.com

Powered by WordPress and Live Wire.

Scroll