Sunday, October 17, 2010

How to: Designing List schemas, instances and content types in SharePoint 2010


Hi all, 

In this walk through I will step-by-step to develop the a custom list definition, list instance and custom content type.

Step1: Create a new VS 2010 project. New >> Project >> Visual C# >> SharePoint 2010 >> Empty SharePoint Project. 

Project name: Lab04_ListsAndSchemas
.NET Framework 3.5 



Step2: Right click on the project >> Add >> New Item >> Choose Content type.
Name: ProductCT. 



Step3: We create two site columns (fields, site wide) as follows:
Name: Product Description , Product ID
Type: Text, Number 

In the content type, we add field reference as well. 

Elements.xml:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <!-- Site Columns -->
  <Field SourceID="http://schemas.microsoft.com/sharepoint/v3"
         ID="{B39F9441-0124-4221-9C64-D61983A3397A}"
         Name="ProductDescription"
         DisplayName="Product Description"
         Group="My Custom Columns"
         Type="Text"
         DisplaceOnUpgrade="TRUE" />

  <Field SourceID="http://schemas.microsoft.com/sharepoint/v3"
         ID="{22A5A133-BC36-4AD4-93EA-A5DE9C36F417}"
         Name="ProductID"
         DisplayName="Product ID"
         Group="My Custom Columns"
         Type="Number"
         DisplaceOnUpgrade="TRUE" />

  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x01003044473278df426692f1919852d4d8af"
               Name="Lab04_ListsAndSchemas - ProductCT"
               Group="Custom Content Types"
               Description="My Content Type"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}"
          Name="Title"
          DisplayName="Product Name" />
      <FieldRef ID="{B39F9441-0124-4221-9C64-D61983A3397A}"
                Name="ProductDescription" />
      <FieldRef ID="{22A5A133-BC36-4AD4-93EA-A5DE9C36F417}"
                Name="ProductID" />

    </FieldRefs>
  </ContentType>
</Elements>

Step4: Right click on the project. Add >> New Item >> Choose "List Definition from content type".
Name: ProductCT


In this project we have create just one content type. Therefore,  in the drop down list we will see only one content type. In addition, we will create instance of this list. 


Step5: Lets examine list definition list schema. 
As we have chose "List Definition from the content type", we will get all the fields such as Product Description and Product ID automatically as illustrated below: 


However, we should these custom fields in the view as well. Therefore, we add FieldRefs in the BaseViewID="1" view. 


Step6: List Definitions and instances description. 

We need to give a meaningful names to the list definitions and instances. Also, in type we should give a number more than 10000.

Elements.xml (List Definition)

<?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="ProductListDefinition"
        Type="10005"
        BaseType="0"
        OnQuickLaunch="TRUE"
        SecurityBits="11"
        Sequence="410"
        DisplayName="ProductListDefinition"
        Description="My List Definition"
        Image="/_layouts/images/itgen.png"/>
</Elements>

Elements.xml (List Instances)
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="Products"
                OnQuickLaunch="TRUE"
                TemplateType="10005"
                Url="Lists/Products"
                Description="My List Instance">
  </ListInstance>
</Elements> 

 The project structure will look like this:  

Build and Deploy the solution. 
Navigate to the site to view the new content type and list instance. 


Cheers, 
--aaroh 

Download the source here.

Sunday, October 10, 2010

Error occurred in deployment of step ‘Activate Features’: the field with ID defined in feature was found in the current site collection or in a sub site.

Hi all,

While developing a custom content type, custom list definition based on the custom content type, I got an error while deploying to the SharePoint 2010 server.

Error:
‘Error occurred in deployment of step ‘Activate Features’: the field with ID <New GUID> defined in feature <FeatureGUID> was found in the current site collection or in a sub site.

Fix: 

Its a known issue and there couple of workarounds to fix this issue. 

Workaround 1:• Right click on Project node in Solution explorer and click "Retract".
• Close VS. This will flush the stale cache.
• Open the project in a new instance of VS and re-deploy the solution.

Workaround 2:If closing VS after retract doesn't solve the problem then a longer workaround is:

• Redeploy the project -> This will show an error during feature activation.
• Go to "Manage Site Features" under "Site Settings" in SharePoint UI in the browser.
• Activate and then Deactivate the deployed feature which contains the Field.
• Retract the Solution in VS i.e. right click on the Project node and click "Retract".
• Close VS. This will flush the stale cache.
• Open the project in a new instance of VS and re-deploy the solution.

Please refer to the MSDN KB article.

Cheers, 
--aaroh 

Low Code Reimagined with AI + Copilot Pitch Deck - Copy Copilot day (Virtual) - 2023

 Hi All,  I presneded a session at Pune UG on Low Code Reimagined with AI + Copilot Pitch Deck.  Video is at this address  https://www.youtu...