Monthly Archives: April 2010

How to add custom validators to the SharePoint ListFieldIterator

Requirements

Today I had the requirement to add custom validation logic to a SharePoint edit form. The edit form is being displayed using a ListFieldIterator. The requirement was to make sure the Date field could not be set to a date in the past.

The solution (Other solutions are available and are most probably better!) I came up with involved subclassing ListFieldIterator and attaching ASP.NET validators at run-time.

Challenge 1

How do you attach a ASP.NET validation control at run-time.

  • Attach the validator at the correct stage of the page/control life-cycle. In the case of the ListFieldIterator I wanted to attach it after the control had build it’s control tree. Override CreateChildControls, call base, then attach.
  • Find the FormField associated with the SharePoint field (SPField) you want to validate.I have an extension method that I use to parse the control hierarchy of a ListFieldIterator as follows:

Usage:

FormField formField = listFieldIterator.GetFormField("MyInternalFieldName");

public static class ListFieldIteratorExtensions
{
    public static FormField GetFormField(this ListFieldIterator listFieldIterator, string fieldName)
    {
        return GetFormField(listFieldIterator, GetFormFields(listFieldIterator), fieldName);
    }


    public static FormField GetFormField(this ListFieldIterator listFieldIterator, List<FormField> formFields, string fieldName)
    {
        FormField formField = (from form in formFields
                               where form.FieldName.Equals(fieldName, StringComparison.InvariantCultureIgnoreCase)
                               select form).FirstOrDefault();

        if (formField == null)
        {
            throw new GeneralApplicationException("Could not find form field: " + fieldName);
        }

        return formField;
    }

    public static List<FormField> GetFormFields(this ListFieldIterator listFieldIterator)
    {
        if (listFieldIterator == null)
        {
            return null;
        }

        return FindFieldFormControls(listFieldIterator);
    }        

    private static List<FormField> FindFieldFormControls(System.Web.UI.Control root)
    {
        List<FormField> baseFieldControls = new List<FormField>();

        foreach (System.Web.UI.Control control in root.Controls)
        {
            if (control is FormField && control.Visible)
            {
                FormField formField = control as FormField;
                if (formField.Field.FieldValueType == typeof(DateTime))
                {
                    HandleDateField(formField);
                }

                baseFieldControls.Add(formField);
            }
            else
            {
                baseFieldControls.AddRange(FindFieldFormControls(control));
            }
        }

        return baseFieldControls;
    }

    private static void HandleDateField(FormField formField)
    {
        if (formField.ControlMode == SPControlMode.Display)
        {
            return;
        }

        Control dateFieldControl = formField.Controls[0];
        if (dateFieldControl.Controls.Count > 0)
        {
            DateTimeControl dateTimeControl = (DateTimeControl) dateFieldControl.Controls[0].Controls[1];
            TextBox dateTimeTextBox = dateTimeControl.Controls[0] as TextBox;
            if (dateTimeTextBox != null)
            {
                if (!string.IsNullOrEmpty(dateTimeTextBox.Text))
                {
                    formField.Value = DateTime.Parse(dateTimeTextBox.Text, CultureInfo.CurrentCulture);
                }
            } 
        }
    }
}
  • Find the Control that is rendered by the FieldControl.Field.FieldRenderingControl. In my specific case a DateTimeField will render a DateTimeControl. Now that we have the form field we grab the rendering control:

Usage:

Control renderedControl = GetControl(formField);

private static Control GetControl(FieldMetadata formField)
{
    return formField.FindControlRecursive(x => x.GetType() == GetChildControlBasedOnFieldType(formField.Field.FieldRenderingControl));
}

private static Type GetChildControlBasedOnFieldType(object field)
{
    if (field is TextField)
    {
        return typeof(TextBox);
    }

    if (field is DropDownChoiceField)
    {
        return typeof(DropDownList);
    }

    if (field is DateTimeField)
    {
        return typeof (DateTimeControl);
    }

    return null;
}

public static Control FindControlRecursive(this Control control, Func<Control, bool> evaluate)
{
    if (evaluate.Invoke(control))
    {
        return control;
    }

    foreach (Control childControl in control.Controls)
    {
        Control foundControl = FindControlRecursive(childControl, evaluate);
        if (foundControl != null)
        {
            return foundControl;
        }
    }

    return null;
} 

  • Now we have found the control we want to validate, we can add the ASP.NET to it’s parent’s control collection

renderedControl.Parent.Controls.AddAfter(control, validator as Control);

Uses another little extension method:

public static void AddAfter(this ControlCollection collection, Control after, Control control)
{
    int indexFound = -1;
    int currentIndex = 0;
    foreach (Control controlToEvaluate in collection)
    {
        if (controlToEvaluate == after)
        {
            indexFound = currentIndex;
            break;
        }

        currentIndex = currentIndex + 1;
    }

    if (indexFound == -1)
    {
        throw new ArgumentOutOfRangeException("control", "Control not found");
    }

    collection.AddAt(indexFound + 1, control);
}

Tagged ,

How do you update the ‘Author’ or ‘Created by’ and ‘Editor’ or ‘Modified By / Last Modified’ fields of a list item (SPListItem)

Sometimes it’s useful to overwrite the created by and last modified by fields and get rid of that pesky ‘System Account’ !

Created By

