Automatic content provisioning to your existing SharePoint lists and libraries
by Yaroslav Pentsarskyy
In my previous article on automated content provisioning we`ve seen how you can provision content to document libraries and lists at the time of those document libraries being created as a part of your deployment. This technique is quite common when you deploy your solutions to QA environment and need to provision initial files and list items that will be part of your solution (site images, list entries etc). But what about lists that have been already create by SharePoint as a part of the template that you have no control over?
In my case I made a use of Reusable Content list where I needed to add several list items so that users have something to pick from when they want to use reusable content on the site. Reusable Content list is created as a part of the default deployment of publishin site on the site collection - so it`s not like you define it along with the list items inside feature.
What you can do - is you can create a feature that will be activated along with your site collection and will access your list (Reusable Content in my case) and add all of the needed content as a part of the feature activated method of the feature receiver. Here is how:
1. You create a feature which will be activate on a site collection level (at least in my list it had to be on a site collection level), with similar to this definition (Feature.xml)
<?xml version=”1.0″ encoding=”utf-8″?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/”
Id=”499262BB-4A7A-411d-AFA5-86E9AB638E88″
Title=”Provisions Content to Lists”
Description=”This feature provisions content to lists for this site.”
Version=”1.0.0.0″
Scope=”Web”
Hidden=”TRUE”
ActivateOnDefault=”FALSE”
AlwaysForceInstall=”TRUE”ReceiverAssembly=”MyProject.AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=02222feaffff3d79″ReceiverClass=”MyProject.AssemblyName.ProvisionContentEventReceiver”>
<Properties>
</Properties>
</Feature>
2. As you can see on one of the lines of feature definition we have ReceiverClass that will handle our receiver events defined in class MyProject.AssemblyName.ProvisionContentEventReceiver ; now we need to create a class called ProvisionContentEventReceiver inheriting from SPFeatureReceiver that will have FeatureActivated method looking something like this:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPWeb rootweb = properties.Feature.Parent as SPWeb)
{
rootweb.AllowUnsafeUpdates = true;
SPList reusableContent = rootweb.Lists[Constants.ReusableContentList];
if (reusableContent!=null)
{
SPListItem item = reusableContent.Items.Add();
item["Title"] = ContentToProvision.MySampleContent;
item["Description"] = ContentToProvision. MySampleContent;
item.Update();
}
}
}
Pretty standard code above - we get a hold of the list that resides on the site where the feature is activated (available through feature property bag) and create items in the list of our interest.
Keep in mind, if you need the content to be removed when your feature is deactivated - you must implement
FeatureDeactivating method.
Drop me a note if you have any questions.
Yaroslav Pentsarskyy, MVP
Blog:
www.sharemuch.com