Thursday, February 16, 2012

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.DashboardLinkssharepoint 2010 list instance visual studio

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
sharepoint 2010 create list instance visual studio
 4. Enter the List Instance settings: Link List Name, List to Instantiate, URL. Click Finish.
sharepoint 2010 create list visual studio

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)

sharepoint 2010 c# create list instance
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.
create a list instance sharepoint 2010
7. Our requirement is to Pre-populate the Dashboard Links list. So, Lets 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="http://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">http://Crescent.SharePoint.com/sites/PMO/Dashboard, PMO Dashboard</Field>
          <Field Name="Comments">PMO Dashboard Link</Field>
        </Row>
        <Row>
          <Field Name="URL">http://Crescent.SharePoint.com/sites/functional/Dashboard, Functional Dashboard</Field>
          <Field Name="Comments">Functional Dashboard Link</Field>
        </Row>
        <Row>
          <Field Name="URL">http://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)
 sharepoint 2010 list instance feature

10. See it in action! List created in SharePoint using visual studio 2010 feature.sharepoint 2010 create list instance



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

8 comments :

  1. Yes - nice article.

    Thanks

    ReplyDelete
  2. Hi,

    i have a question to this row:
    http://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

    ReplyDelete
  3. ~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

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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?

    ReplyDelete
    Replies
    1. 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.

      Delete
  6. 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?

    ReplyDelete
    Replies
    1. Instead of Declarative(XML) way, Use Feature Receiver code to Check if the list already exists and update!

      Delete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...