BLOG zzy.my

合抱之木, 生于毫末; 九层之台, 起于累土; 千里之行, 始于足下。

SharePoint List 列表添加只读(Read Only) 字段

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

转自 http://nishantrana.wordpress.com/2009/03/21/adding-a-read-only-field-or-read-only-column-to-sharepoint-list/

Loading