Thursday 20 June 2013

Assign sharepoint list field values using javascript(Ecmascript)

Refer the below link:http://spjsblog.com/2010/05/28/get-or-set-value-for-sharepoint-field-in-newform-editform-and-dispform-get-only-in-dispform/

http://www.c-sharpcorner.com/UploadFile/anavijai/get-and-set-the-list-item-value-in-sharepoint-2010-using-ecm/


Add the script in List newform.aspx or editform.aspx(after editing the item) or dispform.aspx Content editor webpart.(sometimes functionality doesnt work that time  add the content editor webpart below the form








Maintain both Content editor webpart and form into single div)


<script src="/sites/newsite/SiteAssets/jquery-1.9.0.min.js" type="text/javascript"></script><script src="/sites/newsite/SiteAssets/spjs-utility.js" type="text/javascript"></script><script type="text/javascript">
function PreSaveAction()
{
fields = init_fields_v2();

// Get value from a multicheck field(multi selection check box value)
var myRichTextValue = getFieldValue('Statuscheck');

// set the value to assignfield(single line text field)
setFieldValue('assignedfield',myRichTextValue);
return true;
}</script>



Presaveaction(will save the form actions at the time of clicking the save button)

Statuscheck:checkbox with multiple selection
assignedfield':single line text field

Download the spjs-utility.js :http://spjsfiles.com/index.php?dir=SharePoint+JavaScripts%2Fspjs-utility%2F

code to itterating through each and choice in checkbox using jquery

Follow the same steps like above

<script src="/sites/newsite/SiteAssets/jquery-1.9.0.min.js" type="text/javascript"></script><script src="/sites/newsite/SiteAssets/spjs-utility.js" type="text/javascript"></script><script type="text/javascript">

function PreSaveAction()
{
fields = init_fields_v2();

// Get value from a multicheck field(multi selection check box value)
var myRichTextValue = getFieldValue('Statuscheck');
if (myRichTextValue == "A") {
             setFieldValue('assignedfield',"A");
         }
         else if (myRichTextValue == "B") {
           setFieldValue('assignedfield',"B");
         }
         else if (myRichTextValue == "C") {
            setFieldValue('assignedfield',"C");
         }
// set the value to assignfield(single line text field)
setFieldValue('assignedfield',myRichTextValue);
alert(myRichTextValue);
return true;
}</script>




Assign sharepoint list field values using Ecmascript
Refer the below link:http://www.c-sharpcorner.com/UploadFile/anavijai/get-and-set-the-list-item-value-in-sharepoint-2010-using-ecm/
Add this code in list content editor webpart(Allitem.aspx page) or content place holder main in sharepoint designer allitems.aspx page

<script language="ecmascript" type="text/ecmascript">
       var listItem;
        var list;
        var clientContext;
        function getSetListItem() {
        var uid=document.getElementById('textbox1').value;
            this.clientContext = SP.ClientContext.get_current();
            if (this.clientContext != undefined && clientContext != null) {
                var webSite = clientContext.get_web();
                this.list = webSite.get_lists().getByTitle("Assignvalue");
                this.listItem = list.getItemById(uid);
                clientContext.load(this.listItem);
                this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess),
Function.createDelegate(this, this.OnLoadFailed));
            }
        }
        function OnLoadSuccess(sender, args) {
            var value = this.listItem.get_item("Multicheck");
             alert("value");
            this.listItem.set_item("assignfield", value);
            this.listItem.update();
            this.clientContext.load(this.listItem);
            this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess1),
Function.createDelegate(this, this.OnLoadFailed));
        }
        function OnLoadSuccess1(sender, args) {
            alert(this.listItem.get_item("assignfield"));
        }
        function OnLoadFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }</script><input id="textbox1" type="text"/>&#160; <input id="btnGetSetListItem" onclick="getSetListItem()" type="button" value="Get &amp; Set List Item"/>
















No comments:

Post a Comment