Create items on List via GET
Вопрос
I am using SharePoint 2010, and I want to create Items in my List based on the url parameters(GET):
http://mysite/mylist/AllItems.aspx?code=123&user=xpto
In this example, my new item would be:
Code: 123
User: xpto
Is it possible to do this?
Can I have some code example please so I can try?
Решение
Doable. what you need to create is a javascript code that does the following:
- Parse the URL and save code (into a variable) and user (into another variable) This link my help you get that https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
- Pass the 2 variables with the site URL and list name to the following function:
function createListItem(siteUrl,listName,code,user) {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle(listName);
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('code', code);
oListItem.set_item('user', user);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
alert('Item created: ' + oListItem.get_id());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
Не связан с sharepoint.stackexchange