The internal field name for the person who created a list item is ‘Author’ use SPBuiltInFieldId.Author to access this field. The display name for this field is ‘Created By’ it can be seen in the UI circled below:

SPListItem CreatedBy Author

Last modified

The internal field name for the person who created a list item is ‘Editor’ use SPBuiltInFieldId.Editor to access this field. The display name for this field is ‘Modified By’ it can be seen in the UI circled below:

SPListItem ModifiedBy Editor

Updating Created By, Modified By

The trick here is to call SPListItem.UpdateOverwriteVersion() instead of SPListItem.Update()

SPListItem item = list.Items[0];
item[SPBuiltInFieldId.Author] = "1;#Edward Wilde";
item[SPBuiltInFieldId.Editor] = "1;#Edward Wilde";
copiedItem.UpdateOverwriteVersion();
Tagged , ,

Typemock: System.NotSupportedException: Cannot dynamically create an instance of System.Void.

Had a problem mocking a call to SPWorkflowActivationProperties.Item today. Turns out to be a bug in the TypeMock library.

Problematic unit test

[Test, Isolated]
        public void PublishToExternalDocumentLibrary_Calls_CopyTo_Correctly()
        {
            ApprovalWorkflow approvalWorkflow = Isolate.Fake.Instance<ApprovalWorkflow>(Members.CallOriginal, ConstructorWillBe.Called);
            SPWorkflowActivationProperties activationProperties = Isolate.Fake.Instance<SPWorkflowActivationProperties>(Members.CallOriginal, ConstructorWillBe.Called);
            SPListItem item = Isolate.Fake.Instance<SPListItem>();

            Isolate.WhenCalled(() => approvalWorkflow.PublishWhenComplete).WillReturn(true);
            Isolate.WhenCalled(() => approvalWorkflow.PublishDocumentLibraryUrl).WillReturn(ExternalDocumentLibraryUrl);
            Isolate.WhenCalled(() => approvalWorkflow.WorkflowActivationProperties).WillReturn(activationProperties);
            Isolate.WhenCalled(() => activationProperties.Item).WillReturn(item);
           
            Isolate.WhenCalled(() => item.Name).WillReturn("test.docx");
           
            // Fire the workflow completed event
            Isolate.Invoke.Event(() => approvalWorkflow.Completed += null, new object[] { this, EventArgs.Empty });

            Isolate.Verify.WasCalledWithExactArguments(() => item.CopyTo("http://xdev03.trading.ad.int.corp.local/Policies/test.docx"));
        } 

When executed it throws the following exception:

System.NotSupportedException: Cannot dynamically create an instance of System.Void.
at System.RuntimeType.CreateInstanceCheckThis()
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at hi.b(MethodBase A_0)
at hi.a(MethodBase A_0, fk A_1)
at a.a(fk A_0)
at a.c(fk A_0)
at fz.a(fk A_0)
at fz.a(Object A_0, MethodBase A_1, Object[] A_2)
at fz.a(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
at Microsoft.SharePoint.Security.SharePointPermissionAttribute.CreatePermission()
at System.Security.PermissionSet.CreateSerialized(Object[] attrs, Boolean serialize, Byte[]& nonCasBlob, PermissionSet& casPset, HostProtectionResource fullTrustOnlyResources)
at Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties.get_Item()
at Total.SharePoint.Approval.Workflow.Test.ApprovalWorkflowFixture.<>c__DisplayClassd.<PublishToExternalDocumentLibrary_Calls_CopyTo_Correctly>b__9() in ApprovalWorkflowFixture.cs: line 44
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at Total.SharePoint.Approval.Workflow.Test.ApprovalWorkflowFixture.PublishToExternalDocumentLibrary_Calls_CopyTo_Correctly() in ApprovalWorkflowFixture.cs: line 0 

Fix:
I can work around this issue by replacing the line:

Isolate.WhenCalled(() => activationProperties.Item).WillReturn(item);

With:

Isolate.NonPublic.WhenCalled(activationProperties, "get_Item").WillReturn(item); 

See thread on Typemock forum

Tagged ,

InfoPath forms with workflow: ‘The form has been closed’ or Exception occurred during request processing. (User: {Domain}\{UserName}, Form Name: , IP: , Request: http://localhost/_layouts/CstWrkflIP.aspx?List={ID}, Form ID: , Type: InfoPathLocalizedException, Exception Message: The specified form cannot be found.)

Okay so today’s error message, totally my fault: was

Exception occurred during request processing. (User: DOMAIN\xx, Form Name: , IP: , Request: http://localhost/_layouts/CstWrkflIP.aspx?List={ID}, Form ID: , Type: InfoPathLocalizedException, Exception Message: The specified form cannot be found.)

Make sure that:

  • Your feature file has the RegisterFroms property correctly pointing to a folder in your solution package that contains your infopath forms

    WindowClipping (23)
    In my example all my info paths forms are contained in a sub directory called ‘Forms’ relative to feature file:
    WindowClipping (24)

  • Once the feature has been installed using stsadm –o install feature check that the forms have been placed in the InfoPath template library.

    Central Administration –> Application Management –> Manage Form Templates:

    WindowClipping (25)

As always, your mileage my vary. If this doesn’t help you could try some of the tips on Lucy’s blog: http://blogs.tamtam.nl/lucy/2007/06/08/WorkflowErrors.aspx

Tagged ,