I want to add item to list where Title is equal to current user ID by JavaScript Object Model.
As far as I know I first need to get currentUserID and I wrote the following code
currentUser = web.get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(
Function.createDelegate(this, onQuerySucceeded),
Function.createDelegate(this, onQueryFailed)
);
function onQuerySucceeded(){
alert (currentUser.get_id());
}
function onQueryFailed(){
alert("Fail!" + args.get_message() + "\n" + args.get_stackTrace());
}
With following piece of code I can add item to the list
list = oWebsite.get_lists().getByTitle("MyList");
itemCreateInfo = new SP.ListItemCreationInformation();
currentUser = oWebsite.get_currentUser();
istItem = list.addItem(itemCreateInfo);
listItem.set_item("Title", "Current SPUser ID"); //here I'd like to write user's ID
now = new Date();
now.format("dd/M/yy h:mm tt");
listItem.set_item("Visit", now);
listItem.update();
userData = clientContext.load(listItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, onQuerySucceeded),
Function.createDelegate(this, onQueryFailed)
);
function onQuerySucceeded() {
alert("Success");
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message());
}
What I want is to replace string "Current SPUser ID" with actual user ID (currentUser.get_id()).