Create List Instance Feature for SharePoint 2010 in Visual Studio
Requirement:
Create a new Link list with pre-populated list items as a feature-based solution.
Steps:
1. Lets create a SharePoint 2010 list instance feature with Visual Studio. Create a New Empty SharePoint Project, Give it a name, I’ve named it as Crescent.Portal.DashboardLinks
2. In the next dialog box, Select either Farm solution or Sandboxed solution whichever applicable to your requirements. I’ve selected “Deploy as a Farm Solution”
3. Right click the Project in visual studio solution explorer >> Add >> New Item >> List Instance to create new SharePoint 2010 list using visual studio
4. Enter the List Instance settings: Link List Name, List to Instantiate, URL. Click Finish.
5. Now our project will look like the below screen. Look at the “FeatureId” attribute. This is the Feature ID of the Link List feature. In case, If you want to create a List instance for your own List definition, You can use the Feature ID of the particular List. (You can just get into 14 Hive, Templates and pick the relevant feature folder’s Feature.xml file to get the feature Id)
6. Rename the Feature from “Feature1” to “DashboardLinks” in solution explorer. Get into the Feature designer (Right click the “DashboardLinks” and choose “View Designer”), Give it a meaning full title (Here I’ve given “Dashboard Links Feature”) that will appear on the site features page. Add the DashboardLinks ListInstance Element to the feature.
7. Our requirement is to Pre-populate the Dashboard Links list. So, let’s add the <Data> element with values to the Elements.xml as below:
SharePoint 2010 list instance elements.xml
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<ListInstance Title="Dashboard Links"
OnQuickLaunch="TRUE"
TemplateType="103"
FeatureId="00bfea71-2062-426c-90bf-714c59600103"
Url="Lists/DashboardLinks"
Description="">
<Data>
<Rows>
<Row>
<Field Name="URL">https://Crescent.SharePoint.com/sites/PMO/Dashboard, PMO Dashboard</Field>
<Field Name="Comments">PMO Dashboard Link</Field>
</Row>
<Row>
<Field Name="URL">https://Crescent.SharePoint.com/sites/functional/Dashboard, Functional Dashboard</Field>
<Field Name="Comments">Functional Dashboard Link</Field>
</Row>
<Row>
<Field Name="URL">https://Crescent.SharePoint.com/sites/operations/Dashboard, Operational Dashboard</Field>
<Field Name="Comments">Operations Dashboard Link</Field>
</Row>
</Rows>
</Data>
</ListInstance>
</Elements>
8. Build and deploy the Project (Just right click the Project in visual studio, choose Build and then choose Deploy)
9. Verify the feature is activated by going to Site actions >> Site settings >> Manage Site features (Under Site actions tab)
10. See it in action! List created in SharePoint using Visual Studio 2010 feature.
One query- every time deploy this feature it deletes the existing list instance(along with its records) and creates a new instance. due to which I loose the existing data. How can I only update the changes while I re-deploy the feature?
Instead of Declarative(XML) way, Use Feature Receiver code to Check if the list already exists and update!
Hey!
I created list instance step by step as the above guide. but my created list instance don’t show on home page in sharepoint server 2010. Although the feature is activated!
can anybody help me?
Hi there,
When you create a list, it will not be placed in home page by default. You have to do it either manually or programmatically.
Hello Salaudeen,
thanks for your quick response, but it doesn’t resolve my problem. If I would do it the way you described, it would fix my solution allways to special web: “/pageLib/myPage.aspx” (for site’s level) or “/myWeb/mySubWeb/pageLib/myPage.aspx”. I hope you know what I mean, I have allway exactly to determine the Url.
I’ve played around a little last days, and figured out that I can probably solve it, by using “$resource:” to build my urls dynamically. But I’ve not found a placeholder like “$ressource:wss_currenweburl” or something like this, yet.
~site or ~sitecollection tokens can not be used directly in Link lists.
If you are in site and want to link to current site’s list/library, use: /lists/list-name/AllItems.aspx
Hi,
i have a question to this row:
https://Crescent.SharePoint.com/sites/PMO/Dashboard, PMO Dashboard
how can i create a link, which refers to a page, which lies in a pagelib in the same web where i activate my list intance feature? is there a place holder like ~site or ~web. so it finally looks like this:
~currentWeb/custompagelib/mypage.aspx, Foo Boo
Yes – nice article.
Thanks