Thursday, December 17, 2009

How to: remove the “title” column from a SharePoint list programatically

 Hi All,

Often we do not want to show the "Title" field which is default field for all SharePoint list. Many times this title field does not much any sense in particular scenarios.  We can get rid of this title field by 2 ways:

  1. SharePoint user interface:

    Someone has already written in one of the articles on codeproject. He has also given screen shots and description is crisp, neat and clear.

  2. Programmatically:

    Many times we have to put our artifacts such as lists, web parts, workflows etc. in different environments in a packaged solution i.e. WSP solution.  For instance, we have to deploy the solution on the development, testing, pre-live and live environments. If we do our deployments on different environments through UI is trouble some and its manual process.

    There is another technique is to package the solution and removed the “Title” column in the list schema itself. Therefore, we don’t have to worry for different environments. 
Here are the steps and we will disucss in three parts: 


PART1 -  Create VS.Net project with VS WSS extensions

--------------------------------------------------------------------

Step1) Use VSeWSS extensions

a) Create a New >> New Project >> SharePoint >>Empty >> Project name: "CustomList".
b)
Right click on the solution >> Add >> New Item >> Select SharePoint >> Choose "List Definition"
 

















c) From the drop-down choose, "Custom List" and check the "Create an Instance of this list".
  










Step 2) ListDefinition.xml, Instance.xml and Schema.xml synchronized all the time since we are using IDs. 


Moreover, in "CustomList" project, we will have following files: 
>> AllItems.aspx
>> DispForm.aspx
>> EditForm.aspx
>> instance.xml (Instance of custom list)
>> ListDefinition.xml (List template for the custom list)
>> NewForm.aspx
>> schema.xml (core for any list. If list has a body, then schema.xml its soul).
We will use it in part 3. 


We have to just modify schema.xml. Its huge file and if we try to modify by hand, you be overwhelmed and high probability and succumb to errors. Apparently, its an easy technique. Create the list using the SharePoint UI and export it using the SharePoint Solution Generator 2008 which comes with the VSeWSS 1.3.

What is happening that when we create a Document Libary through a SharePoint UI and try to export this list, then SharePoint Solution Generator 2008 reverse engineer the lists and we can use this list as WSP package.

Step 3) We create a Custom List namely "Test Custom List" using SharePoint UI.






































Its a custom list with one field namely "Title". We add 2 more fields: 
     
     Column                Type

a)  Name of person  - Person or Group

b)  Role                   -  Single line of text





PART2 - Using SharePoint Solution Generator 2008

-------------------------------------------------



Step 4) We need to SharePoint Solution Generator 2008


>> Choose "List Definition"

































>> Choose list (Test Custom List) (Its from SharePoint UI) >> Project Name: CustomList >> Finish


Under "SharePoint Definitions 2008", we can see a folder called as "CustomList". It will have following files: 
>> AllItems.aspx
>> DespForm.aspx
>> EditForm.aspx
>> ListDefinition.xml
>> NewForm.aspx
>> schema.xml (core for any list. If list has a body, then schema.xml its soul). We will use these files in part 3.



PART3 - Create new WSPBuilder project
-----------------------------------------------------------
Step 5)
We create new WSPBuilder project. Project name would be: WSPCustomList.


Step 6) Right click on the project >> Add >> New Item >> Under "Categories" choose WSPBuilder >>  From the "Templates" >> choose "Feature with receiver" and name it: "CustomListReceiver"



Click "Add" button. WSPBuilder will prompt about feature settings. Fill in these details:
Title: Custom List Receiver
Description: Description of the CustomListReceiver

Scope: Web



























Now, under "CustomListReceiver", we need to create 3 folders namely: ListTemplates, ListInstances and DocLib.

Copy these files from Part1 ( all files from "CustomList" project i.e.
AllItems.aspx, DispForm.aspx, EditForm.aspx, instance.xml, ListDefinition.xml, NewForm.aspx, schema.xml)
) to WSPCustomList.

>> ListTemplates: ListDefinition.xml

>> ListInstances: ListInstances.xml
>> DocLib: schema.xml (CustomList project) and other aspx pages.




































Step 7) We overwrite the schema.xml (Part1 - CustomList project) which is generated by VSeWSS extensions to the Solution generator (Part2 - Solution Generator). 


















This step is very important.





Step 8) Modfying the feature.xml 


We need add some elements in the feature.xml. 


>> Firstly, we add element files which as stored in DocLib folder (schema.xml, AllItems.apsx, DispForm.aspx etc.) 
>> Secondly, we have to set custom list definition which as stored in ListTemplates folder  (ListDefinition.xml)
>> Thirdly, we set the elements.xml  


