none
Creating page based on EditForm.aspx w/codebehind RRS feed

  • Question

  • Hi,

    I have a requirement to build a page which loads the values of the properties from an existing document library item into the page (EditForm.aspx), have the user edit those fields,  and upload a document, and then when a button is clicked the new document is added to the document library with the new property values.

    I am thinking of creating a new page based on the EditList.aspx page and adding a save and upload button and then, after making this page inherit from a custom code-behind dll,  to capture the events of the save and upload buttons to add a new item with the modified field values.


    My question is whether I am going about this in the right way, and/or whether there are any better/easier ways of doing this.
    thanks
    • Changed type Mike Walsh FIN Saturday, April 25, 2009 5:45 AM always use the question type so people get credit for their replies to you
    Friday, April 24, 2009 8:54 PM

Answers

  • It all depends on what you are trying to accomplish. If you are for instance just trying to add extra metadata at the time an item is being edited you may be better served using a event handler on the document library and doing it there. On the other hand, if you are trying to add functionality to the indexing process like custom lookup fields etc then creating an EditForm.aspx is certainly achievable. In the past I have taken the EditForm and inherited from a custom code behind dll. I inherited from Microsoft.SharePoint.WebControls.LayoutsPageBase.  When customiizing your EditForm.aspx page it pays to have a very good understanding of what Microsoft.SharePoint.WebControls are available.  It will also pay to encapsulate your indexing logic into your own webcontrol.  Then you can hook into the OK and Cancel button events by using the _controltemplates/ToolBar.ascx user control, below is an example. You can see the code behind has access to the SharePoint context and you can use it to easily navigate back or update the item that needs to be edited. After creating the page and the assembly, deploy the assembly to the GAC and the page to the layouts directory. In order for SharePoint to find the new page it must be in the layouts directory. Finally, in order to SharePoint to automatically use your new EditForm you must assign it to a contenttype programatically. You can look at the following link for that information.  http://www.sharepointblogs.com/smc750/archive/2007/11/20/setting-a-custom-edit-form-for-a-content-type.aspx

     <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl2" RightButtonSeparator="&nbsp;" runat="server">
    			<Template_Buttons>
    				<SharePoint:CreatedModifiedInfo ID="CreatedModifiedInfo1" runat="server" ControlMode="Display"/>
    			</Template_Buttons>
    			<Template_RightButtons>
    			    <asp:Button ID="Button1"  Text="OK"  runat="server" onclick="SaveToSP_Click" CssClass="ms-ButtonHeightWidth"/> 
    		        <asp:Button ID="Button2"  Text="Cancel"  runat="server" onclick="GoBack_Click" CssClass="ms-ButtonHeightWidth"/>
    		     </Template_RightButtons>    
            </wssuc:ToolBar> 
     protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
    
                //get list item
                if (string.IsNullOrEmpty(Request.Params["List"]) || string.IsNullOrEmpty(Request.Params["ID"]))
                    throw new ArgumentException("No item to edit./");
    
                _item = SPControl.GetContextWeb(HttpContext.Current).Lists[new Guid(Request.Params["List"])].GetItemById(int.Parse(Request.Params["ID"]));
    
                if (_item == null)
                    throw new Exception("No list item found.");
    
     
            }
    
    
    
     protected void SaveToSP_Click(object sender, EventArgs e)
            {
    
                try
                {
                    _item.Update();
    
                       _item = SPControl.GetContextWeb(HttpContext.Current).Lists[new Guid(Request.Params["List"])].GetItemById(int.Parse(Request.Params["ID"]));
    
                    if (_item.File.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                    {
                        _item.File.CheckIn(string.Empty);
                    }
    
                    Response.Redirect(Request.Params["Source"]);
                   
                }
                catch (Exception ex)
                {
                    this.ErrorLabel.Text = "Unable to save your changes due to the following error: " + "<br>" + ex.Message.ToString();
    
                }
    
            }
    
       protected void GoBack_Click(object sender, EventArgs e)
            {
                Response.Redirect(Request.Params["Source"]);
               
            }

    http://www.certdev.com
    Saturday, April 25, 2009 2:17 PM

All replies

  • I don't think this approach may work.  I've tried to add a code-behind(PageLoad events) for newform.aspx using the following way
    http://msdn.microsoft.com/en-us/library/bb986729.aspx

    Using this approach you can wire events to the page using code-behind. I don't think you can add a new control to the page using this approach.

    I'd recommend you to develop the list forms(EditForm.aspx, DispForm.aspx and NewForm.aspx) as sharepoint application pages. Then use the content editor web part to mimic the list functionality. Basically use html +javascript  to provide the options like new, upload options to the list.

    Hope this helps !

    Sundar Narasiman
    Saturday, April 25, 2009 11:53 AM
  • It all depends on what you are trying to accomplish. If you are for instance just trying to add extra metadata at the time an item is being edited you may be better served using a event handler on the document library and doing it there. On the other hand, if you are trying to add functionality to the indexing process like custom lookup fields etc then creating an EditForm.aspx is certainly achievable. In the past I have taken the EditForm and inherited from a custom code behind dll. I inherited from Microsoft.SharePoint.WebControls.LayoutsPageBase.  When customiizing your EditForm.aspx page it pays to have a very good understanding of what Microsoft.SharePoint.WebControls are available.  It will also pay to encapsulate your indexing logic into your own webcontrol.  Then you can hook into the OK and Cancel button events by using the _controltemplates/ToolBar.ascx user control, below is an example. You can see the code behind has access to the SharePoint context and you can use it to easily navigate back or update the item that needs to be edited. After creating the page and the assembly, deploy the assembly to the GAC and the page to the layouts directory. In order for SharePoint to find the new page it must be in the layouts directory. Finally, in order to SharePoint to automatically use your new EditForm you must assign it to a contenttype programatically. You can look at the following link for that information.  http://www.sharepointblogs.com/smc750/archive/2007/11/20/setting-a-custom-edit-form-for-a-content-type.aspx

     <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl2" RightButtonSeparator="&nbsp;" runat="server">
    			<Template_Buttons>
    				<SharePoint:CreatedModifiedInfo ID="CreatedModifiedInfo1" runat="server" ControlMode="Display"/>
    			</Template_Buttons>
    			<Template_RightButtons>
    			    <asp:Button ID="Button1"  Text="OK"  runat="server" onclick="SaveToSP_Click" CssClass="ms-ButtonHeightWidth"/> 
    		        <asp:Button ID="Button2"  Text="Cancel"  runat="server" onclick="GoBack_Click" CssClass="ms-ButtonHeightWidth"/>
    		     </Template_RightButtons>    
            </wssuc:ToolBar> 
     protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
    
                //get list item
                if (string.IsNullOrEmpty(Request.Params["List"]) || string.IsNullOrEmpty(Request.Params["ID"]))
                    throw new ArgumentException("No item to edit./");
    
                _item = SPControl.GetContextWeb(HttpContext.Current).Lists[new Guid(Request.Params["List"])].GetItemById(int.Parse(Request.Params["ID"]));
    
                if (_item == null)
                    throw new Exception("No list item found.");
    
     
            }
    
    
    
     protected void SaveToSP_Click(object sender, EventArgs e)
            {
    
                try
                {
                    _item.Update();
    
                       _item = SPControl.GetContextWeb(HttpContext.Current).Lists[new Guid(Request.Params["List"])].GetItemById(int.Parse(Request.Params["ID"]));
    
                    if (_item.File.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                    {
                        _item.File.CheckIn(string.Empty);
                    }
    
                    Response.Redirect(Request.Params["Source"]);
                   
                }
                catch (Exception ex)
                {
                    this.ErrorLabel.Text = "Unable to save your changes due to the following error: " + "<br>" + ex.Message.ToString();
    
                }
    
            }
    
       protected void GoBack_Click(object sender, EventArgs e)
            {
                Response.Redirect(Request.Params["Source"]);
               
            }

    http://www.certdev.com
    Saturday, April 25, 2009 2:17 PM
  • thanks for your response,

    What I don't understand from your comments are how I can 'use the content editor web par to mimic the list functionality'.... How could I add a new or upload button to a list using javascript.....? Can I do this:

    1) Call the upload.aspx to upload a document.

    2) Then call the EditForm.aspx page which has all its properties pre-filled with the item being copied.

    using javascript?

    thanks
    Monday, April 27, 2009 1:29 AM
  • thanks for your response,

    I have two questions:

    1) Can code be written that in the SaveToSP function, the item is saved as a new item, instead of the same item being edited.

    2) Can the Ok button of the EditList.aspx page (ListFormWebPart) be hidden, so that we can override it with this...

    thanks
    Monday, April 27, 2009 1:37 AM
  • If you take a look at DefaultTemplates.ascx it contains a list of templates used to generate different type of form functionality, if you are concerned about new fileds being added or removed and you don't want to do changes every time that happens then you need to add your own template to this file (you can override SaveButton functionality for 1 and 2 option  http://msdn.microsoft.com/en-us/library/aa544142.aspx) else the easy way out is to create your own content type which will allow you to define your own aspx form as shown below -


    <XmlDocuments>
           <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
            <FormsUrl xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
              <New>_layouts/mynewform.aspx</New>
              <Display>_layouts/mynewform.aspx</Display>
              <Edit>_layouts/myeditform.aspx</Edit>
              </FormsUrl>
          </XmlDocument>
        </XmlDocuments>

    Once you have your own form you control everything, as you can define what field you want to display or not to display the only draw back with this approach is that if fields are added or removed then you need to modify your form.


    Thanks,
    Suhaib Khan
    Monday, April 27, 2009 2:08 AM
  • With the technique I described, you are in full control with what you want to do. You can put any code you want in the SaveToSP function and use the SharePoint object model. If you don't want to display the OK button you can either remove it from the aspx page or set it's visible property in the OnLoad event of the aspx page.
    http://www.certdev.com
    Monday, April 27, 2009 3:19 AM
  • sivilian,
    why don't u do all the work from SharePoint Designer..
    just convert the form to XSLT, do whatever you want, for example add FileUpload control , then remove the OOB SaveButton and add your own button.
    Then  inherit from a custom code behind dll and add your code there.


    Ayman M. El-Hattab, Microsoft Certified SharePoint Specialist, http://ayman-elhattab.blogspot.com .. Follow me at twitter @ http://twitter.com/aymanelhattab
    Monday, April 27, 2009 9:10 PM
  • Steve,
    In the technique you descibed, how can we get reference to other controls we added.
    for example, you added a TextBox with an ID of "SteveTextBox", how can you reference it from  your SaveToSP function ?
    Ayman M. El-Hattab, Microsoft Certified SharePoint Specialist, http://ayman-elhattab.blogspot.com .. Follow me at twitter @ http://twitter.com/aymanelhattab
    Monday, April 27, 2009 9:21 PM

  • To reference other controls
    in your page declaration you point to your custom dll, for example,

    Inherits "CustomForms.ItemEdit,CustomForms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b88de33848dde56f"

    Then in the code behind delcare a protetected variable that points to the control's ID. For example:

    protected ListProperty ListProperty1 = null;

    Now you can access the control.


    http://www.certdev.com
    Tuesday, April 28, 2009 2:31 AM
  • aha
    Nice idea


    Ayman M. El-Hattab, Microsoft Certified SharePoint Specialist, http://ayman-elhattab.blogspot.com .. Follow me at twitter @ http://twitter.com/aymanelhattab
    Tuesday, April 28, 2009 3:53 AM
  • Hi, I am new in SP development, could somebody please explain to me how can I inherit editForm from my custom dll when my edit form contains a ListForm web part thus has to inherit from WebPartPage.

    Thanks in advance


    Zarko
    Tuesday, December 28, 2010 1:25 PM