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”);
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”);
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..
Hmm, why not just make it a calculated column?
LikeLike
The simplest approach is always the best…
🙂
LikeLike
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..
}
}
LikeLike
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
LikeLike
Try this:
http://blogs.msdn.com/manish_ahuja/archive/2009/11/03/sharepoint-readonly-site-column.aspx
LikeLike
Try Infowise Smart List Pro
http://www.infowisesolutions.com/product.aspx?id=SmartListPro
LikeLike
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:
LikeLike
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
LikeLike
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.
LikeLike