Question

I have 1 calender list. In the edit form of items in the list, i have added a custom button in the ribbon which is responsible to read the data from the edit form and email it to a person.

I am not able to read data from the edit form. here is my code

var listItemInfo = '';

 function mailThisPage(olist) {
 ExecuteOrDelayUntilScriptLoaded(getListData, "sp.js");
 var mailTo = encodeURIComponent('The following item has been updated:\n Change' + listItemInfo);
 window.location = "mailto:someone@someone.com&Subject=Item Information&body=" + mailTo + "";
}

function getListData() {

var clientContext = SP.ClientContext.get_current();
var myList = clientContext.get_web().get_lists().getByTitle('CMCalendar');
var changeOrder = myList.
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<ViewFields><FieldRef Name="ChangeOrderNumber" /><FieldRef Name="COStatus" /><FieldRef Name="OrderSummary" /><FieldRef Name="OrderDescription" /><FieldRef Name="COURL" /></ViewFields>');
listItem = myList.getItems(camlQuery);
clientContext.load(listItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}


function onQuerySucceeded(sender, args) {
var listItemEnumerator = listItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nTitle: ' + oListItem.get_item('OrderSummary') + '\nLocation: ' + oListItem.get_item('COURL');
   }
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Était-ce utile?

La solution

Since you are in EditForm, the Url of the page will already contain the ID of the list item. Read this ID using JavaScript url mnipulation. Then use it to get item detials. You need to use SP.List.getItemById(id). Example code:

<script type="text/ecmascript" language="ecmascript">

   //Get the list item from the Announcements list whose Id is 4. Note that this is the ID of the item in the list, not a reference to its position in the collection.
   var itemId = 4;   
   var targetListItem;

   function runCode() {
     var clientContext = new SP.ClientContext(); 
     var targetList = clientContext.get_web().get_lists().getByTitle('Announcements');
     targetListItem = targetList.getItemById(itemId);
     clientContext.load(targetListItem, 'Title');
     clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
   }

   function onQuerySucceeded() {

       alert('Request succeeded. \n\nRetrieved Item is: ' + targetListItem.get_item('Title'));

   }

   function onQueryFailed(sender, args) {
     alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
   }

</script>
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top