Chris
Chris

Reputation: 103

Is possible to inherit from SPWeb?

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?

Upvotes: 1

Views: 1209

Answers (5)

guyuming
guyuming

Reputation: 1

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

Upvotes: 0

kerray
kerray

Reputation: 567

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

Upvotes: 0

Rich Bennema
Rich Bennema

Reputation: 10335

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

Upvotes: 2

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65391

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

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245419

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.

Upvotes: 0

Related Questions