3

I have an existing sharepoint list generated by the sharepoint UI. I want to add a custom NewForm to this list that uses its own code behind, because I need to do a lot of stuff in the logic. As far as I have seen, it is not possible to manipulate the code behind of forms via Sharepoint Designer, so I´d rather reference a form created with Visual Studio in the list.

My problem is that the list already exists and that I don´t know how to dock a custom visual studio form to it. If I could create the list from scratch in VS everything would be fine.

Edit

I followed a post and deployed the form to the SP layouts folder. Along with it I deployed a feature and in the activated event receiver used the following code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite site = SPContext.Current.Site;
    using (SPWeb web = site.OpenWeb("mysite"))
    {

       SPList lstTest = web.Lists["MyList"];

       string newUrl = string.Format("{0}/{1}/layouts/NewForm.aspx", web.ServerRelativeUrl, lstTest.RootFolder.Url);

       var form = web.GetFile(newUrl);

       if (form != null && form.Exists)

       {
           lstTest.DefaultNewFormUrl = newUrl;
           lstTest.Update();
       }
    }
}

This did not work because form.Exists is false. If removed, I get the following error:

Unable to find an SPForm matching URL /mysite/Lists/MyList/layouts/NewForm.aspx. Parameter name: url

2
  • what is the "stuff" you need to do in the code behind? There might be a better way to achieve the functionality you need using a different approach. Commented Jul 5, 2013 at 17:52
  • SPWeb.GetFile can only be used on files represented by SPListItem, that is, files that exist in document libraries.
    – James Love
    Commented Oct 6, 2013 at 10:29

2 Answers 2

2

Yes, you could create custom page for your lists and libraries. Just simply crate a ASPX page in your solution, and create a class, which will inherits from base type of your list/library (ex. NewDocSet for page that created a new document set).

For doing something when an "OK" button on form pressed, just add a method to that class:

new public void OKBtn_Click(object sender, EventArgs e) {
    // do something with newly created item
    base.OKBtn_Click(sender, e);
}

Also, you need to your assembly with that custom class using this snippet:

<% @Assembly Name="Your.AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<assembly token goes here>" %>

And change the inherit property of page:

<% @Control Inherits="Your.AssemblyName.ClassName" %>

And finally, add the following code at the end of your content type definition:

<XmlDocuments> 
    <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms"> 
        <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
            <New><![CDATA[ path to your page on the server ]]></New> 
        </FormTemplates> 
    </XmlDocument> 
</XmlDocuments>

A fully documented process of creating custom form pages is here: Create custom SharePoint list form

1
  • To tie this in with OPs original method, instead of tryin to get the URL of the form and setting DefaultNewFormUrl property, this is the recommended method to set the forms of lists.
    – James Love
    Commented Oct 6, 2013 at 10:30
0

I guess it depends what you want to do in the form. You can certainly create new forms or edit existing ones in SPD. You can also add jQuery to manipulate the form.

1
  • The custom form will really be a little bit weird, and I need e.g. to get at the content of some other list in the web which will be loaded in the interface. Commented Jul 5, 2013 at 13:14

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.