Hiding Custom SharePoint fields from edit mode, new window or display mode

A quick post:

Lets say that you do not want to allow users to possibility to edit and insert values to a custom field in one of your lists. To do this you can set the values of one of the following properties of a SPField object:

  1. ShowInEditForm
  2. ShowInDisplayForm
  3. ShowInNewForm

You can do this programmatically at any time, below is a link on how to do it:

http://www.sharepointdynamics.net/2011/09/how-to-hide-a-sharepoint-list-column-from-a-list-form-new-edit-and-display/


#Get the web and site column objects
$web = Get-SPWeb http://portal
$column = $web.Fields["Test Column"]

#Set the PushChangesToLists property for the changes to be applied
#to lists where the column has already been added
$column.PushChangesToLists = $true

#Change the ShowInEditForm and other properties and update objects
$column.ShowInEditForm = $false
$column.ShowInNewForm = $false
$column.ShowInDisplayForm = $true
$column.ShowInViewForms = $true

$column.Update()
$web.Update()
$web.Dispose()

Remember to check that the change is applied to the list field also. If not then do the change directly to the list field.

 

Or by XML definition:

<Field Type=”Number” ShowInEditForm=”FALSE” ShowInNewForm=”FALSE” DisplayName=”Counter” Required=”FALSE” EnforceUniqueValues=”FALSE” Indexed=”FALSE” Group=”My Group” ID=”{7f111eff-569d-4852 8bf2-ba0fcee6c69a}” SourceID=”{b1498d8e-4ad9-4d0d-8584-53d9a5de8c11}” StaticName=”Counter” Name=”Counter”>
<Default>0</Default>
</Field>

Links to MSDN about the properties:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.showineditform.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.showindisplayform.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.showinnewform.aspx

SharePoint Modified field gone crazy or it just won’t work :)

The problem is very simple in this case. The modified field does not work anymore. When a document or any list item is modified the time stamp does not get updated as it should.

To fix this problem you need to do two things unfortunately.

  1. First you need to update the out of the box Modified field. You can do this by a manually modified URL to the edit page of fields. You have to do this because the field is visible by default. Below is a sample URL:

http://vsp2007dev:24327/_layouts/fldedit.aspx?field=Modified

Pay attention to the bold part of the URL.Just past the bold part after your application domain name and you will be fine. Next all you need is to simply press the “OK” button and be sure to leave everything else unchanged. Especially the update option must be set to yes. What this will do is simply go through every “Modified” field use in your SharePoint application and makes some updates to the field. This will fix the time stamp problem.

2. After you do the seconds step you will run into another problem that an xml attribute from the Modified field schema is missing. This needs to be fixed by a script. Below you will find the code that fixes this error AND also does the update procedure in step 1 described above. So no manual labor here :).

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using System.Xml;

using System.IO;

 

namespace ModifiedFieldFix

{

    public class Program

    {

        static void Main(string[] args)

