Adding a Read Only Field or Read Only Column to a SharePoint List


We had a requirement to add a read only column to one of our document library. Using Create column it isn’t possible to add a read only column.  One option could be to create the new column and then using SharePoint designer we can make use of JavaScript and modify the NewForm.aspx and EditForm.aspx to display that field as read only.

We thought of adding it as a Site Column making use of ReadOnly property of field.

<?xml version=1.0 encoding=utf-8?>

<Elements xmlns=http://schemas.microsoft.com/sharepoint/>

                <Field ID={0B8A5574-80BF-4d5e-99B9-9A25D8E8D21E}

                                   Name=_IsApproved

                                   DisplayName=Is Document Approved?

                                   Group=Custom Columns

                                   Type=Text                      

                                   Required=FALSE      

                                   ReadOnly=TRUE       

                                 

                                   >

                </Field>            

</Elements>

However if we set ReadOnly as True the field doesn’t appear on Site Settings pages for managaing site columns and content types. However we can add it to the view using the below code

 

SPSite oSiteCollection = new SPSite(@”http://servername:port&#8221;);

            using (SPWeb oWebsite = oSiteCollection.OpenWeb())

            {

                SPList oList = oWebsite.Lists[“ListName”];

                oList.Fields.Add(“Is Document Approved?”, SPFieldType.Text, false);

                oList.Update();

                SPView oView = oList.DefaultView;

                oView.ViewFields.Add(“Is Document Approved?”);             

                oView.Update();

                }

However the field was still appearing in editform.aspx and newform.aspx in editable mode.         

So finally tried this

Modified the definition for the custom site column as following

<?xml version=1.0 encoding=utf-8?>

<Elements xmlns=http://schemas.microsoft.com/sharepoint/>

                <Field ID={0B8A5574-80BF-4d5e-99B9-9A25D8E8D21E}

                                   Name=_IsApproved

                                   DisplayName=Is Document Approved?

                                   Group=Custom Columns

                                   Type=Text                      

                                   Required=FALSE      

                                   ReadOnly=FALSE

                                   ShowInDisplayForm=TRUE

                                   ShowInEditForm=FALSE

                                   ShowInNewForm=FALSE                        

                                   >

                </Field>            

</Elements>

 

Setting ShowInDisplayForm and ShowInEditForm as False and keeping ReadOnly as False so that the field could appear within Site Settings pages for managing site columns.

This last solution worked ..

The feature file for installing the above site column

<?xml version=1.0 encoding=utf-8?>

<Feature  Id=D829F71B-7FCC-4f0d-950D-6B562AFF400E

          Title=MyCustom Feature

          Description=This is my custom column feature

          Version=12.0.0.0

          Scope=Site

          xmlns=http://schemas.microsoft.com/sharepoint/>

                <ElementManifests>

                                <ElementManifest Location=MyCustomColumn.xml />                          

                </ElementManifests>  

</Feature>

 

Now the only way to edit the column was through SharePoint Object Model

   SPSite oSiteCollection = new SPSite(@”http://servername:port&#8221;);

            using (SPWeb oWebsite = oSiteCollection.OpenWeb())

            {

                SPList oList = oWebsite.Lists[“ListName”];        

                SPListItem item = oList.Items[0];         

                item[“Is Document Approved?”] = “These are my changes”;

                item.Update();              

            }

That’s it..

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

11 thoughts on “Adding a Read Only Field or Read Only Column to a SharePoint List”

  1. What about the DataSheet view?.. I bet users are allowed to edit the field in there..

    Btw it’s never a bad Idea to do:

    using(….)
    {
    try{
    …your update here…
    }catch(Exception e)
    {
    ….do something..
    }
    }

    Like

  2. It does show up in datasheet view, but this approach is still useful for me, I think. But if someone can think of a way to hide it from datasheet view, even better.

    Also, regarding JP’s suggestion, I would say that since the using clause already implements a try-finally block (disposing the SPWeb in this case), putting a try-catch block would only be useful if you were actually going to do something in the catch block. If you’re simply going to rethrow, then I wouldn’t bother; the using clause will take care of it for you.

    Regards,
    Mike Sharp

    Like

  3. Can you please tell me how you applied the last step of modifying the column. I have added the column using your solution but don’t know how to modify the attribute. I need the code to apply this:

    Like

  4. hi,
    I want to customize the versioning of Document set as when a document is created it’s Version should Consists “I” in version with current version number.
    I have written ReceiveEvent for this and got the Version of the Document set But It is ReadOnly is this possible to override this property in event receiver.

    Thanks

    Like

  5. Furnishing ideas and decoration tips, interior projects and home improvement. Interior design ideas and fundamentals on establishing your home lighting and atmosphere. Inspirational ideas for your home, office or hotel. News from the furniture trends.

    Like

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM