Locked Add JS to NewForm.aspx of a list

  • Sunday, September 09, 2012 10:42 AM
     
      Has Code

    Hello, how to place JS-code to NewForm.aspx of a list in SharePoint 2013 APP.
    In sharepoint 2010 we can use this list definition to add JS:

    <List ...>
      <MetaData>
        ...
        <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" UseDefaultListFormWebPart="False">
            <WebParts>
              <AllUsersWebPart WebPartZoneID="Main" WebPartOrder="1">
                <![CDATA[ 
                <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2"
                    xmlns:sfwp="http://schemas.microsoft.com/WebPart/v2/SimpleForm">
                  <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>                         
                  <TypeName>Microsoft.SharePoint.WebPartPages.SimpleFormWebPart</TypeName>
                  <FrameType>None</FrameType>
                  <Title>JS1</Title>
                  <sfwp:Content>ANY HTML YOU NEED HERE
                  </sfwp:Content>
                </WebPart>
                ]]>
              </AllUsersWebPart>
              <AllUsersWebPart WebPartZoneID="Main" WebPartOrder="2">
                <![CDATA[
                <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
                  <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>                         
                  <TypeName>Microsoft.SharePoint.WebPartPages.ListFormWebPart</TypeName>
                  <PageType>PAGE_NEWFORM</PageType>
                </WebPart>
                ]]>
              </AllUsersWebPart>
            </WebParts>
          </Form>
        </Forms>
      </MetaData>
    </List>

All Replies

  • Thursday, September 13, 2012 9:48 AM
     
     

    Not sure if this is what you're looking for, but if you already have created the list, you can navigate to newform.aspx (by clicking on "New Item") and append "&toolpaneview=2" to the URL.

    You are then able to add a Content Editor Web Part, which in turn you can use to add arbitrary HTML and Javascript to your page.

    Hth, Matthias

    • Proposed As Answer by froodien1 Friday, September 14, 2012 10:27 PM
    • Unproposed As Answer by AlexBBB Tuesday, September 18, 2012 11:51 AM
    •  
  • Tuesday, September 18, 2012 11:56 AM
     
     
    mattgl, thanks. We need to add JS automatically.

    • Edited by AlexBBB Tuesday, September 18, 2012 11:56 AM
    •  
  • Wednesday, September 19, 2012 4:41 PM
    Moderator
     
     

    For the XsltListViewWebPart and ListFormWebPart, we've introduced a new property called JSLink which allows you to reference whatever JS file you'd like to be rendered on the page with the web part.  The property is also available on SPViews and content types.


    Elisabeth Olson SharePoint PM - MSFT

  • Thursday, September 20, 2012 11:49 AM
     
     
    Elisabeth Olson, please show example how to use JSLink property in list definition to add JavaScript to NewForm.aspx.
  • Thursday, September 20, 2012 4:58 PM
    Moderator
     
     

    Basically, you would just add a <JSLink> node to your ListFormWebPart or XsltListViewWebPart definitions, so starting with your existing code it would look something like this:

                <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">

                  <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>                        

                  <TypeName>Microsoft.SharePoint.WebPartPages.ListFormWebPart</TypeName>

                  <PageType>PAGE_NEWFORM</PageType>

                   <JSLink>{path to JS file}</JSLink>

                </WebPart>


    Elisabeth Olson SharePoint PM - MSFT

  • Friday, September 21, 2012 7:28 AM
     
     
    Don't work. Looks like Sharepoint 2013 ignores <WebParts> teg in listDefinition.