Visual Studio 2010 F5 solution deployment

Create a content type project in Visual studio 2010 and then create some custom content types with some custom fields in it.

Sample Elements.xml file :

 <?xml version="1.0" encoding="utf-8"?>
 <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
   <!-- Parent ContentType: Document (0x0101) -->
  
 <Field ID="{D6A841D7-4067-4519-B794-E25AC438E9CF}"
   Name="MyCustomField"
   StaticName="MyCustomField"
   DisplayName="MyCustomField"
   Description="MyCustomField"
   Group="abc"
   Type="Text"
   SourceID="https://schemas.microsoft.com/sharepoint/v3" />
  
 <ContentType ID="0x010100b19e17b94ee044778dcffe8f92ce18e6"
                Name="ContentTypeTest"
                Group="Custom Content Types"
                Description="My Content Type"
                Inherits="TRUE"
                Version="0">
     <FieldRefs>
 <FieldRef ID="{D6A841D7-4067-4519-B794-E25AC438E9CF}" Name="MyCustomField"/>
 </FieldRefs>
   </ContentType>
 </Elements>

Now, Hit F5 to compile and deploy this Content Type.  First time deployment of the solution works fine, but subsequent deployment fails with this error message,

Error occurred in deployment step 'Activate Features': The field with Id {a6a841d7-4067-4519-b794-e25ac438e9cf} defined in feature {a6192d76-512f-48de-9284-251b49902345} was found in the current site collection or in a subsite.

We can get around this problem by setting the OverWrite property of the field to true, https://msdn.microsoft.com/en-us/library/aa979575.aspx

 <Field ID="{D6A841D7-4067-4519-B794-E25AC438E9CF}"
   Name="MyCustomField"
   Overwrite="TRUE"
   ... />

This works well because the attribute Overwrite specifies whether the field definition for a new field that is activated on a site (SPWeb) overwrites the field definition for an existing field, in cases where the new field has the same field ID as the existing field. Hence when Overwrite is set to true, it assumes that the field already exists and it just need to overwrite this field.

What causes this exception?  Actually Visual Studio 2010 doesn't do the cleanup job. VS triggers a process named VSSPHost4.exe which does the job of adding, deploying the features.  VSSPHost4.exe is the 64-bit host process that executes SharePoint commands, https://msdn.microsoft.com/en-us/library/ee256704.aspx. This process doesn't refresh the SPSite/SPWeb objects.

The workaround to get rid of this exception is to kill the VSSPHost4 process

1. F5 (Deploy the solution)

2. Shift +F5 (Retract the solution)

3. Again F5.  This time it will fail.

4. Open the task manager and kill VSSPHost4 process.

5. Now F5 and the solution gets deployed successfully.

Ideally we need to kill the VSSPHost4 process any time we see the error message during the F5 deployment.