net stop sptimerv3
net start sptimerv3
Tuesday, September 23, 2008
Restarting the Sharepoint Timer Service from the command line
When testing a new timer job you may need to restart the timer service for your changes in code to take effect, you can automate this restart in your deployment batch file with the following two commands:
Wednesday, September 10, 2008
Deploying the Regular Expression Custom Field Definition
Deploying the Regular Expression Custom Field Definition outlined in the previous post.
Sharepoint Solution packages (WSP) are Microsoft Cabinet files, the file extension .wsp is generally used to distinguish these files as associated with Sharepoint solutions.
The two popular ways to create these files are to either use a Visual Studio cabinet project, or use the Microsoft Cabinet Software Development Kit
This post will be using the latter method.
The custom field solution in Visual Studio looks like this:

The files on the file system are in exactly the same structure, note also how the TEMPLATE folder and its subfolders are in the same structure as TEMPLATE in the 12 directory on the server.
A Diamond Directive File (DDF) file is used by the MAKECAB.EXE program to create a cabinet file containing all the files that are required by the Sharepoint Solution.
Just four files are required to deploy the Regex Custom Field created in the earlier post:
The DLL is the assembly, the xml is the field definition, and the ascx is the control template. These are files that are to be deployed on to the server running Sharepoint. The WSP deployment process will put these files in their relevant places on the file system (into the TEMPLATE folder in 12, and the GAC for the DLL). Note that if the deployment platform is a FARM of Sharepoint servers, by deploying this WSP we are ensuring that all files will be deployed in the relevant places on each server in the farm. The WSP framework also ensures that if new servers are added to the farm, these machines will also be updated with the relevant objects on their file system.
The only file that is not deployed is the Manifest.xml file, this is used to inform Sharepoint of the finer details of the deployment process.
The manifest file for the regex custom field project looks like this:
Note: The SolutionId in the manifest file should be one generated with a GUID generator like GUIDGEN. The assembly references should contain references to the assembly generated by the source code. By strong naming this assembly the PublicKeyToken can be identified.
The manifest file contains references to the two file system objects, the Control Template, and the Field Definition, and their locations. The Assemblies section instructs Sharepoint to install the DLL to the GAC and to add a SafeControls section to the web.config of the Web Application to which this solution will be deployed.
For this simple WSP the DDF file (CABSettings.ddf) looks like this:
The DestinationDir in each case of the 12 directory objects is set relative to the 12/TEMPLATE folder. No DestinationDir is specified for manifest (as it has no destination directory, it is just used by the system), and the assembly, the assembly’s destination is specified in the manifest.
The files’ locations listed are relative to the Provisioning folder in the solution (where MAKECAB.EXE and the DDF are run from).
Assuming that MAKECAB’s path has been added to the environment variables, running makecab as follows will create the WSP file:
MAKECAB.EXE should give a friendly message describing compression, time elapsed and so on, but importantly how many files have been included in the cab. In this case four.
Note: This example makes a .cab file, an WSP is a cab file, Sharepoint cares not the slightest what its file extension is. To change it to output as a .wsp change the CabinetNameTemplate setting in the DDF file.
Sticking with the .cab file extension, the following file should have been created in the Provisioning folder:
It is worthwhile double checking the Paths of the files by inspecting the contents of the cab file.
This file needs to be copied to the Sharepoint server to be added to the Solution store and deployed to a site collection.
The solution can be deployed to the Sharepoint Server by using the STSADM command line tool:
(Assuming that STSADM ’s path has been added to the environment variables)
The custom field type should now be visible in the Create Column drop down of a Sharepoint List
Sharepoint Solution packages (WSP) are Microsoft Cabinet files, the file extension .wsp is generally used to distinguish these files as associated with Sharepoint solutions.
The two popular ways to create these files are to either use a Visual Studio cabinet project, or use the Microsoft Cabinet Software Development Kit
This post will be using the latter method.
The custom field solution in Visual Studio looks like this:
The files on the file system are in exactly the same structure, note also how the TEMPLATE folder and its subfolders are in the same structure as TEMPLATE in the 12 directory on the server.
A Diamond Directive File (DDF) file is used by the MAKECAB.EXE program to create a cabinet file containing all the files that are required by the Sharepoint Solution.
Just four files are required to deploy the Regex Custom Field created in the earlier post:
Manifest.xml
RegExTextField.ascx
fldtypes_RegExControl.xml
RegExField.dll
The DLL is the assembly, the xml is the field definition, and the ascx is the control template. These are files that are to be deployed on to the server running Sharepoint. The WSP deployment process will put these files in their relevant places on the file system (into the TEMPLATE folder in 12, and the GAC for the DLL). Note that if the deployment platform is a FARM of Sharepoint servers, by deploying this WSP we are ensuring that all files will be deployed in the relevant places on each server in the farm. The WSP framework also ensures that if new servers are added to the farm, these machines will also be updated with the relevant objects on their file system.
The only file that is not deployed is the Manifest.xml file, this is used to inform Sharepoint of the finer details of the deployment process.
The manifest file for the regex custom field project looks like this:
<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="E3F3F75B-E76D-4f6e-82C0-0827DB82F925">
<TemplateFiles>
<TemplateFile Location="ControlTemplates\RegExTextField.ascx" />
<TemplateFile Location="XML\fldtypes_RegExControl.xml" />
</TemplateFiles>
<Assemblies>
<Assembly Location="RegExField.dll" DeploymentTarget="GlobalAssemblyCache">
<SafeControls>
<SafeControl Assembly="RegExField, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9ba34c98dffd3009" Namespace="RegExField" Safe="True" TypeName="*" />
</SafeControls>
</Assembly>
</Assemblies>
</Solution>
Note: The SolutionId in the manifest file should be one generated with a GUID generator like GUIDGEN. The assembly references should contain references to the assembly generated by the source code. By strong naming this assembly the PublicKeyToken can be identified.
The manifest file contains references to the two file system objects, the Control Template, and the Field Definition, and their locations. The Assemblies section instructs Sharepoint to install the DLL to the GAC and to add a SafeControls section to the web.config of the Web Application to which this solution will be deployed.
For this simple WSP the DDF file (CABSettings.ddf) looks like this:
.OPTION Explicit
.Set CabinetNameTemplate="RegExField.cab"
.Set DiskDirectory1=
manifest.xml
;** REGEX Control Template
.Set DestinationDir=CONTROLTEMPLATES
..\TEMPLATE\CONTROLTEMPLATES\RegExTextField.ascx
;** REGEX Field Def
.Set DestinationDir=XML
..\TEMPLATE\XML\fldtypes_RegExControl.xml
;** DOCUMENT LIBRARY CODE
.Set DestinationDir=
..\bin\Debug\RegExField.dll
The DestinationDir in each case of the 12 directory objects is set relative to the 12/TEMPLATE folder. No DestinationDir is specified for manifest (as it has no destination directory, it is just used by the system), and the assembly, the assembly’s destination is specified in the manifest.
The files’ locations listed are relative to the Provisioning folder in the solution (where MAKECAB.EXE and the DDF are run from).
Assuming that MAKECAB’s path has been added to the environment variables, running makecab as follows will create the WSP file:
MAKECAB.EXE /f CABSettings.ddf
MAKECAB.EXE should give a friendly message describing compression, time elapsed and so on, but importantly how many files have been included in the cab. In this case four.
Note: This example makes a .cab file, an WSP is a cab file, Sharepoint cares not the slightest what its file extension is. To change it to output as a .wsp change the CabinetNameTemplate setting in the DDF file.
Sticking with the .cab file extension, the following file should have been created in the Provisioning folder:
RegExField.cab
It is worthwhile double checking the Paths of the files by inspecting the contents of the cab file.
This file needs to be copied to the Sharepoint server to be added to the Solution store and deployed to a site collection.
The solution can be deployed to the Sharepoint Server by using the STSADM command line tool:
(Assuming that STSADM ’s path has been added to the environment variables)
STSADM -o addsolution -filename RegExField.cab
STSADM -o deploysolution -name RegExField.cab -immediate -allowGacDeployment -force -url [URL of SiteCollection]
STSADM -o execadmsvcjobs
The custom field type should now be visible in the Create Column drop down of a Sharepoint List
Creating a custom text field in SharePoint to validate by a regular expression
The purpose of this exercise was to create a field that could be used as a column in a SharePoint list that would validate text entered against a regular expression defined by the user that set up the column.
Create a control template ascx file in the 12\TEMPLATE\CONTROLTEMPLATES folder containing:
Create a field definition file in the 12\TEMPLATE\XML folder containing:
And create two classes in your source code for your assembly:
And:


