Question

We have a requirement where there should be a button in visual web part and on button click, it would create custom new/edit forms for fix SharePoint list.

I have tried below code to create a new aspx page with web part which has all columns required to work as a new custom form.

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
    using (SPWeb oWeb = site.OpenWeb())
    {
        oWeb.AllowUnsafeUpdates = true;
        SPList oList = oWeb.Lists.TryGetList("Custom Forms Test");
        if (oList != null)
        {
            SPFile newForm = oList.RootFolder.Files.Add(oList.RootFolder.ServerRelativeUrl + "/CustomNewForm.aspx", GetFormBytes(FormControlMode.New.ToString(), oList, "Item"), true);
            SPFile editForm = oList.RootFolder.Files.Add(oList.RootFolder.ServerRelativeUrl + "/CustomEditForm.aspx", GetFormBytes(FormControlMode.Edit.ToString(), oList, "Item"), true);

            var a = newForm.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
            NewCustomForms sp = new NewCustomForms(); //This custom web part has fields for the columns.

            a.AddWebPart(sp, "Main", 1);

            oList.Update();
            var newFormUrl = string.Format("{0}{1}/CustomNewForm.aspx", oWeb.ServerRelativeUrl, oList.RootFolder.Url);
            var editFormUrl = string.Format("{0}{1}/CustomEditForm.aspx", oWeb.ServerRelativeUrl, oList.RootFolder.Url);
            var form = oWeb.GetFile(newFormUrl);
            if (form != null && form.Exists)
            {
                oList = oWeb.Lists[oList.ID];
                oList.DefaultNewFormUrl = oList.RootFolder.ServerRelativeUrl + "/CustomNewForm.aspx";
                oList.Update();
            }
        }
        oWeb.AllowUnsafeUpdates = false;
    }
}

But when I try to set this page's URL in DefaultNewFormUrl parameter in SPList object, it gives me below error:

Unable to find an SPForm matching URL /Lists/Custom Forms Test/CustomNewForm.aspx. Parameter name: url

I am able to set custom forms for the content types added in list using below code chunk:

oList.ContentTypes["Item"].NewFormUrl = newFormUrl;
oList.ContentTypes["Item"].EditFormUrl = editFormUrl;
oList.ContentTypes["Item"].Update();

But then these pages will work as normal pages of the site instead of form pages and due to that it don't contains the Edit tabs of ribbon in form which is coming in OOTB forms. See attached images for reference:

OOTB form:

enter image description here

Custom form: (You can see, Edit ribbon tab is not coming)

enter image description here

I found few references where they are using iListWebPart interface to make a custom web part for these forms but they are using PowerShell and not working though:

Please guide me with any solution or a way to create a custom forms for SharePoint lists which can be connected to SharePoint list and has "Edit" ribbon tab similar to OOTB forms.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top