        {

            StringBuilder output = new StringBuilder();

            TextWriter tw = new StreamWriter(“LOGFILE_” + DateTime.Now.ToFileTime()  + “.txt”);

            if (args != null && args.Length > 0)

            {

                tw.WriteLine(“Given param: ” + args[0]);

                try

                {

                    using (SPSite msite = new SPSite(args[0]))

                    {

                        tw.WriteLine(“Site opened”);

                        SPField siteMField = msite.RootWeb.Fields[new Guid(“28cf69c5-fa48-462a-b5cd-27b6f9d2bd5f”)];

                        siteMField.Update(true);

                        tw.WriteLine(“Modified field update request performed”);

                        foreach (SPWeb mweb in msite.AllWebs)

                        {

                            tw.WriteLine(“———————————————————————-“);

                            tw.WriteLine(String.Empty);

                            tw.WriteLine(“WEB UNDER PROCESSING: ” + mweb.Title);

                            tw.WriteLine(“WEB URL: ” + mweb.Url);

                            for (int i = 0; i < mweb.Lists.Count; i++)

                            {

                                try

                                {

                                    SPList mlist = mweb.Lists[i];

                                    SPField mfield = mlist.Fields[new Guid(“28cf69c5-fa48-462a-b5cd-27b6f9d2bd5f”)];

                                    tw.WriteLine(String.Empty);

                                    tw.WriteLine(“START OF LIST PROCESSING”);

                                    tw.WriteLine(“######################################################”);

                                    tw.WriteLine(“List Title: ” + mlist.Title + ” Field Title: ” + mfield.Title + ” FormBaseType ” + mfield.FromBaseType);

                                    tw.WriteLine(“List location:” + mweb.Url + “/” + mlist.RootFolder.Url);

 

                                    if (mfield.FromBaseType != true)

                                    {

                                        XmlTextReader xmlReader = new XmlTextReader(new StringReader(mfield.SchemaXml));

 

                                        XmlDocument xmlDocument = new XmlDocument();

 

                                        XmlNode xmlNode = xmlDocument.ReadNode(xmlReader);

 

                                        //xmlNode.Attributes[“Hidden”].Value = “FALSE”;

 

                                        XmlAttribute fromBaseType = xmlDocument.CreateAttribute(“FromBaseType”);

 

                                        fromBaseType.Value = “TRUE”;

 

                                        xmlNode.Attributes.Append(fromBaseType);

 

                                        tw.WriteLine(xmlNode.OuterXml);

 

                                        mfield.SchemaXml = xmlNode.OuterXml;

 

                                        mfield.Update();

                                    }

                                    tw.WriteLine(“######################################################”);

                                    tw.WriteLine(“END OF LIST PROCESSING”);

                                    tw.WriteLine(String.Empty);

                                }

                                catch (Exception ex)

                                {

                                    tw.WriteLine(“Error in list procesing of the MODIFIED FIELD FIX!!”);

                                    tw.WriteLine(ex.Message);

                                    tw.WriteLine(ex.StackTrace);

                                }

                                finally

                                {

                                    if (mweb != null)

                                        mweb.Dispose();

                                }

                            }

                            tw.WriteLine(“———————————————————————-“);

                            tw.Flush();

                        }

 

                    }

                }

                catch (Exception ex)

                {

                    tw.WriteLine(ex.Message);

                    tw.WriteLine(ex.StackTrace);

                }

                tw.WriteLine(output.ToString());

                tw.WriteLine(“END OF PROGRAM”);

                tw.Flush();

                tw.Close();

                Console.Write(output.ToString());

            }

        }

    }

}

Resources: SharePoint Development,Programming And PowerShell

Fields and Lists:

SPBuiltInFieldId Members (Microsoft.SharePoint)

Cherie Riesberg SharePoint Tips and Tricks: Adding a “Last Modified” Date on SharePoint Pages

SPListTemplateType Enumeration (Microsoft.SharePoint)

Managing Tasks Permissions Programmatically within SharePoint using event reciever or using special permissions property « Nishant Rana’s Weblog

SharePoint list item permissions – Stack Overflow

How to set Item Level Permission for SharePoint 2007 (MOSS/WSS) List/Document Library Programmatically – Pranab Paul’s Blog – Development Tips on SharePoint, Web, Office and OCS – Site Home – MSDN Blogs

Lists:

What I’ve Been Up To…ListViewByQuery and Its Nuances

Finding a SharePoint List Template Id – Cliff Green’s Blog – Site Home – MSDN Blogs

MOSS How to …: SharePoint List Template Id’s

ListViewByQuery – cannot go insde the folders SharePoint

The ListViewByQuery Control « Karine Bosch’s Blog

Create SharePoint List View Programmatically | Experience The Creativity

Queries, Data retrieval methods:

SharePoint Kings: Using SPSiteDataQuery class to execute cross site queries

SharePoint Large List Performance: SPMetal vs. SPQuery – End User – NothingButSharePoint.com

SPSiteDataQuery.Lists property (Microsoft.SharePoint)

Designing large lists and maximizing list performance (SharePoint Server 2010)

Get Recent Changes across Site Collections | Mike’s Notebook

Alexander Brütt » Blog Archive » Query whole site collection using SPSiteDataQuery

Wylde SharePoint etc.: Query list with CAML – DateTime column

Accessing Data From Sharepoint List

Performance of various methods to retrieve one list item – Waldek Mastykarz

SPSiteDataQuery Class (Microsoft.SharePoint)

Writing CAML Queries For Retrieving List Items from a SharePoint List | SharePoint Magazine

Adventures In SharePoint Land: Musing on CrossListQueryCache class

Content Query Web Part (CQWP), Cross List Query Nightmare Part 1

Increased performance for MOSS apps using the PortalSiteMapProvider – Microsoft Enterprise Content Management (ECM) Team Blog – Site Home – MSDN Blogs

SharePoint Applied: CAML, Your New Pet

Performance of content aggregation queries on multiple lists revisited – Waldek Mastykarz

Microsoft Enterprise Content Management (ECM) Team Blog : Performance

How to: Use the MOSS Enterprise Search FullTextSqlQuery Class – Corey Roth [MVP]

Know More: PortalSiteMapDataSource

Getting Started With SharePoint Web Services

Extreme SharePoint

SharePoint 2010 – LINQ and SPMetal – CodeProject

Linq to SharePoint vs Camlex.NET Performance Comparison

SharePoint 2010 SPSiteDataQuery with Example – CodeProject

PowerShell:

Use SharePoint 2010 PowerShell Cmdlets to Get and Manage Sites – Hey, Scripting Guy! Blog – Site Home – TechNet Blogs

Windows PowerShell for SharePoint Server 2010 reference

Best Practices:

Best Practices: Using Disposable Windows SharePoint Services Objects

Timer Job:

A Complete Guide to Writing Timer Jobs in SharePoint 2010

Modal Dialog:

Chaks’ Corner | Using the SharePoint 2010 Modal Dialog

Other:

SharePoint Validation Controls « Karine Bosch’s Blog

Linq to SharePoint vs Camlex.NET Performance Comparison

Working with URL’s in SharePoint | Michaël’s coding thoughts

.net – Encoding file paths – Stack Overflow

Run console application from a .NET application – CodeProject

Availability and description of the File Checksum Integrity Verifier utility

The Web application at XXX could not be found when using SharePoint Console « All About SharePoint

Using LINQ to SharePoint

C# Remove HTML Tags

Direct SharePoint: Hiding Workspace Checkbox while creating new sharepoint Event\Meeting

Programatically Send E-mail in Sharepoint 2010 : Learning SharePoint

Hide Create Meeting Workspace option when creating/editing a new calendar item in Sharepoint Services 3.0 – Spiceworks Community

john.livingston.tech: Enable Linq for SharePoint 2007

How to: Expire Actions with Post Actions and Bypass Locks in SharePoint Server 2010 (ECM)

Trick the Sharepoint Query String (URL) « Abstract Spaces

Using SharePoint 2010 Search Query String Parameters

Study guide to the exam 70-576: Microsoft SharePoint 2010 Designing and Developing « SharePoint Stream

Recreate your Upload.aspx file « Slinger’s Thoughts

How to enable page trace statistics?

Ten Must-Have .NET Tools Every Developer Should Download Now

SPViewCollection.Add Method (String, StringCollection, String, UInt32, Boolean, Boolean, SPViewCollection.SPViewType, Boolean) (Microsoft.SharePoint)

3 Different Ways to Display Progress in an ASP.NET AJAX Application

XPath, XQuery, and XSLT Function Reference

Programmatically creating a datasheet view

CleverWorkarounds » SharePoint Branding Part 6 – A “solution” to all issues?

Programming UpdateProgress Controls in Client Script

SharePoint 2007 & 2010 development: Code Block are not allowed in this file

Configure People Picker (SharePoint Server 2010)

The Script tag runat=”server” Problem Solution Using ResolveUrl – Daily Coding – daily coding tips and tricks, C#, .NET, ASP.NET, SQL, ResolveUrl, script runat=server, javascript runat=server, javascript ResolveUrl

NodeTypes Enumeration (Microsoft.SharePoint.Publishing)

Creating a CSV file and dealing with special characters. – Sebastien Lachance

SharePoint Debugging and Logging Tips and Tricks

SharePoint statistics: source processing – Serge van den Oever [Macaw]

PeopleEditor Control « Karine Bosch’s Blog

SharePoint Controls « Karine Bosch’s Blog

.Net Managed Client Object Model: Fetch All Webs, Libraries, Items « J.Shah

Understanding SharePoint: SPRequest « SharePoint Internals – Hristo Pavlov’s Blog

Debug tips for SharePoint

Dirty Words: Must-know SharePoint debugging tips

Tools:

Using the Developer Dashboard

Enable SharePoint Developer Dashboard with PowerShell « SharePoint Group

Resources: SharePoint List(s), List field(s)

Lists:

Passing default values to a SharePoint list’s NewForm.aspx page. « Tautic.com

Creating SharePoint 2010 List Definitions in Visual Studio 2010

DevExpertise » Blog Archive » SharePoint List Template IDs and Site Template IDs

SharePoint 2010 Base Types, List Template and Definition IDs, and Content Types IDs | Joel’s SharePoint Architect Blog

Create Custom Lists in CAML based on existing List Templates « Karine Bosch’s Blog

Working with Folders in SharePoint Lists In Code

How to use Rating in SharePoint 2010?

Using Notes Hyperlinks Within SharePoint Lists – MOSS Blog

ListTemplate Element (Site)

How to: Create a Custom List Definition

sharepoint2007 – Sharepoint List Definition that binds only to my Custom Content Type – Stack Overflow

Understanding Schema.xml Files

View Element

My time break for SharePoint: Hide List or Document Library With SharePoint Designer 2010 and ECMAScript

Hide Listtemplate from Create Dialog in PowerShell – SharePoint – Stack Exchange

Programmatically Creating And Programmatically Deleting List Templates Programmatically Via A SharePoint 2007 Programmatic Feature That Uses Code And Not XML Programmatically!

List of SharePoint Events « Development « Adis Jugo

Event Receiver Methods Compared: Windows SharePoint Services 3.0 and SharePoint Foundation 2010

SharePoint List Web Service GetListItems | Experience The Creativity

Hannah Scott’s Blog: List View Lookup Threshold

Fields And Items:

Frodes awesome list of SharePoint Column Field IDs – for SharePoint 2010 « aarebrot.net

FieldId class (Microsoft.SharePoint.Publishing)

SharePoint Calculated Columns – working days, weekends and holidays. | Pentalogic Technology

Calculated Field Formulas

Direct SharePoint: SharePoint – Programmatically creating comments of a blog post in blog site

A better way to obtain fields values of an SPListItem – Waldek Mastykarz

‘Modified’ field not getting updated anymore – Writing custom code for SharePoint – Site Home – MSDN Blogs

How to: Create a Custom Field Type and Field Control

Error : Invalid Field name. {7581e709-5d87-42e7-9fe6-698ef5e86dd3} when you try to browse the Master Page link under Site Settings

Updating SharePoint site columns programatically | Yaroslav Pentsarskyy on SharePoint Development

Techno Freak: Through javascript sets the default value of a lookup field identified MOSS 2007

Using Javascript to Manipulate a List Form Field – Microsoft SharePoint Designer Team Blog – Site Home – MSDN Blogs

How can I set the default value in a SharePoint list field, based on the value in another field? – Stack Overflow

Get or Set value for SharePoint field in NewForm, EditForm and DispForm (Get only in DispForm) « SharePoint JavaScripts

.Net & SharePoint ’07 – Getting\Setting values from\to the lookup, hyperlink and people column fields

Sharepoint Tips And Tricks: Code Practices – getting\setting values from\to the lookup and the hyperlink fields

Field Element (List)

System.What?: Free Last Modified By/Date From SharePoint

Managing Tasks Permissions Programmatically within SharePoint using event reciever or using special permissions property « Nishant Rana’s Weblog

SharePoint list item permissions – Stack Overflow

How to set Item Level Permission for SharePoint 2007 (MOSS/WSS) List/Document Library Programmatically – Pranab Paul’s Blog – Development Tips on SharePoint, Web, Office and OCS – Site Home – MSDN Blogs

How to: Display Custom Fields in a Content By Query Web Part

SharePoint Techie: Created and Modified Field went missing

SharePoint Modified column not updated (Network Steve Forum)

SharePoint Modified column not updated

Creating a custom column with a link to document (other than the name field) in document library

Documents:

How to configure Document expiration policy – Windows Sharepoint Technical Hints and Tips – Site Home – TechNet Blogs

Baris’s SharePoint Blog: Most viewed documents in SharePoint 2010

Open a workbook in Web browser view – SharePoint Server – Office.com

With SharePoint Designer:

How to set default values based on a query string and make those fields uneditable ~ IT Pro Ramblings