1

Is possible to inherit from SharePoint classes such like: SPWeb, SPList etc. or this classes are sealed? I couldn't find right answer.

Chris


Thanks for replys. Rich, you are right - the constructors are internal. So it means that I coudn't extend the functionality of these classes in any elegance way?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Chris
  • 103
  • 1
  • 8

5 Answers5

2

According to Reflector, SPWeb is not sealed in either 2007 or 2010.

2007:

[SharePointPermission(SecurityAction.LinkDemand, ObjectModel=true), 
SharePointPermission(SecurityAction.LinkDemand, ObjectModel=true), 
SharePointPermission(SecurityAction.LinkDemand, ObjectModel=true), 
SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel=true)]
public class SPWeb : IDisposable, ISecurableObject

2010:

[SubsetCallableType, 
ClientCallableType(Name="Web", ServerTypeId="{A489ADD2-5D3A-4de8-9445-49259462DCEB}", FactoryType=typeof(SPObjectFactory), ObjectIdentityPropertyName="CanonicalId"), 
SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel=true), 
SharePointPermission(SecurityAction.LinkDemand, ObjectModel=true), 
SharePointPermission(SecurityAction.LinkDemand, ObjectModel=true), 
SharePointPermission(SecurityAction.LinkDemand, ObjectModel=true)]
public class SPWeb : SPSecurableObject, IDisposable

However, in both versions, the class only has internal constructors, so while Visual Studio will let you try to inherit from the class, it will not compile:

The type 'Microsoft.SharePoint.SPWeb' has no constructors defined

Rich Bennema
  • 10,295
  • 4
  • 37
  • 58
0

I think a lot of SharePoint server object model programmer had came to this issue.

At first, I simply start with a helper class as a wrapper for SPWeb, which use Managed Navigation.

As the requirement became more complicate, I have to deal with more than one type of SPWeb. So, I refacted the code, create a Factory class to instantiate typed SPSite and SPWeb. Which bind a SPWeb with Managed metadata term, and store the type information in both SPWeb property and Term custom property.

I would like to help Microsoft to find out if this is a design that make sense. And if it worthwhile for Microsoft to start a opensource project for this. Since sometimes programmers have to focus on business logic, don't want to implement Factory, Abstract Factory again and again.

https://social.msdn.microsoft.com/Forums/office/en-US/62c1355f-0b71-49b7-967a-648830bd6dfa/creating-a-factory-class-with-sharepoint-server-side-api-to-instantiate-a-wrapper-class-around#62c1355f-0b71-49b7-967a-648830bd6dfa

guyuming
  • 1
  • 1
0

According to their MSDN pages, the classes are not sealed:

SPWeb Class

SPList Class

Even though you may be able to inherit from those classes, I don't see the point since you can't force SharePoint to use them internally.

It may make more sense to provide your added functionality via Extension Methods rather than actually inheriting from the base classes.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

SPWeb and SPList are sealed in SharePoint 2007, see: http://blogs.msdn.com/b/francischeung/archive/2008/08/22/unit-testing-sharepoint-2007-applications.aspx

But they are not sealed in SharePoint 2010, see: http://my.safaribooksonline.com/9781435456457/365

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • Interesting. If you look at the MSDN docs for those classes, you can clearly see that the sealed keyword is not mentioned (as it is for other sealed classes on MSDN). – Justin Niessner Oct 14 '10 at 12:58
  • @justin, it was a difference between 2007 and 2010, I have updated the answer – Shiraz Bhaiji Oct 14 '10 at 13:00
  • But even if you switch the MSDN Documentation to WSS 3.0 (which was the WSS used in SharePoint 2007), they still do not appear sealed. – Justin Niessner Oct 14 '10 at 13:01
0

Of course they can be extended, simply using Extension Methods - http://msdn.microsoft.com/en-us/library/bb383977.aspx

As an example I'm trying to push a bit, you can take a look at ItemTools, or ListTools or other source files in https://github.com/kerray/NAVERTICA-SPTools

kerray
  • 557
  • 5
  • 13