Tuesday 2 April 2013

Avoiding deleting the data in a list every time you deploy a List Definition on Visual Studio for Sharepoint 2010 and 2013

If you have to work hard to create a big custom list with a List Definition, it will be handy for you to keep the data of that list, instead of being deleted every time you deploy your solution. To do that just do the following:

Step 1
Under the List Definition, select “elements.xml” and open it.
image

Step 2
Add the following attribute (  AllowDeletion="FALSE" ) to your list template. Your List Template should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <!-- Do not change the value of the Name attribute below. If it does not match the folder name of the List Definition project item, an error will occur when the project is run. -->
    <ListTemplate
        Name="PresentationLI"
        Type="10000"
        BaseType="0"
        OnQuickLaunch="TRUE"
        SecurityBits="11"
        Sequence="410"
        AllowDeletion="FALSE"
        DisplayName="PresentationLI"
        Description="List Definition for DWF Demos"
        Image="/_layouts/images/itgen.png"/>
  
</Elements>


Step 3
Now go to Schema.xml and add the following attribute under <extensiondata>:
image
It should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<ProjectItem Type="Microsoft.VisualStudio.SharePoint.VisualWebPart" DefaultFile="ExtranetUserControl.ascx" SupportedTrustLevels="FullTrust" SupportedDeploymentScopes="Site" xmlns="http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel">
  <Files>
    <ProjectItemFile Source="Elements.xml" Target="Extranet\" Type="ElementManifest" />
    <ProjectItemFile Source="Extranet.webpart" Target="Extranet\" Type="ElementFile" />
    <ProjectItemFile Source="ExtranetUserControl.ascx" Target="CONTROLTEMPLATES\ExtranetPresentation\Extranet\" Type="TemplateFile" />
  </Files>
  <SafeControls>
    <SafeControl Name="SafeControlEntry1" Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="ExtranetPresentation.Extranet" TypeName="*" IsSafe="true" IsSafeAgainstScript="false" />
  </SafeControls>
  <ExtensionData>
    <ExtensionDataItem Key="DeploymentConflictResolutionBehavior" Value="None" />
  </ExtensionData>
</ProjectItem>