The most important is that we have to always look feature ID. ListDefinition.xml, Instance.xml and Schema.xml synchronized all the time since we are using IDs and we have to copy the feature ID across all 3 files. 





a) feature.xml 
















































<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="49beac74-762b-42f9-b28a-7d742c118771"
          Title="Custom List Receiver"
          Description="Description for CustomListReceiver"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Web"
          DefaultResourceFile="core"
          ReceiverAssembly="WSPCustomList, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6a9e3da7c585f60f"
          ReceiverClass="WSPCustomList.CustomListReceiver"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>


    <!-- For the custom list definitions -->
    <ElementManifest Location="ListTemplates/ListDefinition.xml"/>
  
    <!-- For the custom list  -->
    <ElementFile Location="DocLib\schema.xml" />
    <ElementFile Location="DocLib\AllItems.aspx" />
    <ElementFile Location="DocLib\DispForm.aspx" />
    <ElementFile Location="DocLib\EditForm.aspx" />
    <ElementFile Location="DocLib\NewForm.aspx" />


  </ElementManifests>
</Feature>


b) elements.xml



























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

  <!-- For the custom list  -->
  <ListInstance FeatureId="49beac74-762b-42f9-b28a-7d742c118771"
                Id="42A4374F-7B51-45fd-909A-858163546BBA"
                Title="Custom list No Title"
                Url="Lists/CustomLstNoTitle"
                Description="Custom list No Title"
                OnQuickLaunch="TRUE"
                TemplateType="10116">
  </ListInstance>
</Elements>



c) ListDefinition.xml



















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

  <!-- For the custom list  -->
  <ListInstance FeatureId="49beac74-762b-42f9-b28a-7d742c118771"
                Id="42A4374F-7B51-45fd-909A-858163546BBA"
                Title="Custom list No Title"
                Url="Lists/CustomLstNoTitle"
                Description="Custom list No Title"
                OnQuickLaunch="TRUE"
                TemplateType="10116">
  </ListInstance>
</Elements>



d) Schema.xml

















































Step 9) Modifying the schema.xml 


When we reverse engineer the List from SharePoint UI to Solution Generator 2008, the list contains lot of elements. 


































a)  Removing unnecessary columns (Fields).


But for our case we do not need so many elements. We just need “Name of person” and “Role”. Therefore., we remove all <Fields> elements but "Name of Person" and "Role". 
















































   


<Fields>
      <Field
        Type="User"
        DisplayName="Name of person"
        List="UserInfo"
        Required="TRUE"
        ShowField="Title"
        UserSelectionMode="PeopleOnly"
        UserSelectionScope="0"
        ID="{c97e360b-27bd-4e5c-8620-6de8dd6bb605}"
        SourceID="{87d7fd01-45e1-41ae-90ea-3fbe78492030}"
        StaticName="Name_x0020_of_x0020_person"
        Name="Name_x0020_of_x0020_person"
        ColName="int1"
        RowOrdinal="0" />
    
      <Field
        Type="Choice"
        DisplayName="Role"
        Required="TRUE"
        Format="Dropdown"
        FillInChoice="FALSE"
        ID="{b7acffb5-f470-449e-8589-b04a06b027db}"
        SourceID="{87d7fd01-45e1-41ae-90ea-3fbe78492030}"
        StaticName="Role"
        Name="Role"
        ColName="nvarchar3"
        RowOrdinal="0">
        <Default>Manager</Default>
        <CHOICES>
          <CHOICE>Manager</CHOICE>
          <CHOICE>Developer</CHOICE>
          <CHOICE>Support</CHOICE>
        </CHOICES>
      </Field>
    </Fields>

 b) Removing FieldsRefs 

Same concept applies to FieldRefs.  







Step 10)  Build, WSPBuilder >> Build WSP >> Deploy 

When the feature is activated, we can activate the feature in a particular "web scope" website and activate the feature. 




Step 11) List is provisioned in the web successfully. 

But "Title" column is still there. Its because from the default content type.

 















We can hide it in the view. We just uncheck  the "Title" column.













Now, we can observe that Title field is not there. 













We can notice that "New" form comes with 2 columns and no "Title" column. 












Moreover, in DispForm.aspx only 2 columns are displayed.





I hope it helps. 

Cheers, 
--aaroh

1 comment:

Anonymous said...

Good article. However, when I click on new from the list, then only title columns is showing, instead of showing all the required columns. hmm

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...