1

I feel I have a fairly simple problem but every solution online is seriously complicated. I am in SharePoint designer 2010 and I am not a programmer but I can get by. I have a SP list with Contract Numbers and when you click the contract number it brings you to the item. I just want JavaScript code that will read the value from the list (by ID or however) and store it in a variable.

For example:

var siteUrl = 'https://...';
var itemID = 22;
var TargetListItem;
Function onLoad(){
var context = new SP.ClientContent(siteUrl);
var web = context.get_web().get_lists().getByTitle('Data Call');
var list = web.getItemById(itemID);
context.load(list, 'Contract Number');

var value = list.get_item('Contract Number');

var url = "/sites/... " + value + "...";
return url
}

The code works if I hard-code value so that the URL returned has a parameter, but not when I set value above. Please if anyone has a really simple way to accomplish this let me know!

1
  • Maybe the answer is complicated.... Try to go back to those solutions and see if they will work for you. If they do, get to reading and see if you can figure them out. If anything is really throwing you for a loop, ask about it here if and only if you can't find the answer anywhere else and it fits the site's question standards. Right now, you're simply asking for code, which doesn't usually work well out well. Try posting what you've tried and why it didn't work as part of your question, and the answers you get will be better (and more likely to happen) Commented May 30, 2014 at 19:47

1 Answer 1

2

You are missing SP.ClientContext.executeQueryAsync method that executes the current pending request asynchronously on the server

SP.ClientContext.executeQueryAsync method has the following signature:

SP.ClientContext.executeQueryAsync(succeededCallback, failedCallback)

and since it is async method you cant return from your method, but have to declare succeededCallback function that contains the returned results.

When working with asynchronous API such as JSOM the following patterns are commonly used:

  • Using nested callbacks
  • Using the promises pattern

Please refer Asynchronous programming in apps for Office article to get acquainted with asynchronous programming for SharePoint.

The below example demonstrates how to get list item using callback approach:

function getItem(listTitle,itemId,success,error){  
   var context = SP.ClientContext.get_current();
   var web = context.get_web();
   var list = web.get_lists().getByTitle(listTitle);
   var listItem = list.getItemById(itemId);
   context.load(listItem);

   context.executeQueryAsync(
   function() {
        success(listItem);
   },
   error
  );
}

Example

getItem('Data Call', 22,
  function(item){
       var result = item.get_item('Contract Number'); 
  },
  function(sender,args){
       console.log(args.get_message());
  }
);

References

How to: Create, Update, and Delete List Items Using JavaScript

Asynchronous programming in apps for Office

Sign up to request clarification or add additional context in comments.

1 Comment

@Vadim : Do you have any idea for call window.open(result) inside the success function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.