Search code examples
sharepoint-2010splist

Check if a list already exists before creating it


I am creating a list definition and a list instance with xml code in my SharePoint 2010 solution. Now, each time I deploy my solution it deletes the list and creates a new one. I only want to create the list if it doesn't exist.

How do I check if the list already exists and where do I put the code?

My list definition and list instance appear among "Items in the Feature" in one of my features.


Solution

  • Thank you for your answers. I found the solution in the SharePointProjectItem.spdata file located in the list instance folder. Setting "DeploymentConflictResolutionBehavior" to "None" stopped Visual Studio from deleting my list on every deploy.

    My SharePointProjectItem.spdata file now looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <ProjectItem Type="Microsoft.VisualStudio.SharePoint.ListInstance" DefaultFile="Elements.xml" SupportedTrustLevels="All" SupportedDeploymentScopes="Web, Site" xmlns="http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel">
      <Files>
        <ProjectItemFile Source="Elements.xml" Target="MyListInstance\" Type="ElementManifest" />
      </Files>
      <ExtensionData>
        <ExtensionDataItem Key="DeploymentConflictResolutionBehavior" Value="None" />
      </ExtensionData>
    </ProjectItem>