Monday, 17 February 2014

Create Site Definition with List Instance

Create Site Definition with List Instance
In this article I provide a practical example as in the following:
  1. You need to create a Site Definition
  2. When the user creates a new site from this Site Definition, a new List Instance is also created
What is the Solution?
We can have any of the following solutions for this:
  1. Use Feature Stapling
  2. Use Event Handler
  3. Use Site Definition
I would recommend the third approach since it is the shortest path.
Procedure
Use the following procedure for the solution:
  1. Create SharePoint Solution Project
  2. Create Site Definition
  3. Create List Definition
  4. Include List in Site Definition
  5. Deploy
  6. Test
Step 1: Create a SharePoint Solution Project
Run Visual Studio 2012 and create a new SharePoint project.
Site Definition with List Instance
Choose the Farm Solution option as in the following:
Site Definition with List Instance
Step 2: Create Site Definition
Choose the Solution then select "Add new item" > "Site Definition" as in the following:
Site Definition with List Instance
Enter a name for the site definition.
Step 3: Create List Definition
"Add a new item" > "List" as in the following:
Site Definition with List Instance
Enter a name for the list instance. Choose the option custom and click the Next button of the wizard.
(The custom option allows you to create new fields, view fields and so on.)
Site Definition with List Instance
Click the Finish button and in the page that appears enter some unique fields for the list.
Site Definition with List Instance
Step 4: Include List in Site Definition
Open the onet.xml of Site Definition and Reference the list from it.
Site Definition with List Instance
You need to use the correct Template Id and Feature Id for it.
Site Definition with List Instance
For finding the feature id, open the Features then select "Feature 1" > ".feature file" then find the GUID against the featureId.
Site Definition with List Instance
Step 5: Deploy
Build and Deploy the solution.
Now you are ready to test the Site Definition.
Step 6: Test
Open the browser then select "Open SharePoint site" > "Site Contents" > "New Sub site".
Site Definition with List Instance
After creating the site go to the Site Contents and you can see the List Instance there.
Site Definition with List Instance
This concludes our Site Definition with List Instance creation.
Site Definition with List Instance
A good read for Site Definition vs. Site Templates

References

No comments:

Post a Comment