• Passing default values to a SharePoint list's NewForm.aspx page


    You may come across the need when working with lists and list items in SharePoint, to allow a default value for a field to be passed via a URL parameter.

    Before we get started, please note that you will need access to Microsoft Office SharePoint Designer, and full access to your site in order to perform all of the steps outlined below. Also, please be aware that modifying the NewForm.aspx page can be troublesome. You should never attempt to remove any web parts that are on this page. Doing so will break your list in SharePoint, requiring you to delete and recreate your list. Proceed at your own risk! For this example we will only be -adding- code, not deleting.

    Take the following form as an example.

    In this case, the new item form is available to the user via a hyperlink, contained within an external system. That external system contains the "Contract Number" that must be entered into this SharePoint list when an item is created. Instead of requiring the user to type in this value every time, we can add some JavaScript to the NewForm.aspx page in this list which will look in the url for a pre-defined value.

    The first thing we will need to make this work, is to know the ID of the text field in which we'll push our dynamic value. In our case, that is the Contract Number field.  Finding this value is easy. Simply open up your web browser, and navigate to your list's NewForm.aspx page, and view the page source. You are looking for the block of html which renders this field.  fortunately SharePoint adds a comment block to the section, so using search to find it is pretty simple.  Here's the html for our textbox:

    <td valign="top" class="ms-formbody" width="400px">
    <!--       FieldName="Contract Number"
    FieldInternalName="Title"
    FieldType="SPFieldText" -->
    <span dir="none">
    <input name="ctl00$m$g_0b7c7e8f_df58_4308_96f5_71ef6c5060c4$ctl00$ctl04$ctl00$ctl00$ctl00$ctl04$ctl00$ctl00$TextField" type="text" maxlength="15" id="ctl00_m_g_0b7c7e8f_df58_4308_96f5_71ef6c5060c4_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField" title="Contract Number" class="ms-long" /><br>
    </span>
    </td>

    The value we're looking for specifically, is the id. In this case, it is:

    ctl00_m_g_0b7c7e8f_df58_4308_96f5_71ef6c5060c4_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField

    SharePoint seems to use long identifiers for their fields, likely to eliminate the chance of duplication. You will need to use the entire id value in the following code.

    Now that we have our id, we are ready to edit our NewForm.aspx page.  Open up SharePoint Designer, and navigate to your site.  within your site, you will find a "Lists" folder. Open that folder, and navigate to the folder that is the name of your list. Once there, open up the file NewForm.aspx

    We will need to add a new <script /> block to this page to contain our JavaScript. The location of this script block is critical! If you fail to place this script block at the correct location, your script will not have access to your form, and no value will be posted. On or about line 62 of your NewForm.aspx page, you should see a script block, with the id of onetidPageTitleAreaFrameScript.  we will be adding our script block directly below this, starting on line 71 in our example.

    Example of where to place our script block.

    The code we need to place follows:

    <script type="text/javascript">
    function GetUrlParameter( name )
    {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null ) return "";
    else return results[1];
    }
    document.forms['aspnetForm'].ctl00_m_g_0b7c7e8f_df58_4308_96f5_71ef6c5060c4_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField.value = GetUrlParameter('contractNumber');
    </script>

    Please remember, the page you are viewing now is likely text wrapping.  The code above should have no line break in the following statement:  document.forms['aspnetForm'].ct100_m_g_...

    Finally, you need to update the script above with the correct id of your field, and save the page. You'll see in the code above that this exists between document.forms['aspnetForm'].  and .value  you will also want to replace 'contractNumber' with the exact name and case of the parameter you are capturing. Note: the javascript function above, which utilizes regular expressions, is case sensitive. You will have to ensure that you are passing your parameter with the correct case.

    Now that we have added this script, we can link to our new form with a hyperlink such as the following (be sure to modify it to match the location of your list!)

    http://intranet.mycompany.com/sitedirectory/MySi:teLocation/Lists/MyList/Newform.aspx?contractNumber=00004000932

    Once you click in to this link, you will see your new form now has your field pre-populated with your value:

    -----------------------------------------------------------------------------------

    http://sharepointjavascript.wordpress.com/2010/05/28/get-or-set-value-for-sharepoint-field-in-newform-editform-and-dispform-get-only-in-dispform/

    http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/MS-SharePoint/Q_23772390.html

    -----------------------------------------------------------------------------------

    http://blogs.msdn.com/b/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx

    http://blogs.vbcity.com/skullcrusher/archive/2008/04/10/9024.aspx

    http://www.whitworth.org/2009/07/29/how-to-set-sharepoint-people-picker-default-value-to-current-user-through-javascript/

    http://www.sharepointhillbilly.com/archive/2009/07/06/setting-sharepoint-form-fields-using-javascript.aspx

  • 相关阅读:
    MVC5 Entity Framework学习之Entity Framework高级功能(转)
    js获取当前日期的前一天、一月、一年的日期 明天、后天(转)
    easyui datagrid 分页
    博客园技术精粹集合留贴
    常用C# DateTime 日期计算
    Jquery设置Cookie
    设计模式 -- 装饰者模式
    设计模式 -- 策略模式
    IBatis.Net中调用存储过程
    其他元素条件 IBatisNet
  • 原文地址:https://www.cnblogs.com/Areas/p/2234955.html
Copyright © 2020-2023  润新知