0

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?

2

Doable. what you need to create is a javascript code that does the following:

  1. 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
  2. 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());

}

5
  • does SP.ClientContext depends of some plugin?
    – Lugarini
    May 4 '16 at 19:43
  • 1
    you may need to Add ExecuteOrDelayUntilScriptLoaded method before calling the function. ExecuteOrDelayUntilScriptLoaded(createListItem, "sp.js"); or SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem); May 4 '16 at 19:47
  • what is this 'sp.js' ?
    – Lugarini
    May 4 '16 at 20:06
  • SP.js is the JavaScript file used for the ECMA Client Object Model. This file is necessary to work with SharePoint Client Object Model. May 4 '16 at 20:28
  • So the value is always 'sp.js' ?
    – Lugarini
    May 13 '16 at 11:23

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.