0

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());
}
  • The Edit form will have information about one list item and the code you are using is simply reading all list items. Moreover, which version of SharePoint are you using? – Nadeem Yousuf Mar 3 '15 at 14:12
  • SP 2010. How do i reference that open item; a little new to the sharepoint actually. Sorry if the question is too naive – Deepika Mar 3 '15 at 14:21
1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.