Creating List Instance: Object Reference not set to an instance of an object


Introduction

This one is a bit of an old issue but some of my posts are just to remind me of a time that I got stuck in the past. Also if it helps someone else then great.

It is possible to create a feature which creates an instance of a SharePoint List when its activated. However, sometimes you only want a list to be created on the Root Web of a site collection.

Issue

Well the XML for specifying a ListInstance as a feature has an attribute called RootWebOnly which I thought I could use to only allow the ListInstance to be created on the RootWeb.

Unfortunately when you do that you get a message like the following in your SharePoint ULS Logs when the feature is activated:-

Feature Activation: Threw an exception, attempting to roll back. Feature ‘FeatureName’(ID: ‘xxxxxxxx-xxxx-xxxx-xxxxxxxx). Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.SharePoint.SPListInstanceElement.ElementActivated(SPFeaturePropertyCollection props, SPSqlCommand sqlcmdAppendOnly, SPWebApplication webApp, SPSite site, SPWeb web, Boolean fForce)
at Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionListInstances(SPFeaturePropertyCollection props, SPSite site, SPWeb web, Boolean fForce)
at Microsoft.SharePoint.Administration.SPElementDefinitionCollection.ProvisionElements(SPFeaturePropertyCollection props, SPWebApplication webapp, SPSite site, SPWeb web, Boolean fForce)
at Microsoft.SharePoint.SPFeature.ProvisionElements(SPFeaturePropertyCollection props, SPWebApplication webapp, SPSite site, SPWeb web, Boolean fForce)
at Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boolean fForce)

The offending Feature ListInstance element is below:-

 1: <?xmlversion="1.0" encoding="utf-8"?>
 2: <Elements
 3: xmlns="http://schemas.microsoft.com/sharepoint/">
 4: <ListInstance
 5: Id="AListId"
 6: Title="A List"
 7: Description="A List 
 8: Description”
 9: Url="Lists/ListUrlHere"
 10: OnQuickLaunch="FALSE"
 11: RootWebOnly=”TRUE”
 12: FeatureId="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"
 13: TemplateType="45100"></ListInstance>
 14: </Elements>

Solution

To fix the feature activation remove the RootWebOnly=”TRUE” from your ListInstance element manifest file and instead move the RootWebOnly=”TRUE” into your List’s schema.xml.

To ensure that the list is only provisioned at the root web of the site collection change the Feature.xml to use Scope=”Site”

Update:

Thanks to Dennis who picked up my mistake in this post which stated that the RootWebOnly attribute should be moved to the feature.xml.

Issues with Custom List Schema and Editing Field Settings (Cannot Perform this Action)


>

Why is it that when you have a deadline looming then you have a problem which takes an age to fix?!

Well I hope that if you have the problem that I had then this post will help you fix it a bit more quickly!

Problem

The problem occurred within a custom list definition which held a lookup field. When the field settings were adjusted, using Settings->List Settings, modifying a field and clicking ok, an error message would be displayed.

“Cannot Perform this Action”

This error message fills me with dread as you have very little information to help you discover the problem.

Some background information, the field was in a custom list created from the custom list feature found in the SharePoint HIVE.

The custom list schema was updated with a unique content type and some additional fields were added. This content type was a list content type which was derived from the Item Content Type and had the ID: 0x0100[GUID]. The content type was stored inside a folder /Folder/Contacts.

Solution

The solution was found by examining other custom list schemas that I have created in the past. The difference was that mine was using a Folder and it didnt have the tag:-

<ContentTypeRef ID=”0x0120″ />

So I added this ContentTypeRef, which is the reference for the Folder content type within the <MetaData><ContentTypes> element at the top of the schema.xml file.

A redeployment of the feature with updated schema.xml updated the schema.xml file in the 12 HIVE and the issue was fixed, fields could now be edited and updated.

I hope this helps save someone a few hours!

Cheers

Simon

Error when creating custom list from New.aspx


>

Introduction

Anyone who’s been developing SharePoint will I am sure have had problems building custom list schemas. However, this one I hadn’t seen before.

The Problem

A custom site definition had been built which activated a feature which created an instance of a custom list for holding event list items.

When the site was created the events list was created perfectly, no problems.

However, when the list is created through the GUI, by clicking on Site Actions->View All Site Content and clicking Create and choosing the custom list type.

This brings up the new.aspx form, once the title and description is filled out and the create button clicked then the following exception was thrown :-

Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION)) at Microsoft.SharePoint.Library.SPRequestInternalClass.CreateListFromFormPost(String bstrUrl, String& pbstrGuid, String& pbstrNextUrl)
at Microsoft.SharePoint.Library.SPRequest.CreateListFromFormPost(String bstrUrl, String& pbstrGuid, String& pbstrNextUrl)

Solution

So the problem is something to do with the list schema.xml file. This schema.xml file is found in the feature directory. I use the following directory structure:-

12HIVE\Templates\Features\ListFeatureName\ListName\schema.xml

Looking at the error, the stack trace mentions the function, CreateListFromFormPost. I thought the issue was due to a custom NewForm page that we were using.

I could not find anything that would fix the issue, even replacing all the custom xml relating to the NewForm element.

However, the schema.xml was missing an attribute, this was the Url=”” attribute part of the <List> element.

This issue was found by comparing the schema.xml file with an out of the box schema.xml. As soon as the Url attribute was provided (even if the attribute was blank) then the list was created properly.

To recap the section of the schema that needed to updated was:-

<List xmlns:ows=”Microsoft SharePoint” Title=”Events” DisableAttachments=”TRUE” FolderCreation=”FALSE” Url=”Lists/Events” EnableContentTypes=”TRUE” BaseType=”0″>

The way that a list is created through a List Feature Instance must be called by some separate code function probably something like CreateListFromListInstanceFeature() instead of CreateListFromFormPost() and hence the list is created ok by the List Feature.

There are a few conversations about this on MSDN but when I first read them I didn’t realise the solution that they were pointing to.

These posts are:-
http://social.msdn.microsoft.com/Forums/en/sharepointcustomization/thread/458e5716-4fa4-425c-9fda-88ce4ccc6203

and

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/86dd7485-48cc-4836-ad09-40192bd54841

Anyway hope that helps.

Simon