Monday, 4 February 2008

SPContext.Current.FormContext

SPContext.Current.FormContext is my new best friend !
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spformcontext_members.aspx

FormContext has this super Method
1. SetFormMode Use it to change the current SPControlMode (haven't tried this out)

FormContext has two awesome Properties
1. FieldControlCollection - Get a collection of the Field Controls.
2. FormMode - Get the Current SPControlMode

What is SPControlMode?
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spcontrolmode.aspx
Editing content in a MOSS Publishing Site is done “in context” ie. You navigate to a page, and “Edit” the page, after which the page "mode" switches and you are presented with data entry controls that let you enter data into page fields.

So a Page can be in different modes, very similar in concept to how MCMS 2002 functions.

So we need a nice way to determine the “SPControlMode” programmatically in custom code so we can control rendering (do cool stuff) when in different modes.

The SPControlMode enumeration contains these values -
Display, Edit, Invalid and New

Some example CODE that makes use of FormContext.FormMode -
' If Me not in Display Mode, Hide Me
If (SPContext.Current.FormContext.FormMode <> Microsoft.SharePoint.WebControls.SPControlMode.Display) Then
Me.Visible = False
End If

2 comments:

Anonymous said...

just what I needed - thanks!

dotNetFollower said...

Hello! There is only one problem with FormMode - in the most cases, this property returns Invalid value inside OnInit-method. But sometimes to get the correct SPControlMode value in OnInit is strictly necessary. I've developed a special method for that. The method is described in this article - Get SPControlMode, which is associated with the current request. I hope it will help somebody out.