1

I have created a custom form where I add "Mode" parameter to query string based on the type of form I want to display: New/Display/Edit. Based on the mode from query string I set the ControlMode property of FormFields from code-behind.

I have attached custom forms in a Web-scoped Feature:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    web.AllowUnsafeUpdates = true;
    SPList myList = web.Lists.TryGetList("MyList");
    if (myList != null)
    {
        myList.ContentTypes["MyCustomContentType"].EditFormUrl = "_layouts/MyProject/MyForm.aspx?Mode=Edit";
        myList.ContentTypes["MyCustomContentType"].NewFormUrl = "_layouts/MyProject/MyForm.aspx?Mode=New";
        myList.ContentTypes["MyCustomContentType"].DisplayFormUrl = "_layouts/MyProject/MyForm.aspx?Mode=Display";
        myList.ContentTypes["MyCustomContentType"].Update();
    }
    web.AllowUnsafeUpdates = false;
}

Nonetheless there is a need to specify a ControlMode for FormField controls in aspx page, otherwise an error occurs when displaying a page. And that's where the problem begins:

  • if I set them to ControlMode="New" in aspx and setting to ControlMode.New from code-behind - New Form is ok

  • if I set them to ControlMode="New" in aspx and setting to ControlMode.Display from code-behind - SPContext.Current.ItemId is 0 so the form is not created as Display Form

  • if I set them to ControlMode="Display" in aspx and setting to ControlMode.New from code-behind - a folder is created instead of item

  • if I set them to ControlMode="Display" in aspx and setting to ControlMode.Display from code-behind - Display Form is ok

I've tried setting the ControlMode property from different event like OnPreInit, OnInit, CreateChildControls, Page_Load without success.

I've even tried to set the properties in aspx literally from code-behind using tricks like ControlMode="<%# _formMode %>" also without success.

The question is: why SharePoint sticks hardly to ControlMode set in aspx page and gets confused when switching it from code-behind? Is creating separate aspx forms for each mode the only solution?

3
  • 1
    Question: Why do you want one page? To the user there is no UX difference, other then the URL. You are now working against the grain, and having to pay for it. If there was an advantage in a single-Form; Microsoft would have refacterod their SP2001 approach a long time ago Commented May 9, 2017 at 9:13
  • @Danny'365CSI'Engelman Because if there is a requirement to change something on the form (for example order of the fields) I change it in 1 place instead of 3 places. Commented May 9, 2017 at 9:17
  • That's why Microsoft came up with CSR .. And yes I wouldn't be surprised if they tried a single-form as well.. and then they learned they shouldn't mess with SP2001 code. Take into account CSR is no longer there in SPOnline Modern Experiences (SPFx).. but neither is that SP2001 code. SharePoint finally is becoming a modern SPA... But the essence is the same as CSR, no more back-end code, it's the Client that requests content. So CSR code you develop now, should easily be ported to SPFx (it's all JavaScript). Use Cisar for CSR development Commented May 9, 2017 at 9:20

1 Answer 1

0

I have chosen the scenario where I set the controls to ControlMode="Display" but I use code-behind workaround in the form's OnInit method to force SharePoint to create an item instead of a folder with this code:

item.FileSystemObjectType = SPFileSystemObjectType.File;

Looks like all my scenarios work fine with this tweak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.