Wednesday, August 12, 2009

How to: Create custom List Template and List Instance Programatically

Hi all,

I will show, how to create a list template and based this template creating list instances. I have used a Visual Studio 2008 Extensions as its creates XML files extremely fast but its not very useful to create WSP solutions. For this we can use WSPBuilder (click here) to package the solution.

Its always better to use Visual Studio 2008 Extensions. This can be downloaded here.
For our case, we will use both techniques i.e. VSeWSS and WSPBuilder.


Step1) Use VSeWSS extensions

Create a New >> New Project >> SharePoint >> Empty >> Project name: ListTemplateInstance












Step2) List definition settings

















Step3) ListDefinition.xml, Instance.xml and Schema.xml synchronized all the time since we are using IDs as follows:

List Instance:








List Template:








Schema.xml







We have to just modify schema.xml. Its huge file and if we try to modify by hand, you be overwhelmed and high probability and succumb to errors. Apparently, its an easy technique. Create the list using the SharePoint UI and export it using the
SharePoint Solution Generator 2008 which comes with the VSeWSS 1.3.

What is happening that when we create a Document Libary through a SharePoint UI and try to export this list, then SharePoint Solution Generator 2008 reverse engineer the lists and we can use this list as WSP package.

Step4) We create a document library namely "Test System Checklists" using SharePoint UI.











Step5)

We can also add new column (Period, Choice Type, Choices Daily,Weekly,Monthly,Yearly), in view "All Documents" we can select additional columns like created by, create and Group By Period






Now, we need to
SharePoint Solution Generator 2008, Choose "List Definition" >> Choose list (Test System Checklists) >> Project Name: System Checklists >> Finish

Copy schema.xml of "SystemChecklists" (SharePoint Solution Generator 2008) to "ListTemplateInsance".








Keep in mind that we have to modify the schema.xml ID like ListInstance.xml and ListDefintion.xml as SharePoint Solution Generator 2008 created a new ID. All the IDs must be consistent.

Right click on the project >> Properties >> Click on the DEBUG tab >> Choose "Start browser with URL: http://<>/sites/systemcheklists".

--------------------------------------------------------------------------------
CAUTION: If you forget this step then we will get this error:

Attempted to perform an unauthorized operation.
--------------------------------------------------------------------------------

Fix : Goto the project properties and inside the debug tab select the start action as 'start browser with url' and enter the valid site collection URL. Please check the site collection before you use it over here.

Final step is to build and deploy the solution.


We can List Instance and List Templates as follows:


(List Instance)









(List Template)













Step6)
Now, create a new WSPBuilderProject called as
IT.SystemChecklists Right-Click on project >> Add >> Add New Item >> Choose "Feature with Receiver" >> Name: ListTemplateReceiver. The structure should be as follows:




















Rename ListTemplateReceiver to IT.SystemChecklists.






















Now, under IT.Checklists, we need to create 3 folders namely: ListTemplates, ListInstances and DocLib.

Copy these files from ListTemplateInstance to IT.SystemChecklists:

>> ListTemplates: ListDefinition.xml
>> ListInstances: ListInstances.xml
>> DocLib: schema.xml and other aspx pages.






















The MOST important part of this step is that when you define ListTemplate, the NAME attribute is exactly
same as the Folder Name you have the schema.xml File stored in. Trust me, it took almost 2 days to fix this issue.

If you don't use some other folder name then you will get this error:

---------------------------------------------------------------------------------------

Caution: Exception from HRESULT: 0x81070201
--------------------------------------------------------------------------------------








Step7) In feature.xml, get the feat
ure ID and copy this is to ListDefinition.xml, schema.xml and ListInstances.xml.


feature.xml (ID must be same as ListDefinition.xml, schema.xml and ListInstances.xml)







schema.xml






Instances.xml








ListTemplates.xml







Step8)
These is an elements.xml file which is empty. Cut the ListInstance element of ListInstance.xml and copy to elements.xml.

We to provision the List Template (ListTemplates\ListDefinition.xml), List Instances (elements.xml) and schema.xml (DocLib\schema.xml and other aspx pages) in feature.xml as follows:





























feature.xml
 

<?xml version="1.0" encoding="utf-8"?>
<Feature Id="eb656c28-ba1c-4d6e-a117-7e06015b2bc9"
Title="[IT] System Checklists"
Description="Description for ListTemplateReceiver"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
ReceiverAssembly="IT.SystemChecklists, Version=1.0.0.0, Culture=neutral, PublicKeyToken=135fa78ef0185fbd"
ReceiverClass="IT.SystemChecklists.ListTemplateReceiver"
xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests>

<ElementManifest Location="ListTemplates\ListDefinition.xml" />
<ElementManifest Location="elements.xml"/>

<ElementFile Location="DocLib\schema.xml" />
<ElementFile Location="DocLib\AllItems.aspx" />
<ElementFile Location="DocLib\Combine.aspx" />
<ElementFile Location="DocLib\DispForm.aspx" />
<ElementFile Location="DocLib\EditForm.aspx" />
<ElementFile Location="DocLib\Repair.aspx" />
<ElementFile Location="DocLib\Upload.aspx" />
<ElementFile Location="DocLib\WebFldr.aspx" />

</ElementManifests>

</Feature
>

elements.xml



<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<ListInstance FeatureId="eb656c28-ba1c-4d6e-a117-7e06015b2bc9"
Id="BBC82E2A-043C-42d6-A4E7-B0DE6A45687B"
Title="IT System Checklists"
Url="Lists/SystemChecklists"
OnQuickLaunch="TRUE"
TemplateType="101">
</ListInstance>

</Elements>


ListTemplates.xml

<?xml version="1.0" encoding="utf-8" ?>

<Elements Id="eb656c28-ba1c-4d6e-a117-7e06015b2bc9" xmlns="http://schemas.microsoft.com/sharepoint/">

<ListTemplate Name="DocLib"
DisplayName="IT System Checklists"
Description=""
BaseType="1"
Type="101"
OnQuickLaunch="TRUE"
SecurityBits="11"
Sequence="110"
Image="/_layouts/images/itdl.gif"
DocumentTemplate="101" />

</Elements>


Step9) Activate the feature and test it.

Activate the feature (IT.SystemChecklists).





Hopefully, the List Instance should be shown on quick launch menu.









and also List Template as follows:








-------------------------------------------------------------------------------
CAUTION: At times, during the testing and try to activate the "IT.SystemChecklists", I used to get this Error:
Exception from HRESULT 0x81070215

It happens when open a list, either the list is has not properly been deleted and is in a corrupted state.

Fix:

Although the help message displays information about using the forcedeleteweb operation, you have to use forcedeletelist instead. The syntax is:
stsadm.exe -o forcedeletelist -url <url>
For e.g. if the list called as "Annotations" has corrupted just fire up this command:

stsadm -o forcedeletelist -url http://server/apac/lists/annotations

For more info:
http://bloggingabout.net/blogs/harold/archive/2009/01/10/deleting-a-broken-sharepoint-list-using-stsadm.aspx

http://technet.microsoft.com/en-us/library/cc262609.aspx

Just fire the stsadm command and activate the feature.
-------------------------------------------------------------------------------

Happy Programming!
--aaroh

No comments:

Low Code Reimagined with AI + Copilot Pitch Deck - Copy Copilot day (Virtual) - 2023

 Hi All,  I presneded a session at Pune UG on Low Code Reimagined with AI + Copilot Pitch Deck.  Video is at this address  https://www.youtu...