web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Nishant Rana’s Weblog / Adding a Read Only Field or...

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

Nishant Rana Profile Picture Nishant Rana
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?>
                <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?>
                <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..

Posted in MOSS, SharePoint Tagged: SharePoint

This was originally posted here.

Comments

*This post is locked for comments