Since today I want to write about the object model of SharePoint 2007  because there are some goodies that helped me a lot when implementing solutions for Microsoft Office SharePoint Server 2007 and Windows SharePoint Services 3.0.

I want to start with the namespaces

  • Microsoft.SharePoint
  • Microsoft.SharePoint.WebControls
  • Microsoft.SharePoint.Publishing
  • Microsoft.SharePoint.Publishing.WebControls

which can be found at:

  • Microsoft.SharePoint.dll
  • Microsoft.SharePoint.Publishing.dll

Microsoft.SharePoint and Microsoft.SharePoint.WebControls

Programmatically determine the display mode or edit mode of a website

Imagine the following scenario: You have a WCMS website and want to programmatically determine if the whole website is in display mode or edit mode. The namespace

using Microsoft.SharePoint.WebControls;

offers the SPControlMode enumeration with the members:

  • Display
  • Edit
  • Invalid
  • New

You can check the current mode by using the SPContext.Current object:

if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
{
}

If you want implement a custom WebControl you can use Microsoft.SharePoint.WebControls.SPControlMode. Display to determine if your custom WebControl is in display or edit mode.

Programmatically adding FieldControls, OnLoad, CreateChildControls

Imagine the following scenario: You have a WCMS website and want to programmatically add FieldControls depending on a certain situation. The namespace you can utilize is

using Microsoft.SharePoint.WebControls;

You have different options adding FieldControls at runtime. One option is a UserControl you can call in the page layout. You can override the method CreateChildControls() of the UserControl.

protected override void CreateChildControls()
{
    base.CreateChildControls();
 
    //If current page is in edit mode 
    if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
    {
        TextField txtField = new TextField();
        txtField.ID = "FieldControlID";
        txtField.FieldName = "FieldControlFieldName";
 
        DateTimeField dateField = new DateTimeField();
        dateField.ID = "FieldControlID";
        dateField.FieldName = "FieldControlFieldName";
 
        DropDownChoiceField ddField = new DropDownChoiceField();
        ddField.ID = "FieldControlID";
        ddField.FieldName = "FieldControlFieldName";
 
        this.Controls.Add(txtField);
        this.Controls.Add(dateField);
        this.Controls.Add(ddField);
    }
}

This example adds FieldControls of type Text, DateTime and Choice to the page. The FieldControlFieldName has to be the name of a column attached to the pages library (ideally by a content type ;) ).

The FieldControlFieldName has to be the name of a column attached to the pages library.

If you want to get access to the FieldControls value you can use the property ItemFieldValue:

TextField.ItemFieldValue
DateTimeField.ItemFieldValue
DropDownChoiceField.ItemFieldValue

In order to access a FieldControl of type RichHtml you should use

SPFieldMultiLineText richHtml = new SPFieldMultiLineText(SPContext.Current.Fields, 
                                                         "FieldControlFieldName");

where FieldControlFieldName is again the name of a column attached to the pages library.

You can access the FieldControls value by using

object value = rightColumn.FieldRenderingControl.ItemFieldValue;

If you use this way you shouldn't get the following error:

Unable to cast object of type 'Microsoft.SharePoint.Publishing.Fields.HtmlField' to type 'Microsoft.SharePoint.SPFieldText'.

Microsoft.SharePoint.Publishing and Microsoft.SharePoint.Publishing.WebControls

Microsoft.SharePoint.Publishing.PublishingWeb

The namespace

using Microsoft.SharePoint.Publishing; 

offers a class called PublishingWeb. You can use it to check if the current SPWeb is of type PublishingWeb:

SPWeb currentWeb = SPContext.Current.Web;

 

if (PublishingWeb.IsPublishingWeb(currentWeb))

{
    pubWeb = PublishingWeb.GetPublishingWeb(currentWeb);
 
    string title = pubWeb.DefaultPage.Title;
    string title = pubWeb.DefaultPage.ServerRelativeUrl;
}

 If a SPWeb object is of kind PublishingWeb you can create an object of type PublishingWeb where you can easily access the default page object.

Furthermore you can use the out of the box dialogs (i.e. for browsing the site in order to get the URL of a list) when using

using Microsoft.SharePoint.Publishing.Fields;
using Microsoft.SharePoint.Publishing.WebControls;

 

image 

RichLinkSelector mLinkSelector = new RichLinkSelector();
HtmlTagValue htmlTagValue = mLinkSelector.Value;

If you want to get an object where you can access properties like the text or the navigation URL you can use the LinkFieldValue:

LinkFieldValue link = new LinkFieldValue(mLinkSelector.Value.ToString());

There you can get and set properties like 

link.Text = "";
link.NavigateUrl = "";
link.Target = "";
link.ToolTip = "";

 You can also set a predefined URL for the RichLinkSelector:

mLinkSelector.Value = link;

That's it. The SharePoint object model has a lot of cool stuff but sometimes you have to know about it. I hope there are some goodies for you... Please remember that this is not a best practice article since it should only give an overview of what is possible with the SharePoint object model.


Get FREE & Advanced SharePoint Training, how-to's, tips & tricks:
Learn SharePoint

Comments (1) -

1/9/2012 2:27:56 AM #

dotNetFollower

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 (dotnetfollower.com/.../). I hope it will help somebody out.

dotNetFollower United States Reply

Add comment


Loading