The next post will be concerned with wrapping this field definition into a deployable Sharepoint Solution Package (wsp).
Create a control template ascx file in the 12\TEMPLATE\CONTROLTEMPLATES folder containing:
<%@ Control Language="C#" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<SharePoint:RenderingTemplate ID="txtFieldRendering" runat="server">
<Template>
<asp:TextBox ID="RegExText" CssClass="ms-long" runat="server"></asp:TextBox><br />
</Template>
</SharePoint:RenderingTemplate>
Create a field definition file in the 12\TEMPLATE\XML folder containing:
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">RegExField</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">RegEx Text Field</Field>
<Field Name="TypeShortDescription">Text field that is validated by a regular expression</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">[YOUR ASSEMBLY REFERENCE HERE]</Field>
<PropertySchema>
<Fields>
<Field Name="RegularExpression" DisplayName="Regular Expression" MaxLength="1024" DisplaySize="50" Type="Note">
<Default>w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*</Default>
</Field>
<Field Name="ErrorMessage" DisplayName="Error Message" MaxLength="255" DisplaySize="50" Type="Text">
<Default>Please enter a valid email address</Default>
</Field>
</Fields>
</PropertySchema>
<RenderPattern Name="DisplayPattern">
<Switch>
<Expr>
<Column />
</Expr>
<Case Value="" />
<Default>
<Column SubColumnNumber="0" HTMLEncode="True" />
</Default>
</Switch>
</RenderPattern>
</FieldType>
</FieldTypes>
And create two classes in your source code for your assembly:
using System;
using System.Web;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace RegExField
{
public class RegExControlField : SPFieldText
{
public RegExControlField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public RegExControlField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
}
public override string GetValidatedString(object value)
{
String txtToValidate = value.ToString();
if (Required || txtToValidate.Trim() != "")
{
Regex exp = new Regex(GetCustomProperty("RegularExpression").ToString());
String err = GetCustomProperty("ErrorMessage").ToString();
if (!exp.IsMatch(txtToValidate))
throw new SPFieldValidationException(err);
}
return base.GetValidatedString(value);
}
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fieldControl = new RegExControlFieldControl();
fieldControl.FieldName = this.InternalName;
return fieldControl;
}
}
}
}
And:
using System;Compile the classes above into an strong named assembly, install into the GAC, amend the reference in the field definition (the xml file), and an iisreset. The field should then be available to choose when creating a new column in a list.
using System.Web;
using System.Web.UI;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace RegExField
{
public class RegExControlFieldControl : BaseFieldControl
{
protected System.Web.UI.WebControls.TextBox txtRegExText;
protected override string DefaultTemplateName
{
get
{
return "txtFieldRendering";
}
}
public override object Value
{
get
{
EnsureChildControls();
return this.txtRegExText.Text;
}
set
{
EnsureChildControls();
this.txtRegExText.Text = this.ItemFieldValue.ToString();
}
}
protected override void CreateChildControls()
{
if (Field == null || ControlMode == SPControlMode.Display)
return;
base.CreateChildControls();
this.txtRegExText = (System.Web.UI.WebControls.TextBox)TemplateContainer.FindControl("RegExText");
}
}
}
The next post will be concerned with wrapping this field definition into a deployable Sharepoint Solution Package (wsp).
Subscribe to:
Posts (Atom)
