Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Derek Gusoff Jul 5 '13 at 17:52
    
SPWeb.GetFile can only be used on files represented by SPListItem, that is, files that exist in document libraries. –  James Love Oct 6 '13 at 10:29

2 Answers 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

share|improve this answer
    
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 Oct 6 '13 at 10:30

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.

share|improve this answer
    
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. –  AGuyCalledGerald Jul 5 '13 at 13:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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