Wednesday, February 4, 2015

Prevent adding duplicates records from predefined data in ListInstance while reactivating its feature

In SharePoint if we have predefined data in ListInstance then deactivating and then reactivating its feature causes the predefined data to created again resulting in duplicates. Say, for e.g. our ListInstance looks something like this.

<ListInstance ...>
  <Data>
    <Rows>
      <Row>
        <Field Name="Title">Record 1</Field>
      </Row>
      <Row>
        <Field Name="Title">Record 2</Field>
      </Row>
    </Rows>
  </Data>
</ListInstance>

If we deploy this to a SharePoint site and if we deactivate and reactivate its feature then we will have “Record 1” and “Record 2” appear twice. Deactivating and reactivating it again would only create more duplicates.

One way to solve this is to add field named ID to each Row.

<ListInstance ...>
  <Data>
    <Rows>
      <Row>
        <Field Name="Title">Record 1</Field>
        <Field Name="ID">1</Field>
      </Row>
      <Row>
        <Field Name="Title">Record 2</Field>
        <Field Name="ID">2</Field>
      </Row>
    </Rows>
  </Data>
</ListInstance>

This would not create duplicates in subsequent reactivation of feature.

No comments:

Post a Comment