Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a feature which on activation has to create a list called Scanners. Only one column from the list (Title) is actually used in my app. The list needs to be pre-populated with some values. To fill in the values I used the ListInstance's Elements.xml, so now it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="Scanners"                
                TemplateType="10000"
                Url="Lists/Scanners"
                Description="Scanners">
    <Data>
      <Rows>
        <Row>
          <Field Name="Title">DevScanner</Field>
        </Row>
        <Row>
          <Field Name="Title">ProdScanner</Field>
        </Row>
        <Row>
          <Field Name="Title">TestScanner</Field>
        </Row>
      </Rows>
    </Data>    
  </ListInstance>
</Elements>

The list instance is created at the Site(Web) level. On Feature activation, the list instance should be created only if the site doesn't already contain a list with the same name (Scanners). The current behavior does this, but with one problem. On each activation, the data gets added again to the existing list, so I end up with lots of duplicate rows. How can this be prevented?

The client's specs don't allow me to delete the list on feature deactivation. I tried fiddling with the Deployment Conflict Resolution setting, with no success.

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

If your feature can be activated/deactivated multiple times then you should not use the <data> elemente to populate elements into the list instance. As you get the duplicate item effect that you're experiencing and there is no way to prevent that.
The Deployment Conflict Resolution setting is only used by Visual Studio when deploying to your local environment (and the way it solves the problem is by deleting the list instance). I'm not 100% agreeing with MS that this is good idea as it hides a lot of problems which will occur in test/production from the dev. environment.

The solution for your problem is to pre-populated rows into the list instance without duplication. You should pre-populate using code in a FeatureReceiver where you can check if the list is empty before adding the rows.

share|improve this answer
add comment

You can also add the following line for each <row>:

<Field Name="ID">1</Field>

For example:

    <Row>
      <Field Name="Title">DevScanner</Field>
      <Field Name="ID">1</Field>
   </Row>

As the ID is still the same it won't add a new record, so it prevents duplicate rows.

share|improve this answer
    
This worked well for me. Thanks! –  Roel Hans Bethlehem May 16 '13 at 7:58
add comment

This could solve the duplicates: http://jeremysublett.com/archive/2008/12/28/re-provisioning-data-in-a-sharepoint-list.aspx

share|improve this answer
    
wow, nice find, thanks –  pax162 Apr 19 '12 at 8:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.