Hy,
is possible create in a element of a task list a subtask programmatically (event receiver)?
i hope i was explicit.
Thank
Technology Tips and News
Hy,
is possible create in a element of a task list a subtask programmatically (event receiver)?
i hope i was explicit.
Thank
Hi,
From your description, you want to create subtasks in a task list programmatically.
The Javascript code below can be used to create subtasks in a taks list by using SharePoint Client Object Model:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script type="text/javascript"> function newSubTask(){ var clientContext = new SP.ClientContext.get_current(); var oList = clientContext.get_web().get_lists().getByTitle('MyTaskList1'); var itemCreationInfo = new SP.ListItemCreationInformation(); this.oListItem = oList.addItem(itemCreationInfo); oListItem.set_item('Title', 'new subtask'); oListItem.set_item('ParentID', '1'); 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()); } </script> <input id="Button1" type="button" value="Run Code" onclick="newSubTask()" />
You can add this code into a page using SharePoint Designer 2013 or add it into a Content Editor Web Part on a page.
If you want to create subtasks using an Event Receiver, you can modify the code above with C# and SharePoint Server Object Model.
using (SPSite oSiteCollection = new SPSite("http://Site_Name")) { using (SPWeb oWebsiteRoot = oSiteCollection.OpenWeb()) { SPList oList = oWebsiteRoot.Lists["MyTaskList1"]; SPListItem oListItem = oList.Items.Add(); oListItem["Title"] = "new subtask"; oListItem["ParentID"] = 1; oListItem.Update(); } }
The link below show more on SPListItem class:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.aspx
Here are some links on creating Event Receiver for your reference:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver.aspx
http://onceinawhilescribble.blogspot.in/2013/05/creating-simple-event-receiver-in.html
Best regards
Thank you very much , work fine. i used event receiver with this code :
SPList oList = oWebsiteRoot.Lists["MyTaskList1"]; SPListItem oListItem = oList.Items.Add(); oListItem["Title"] = "new subtask"; oListItem["ParentID"] = 1; oListItem.Update();Best regards.
Hi
Thank you for the code - it works fine!
I'm using the JavaScript code and I added it to the DispForm.aspx of a Task in my tasklist. Do you know how to replace the number of the ID:
oListItem.set_item('ParentID', '1');
by a variable, so that it gets always the ID of the current element from which I opened the DispForm.aspx?
Thank you very much in advance!
Hi,
We can get current item id from in DisplayForm from the URL using JavaScript.
Here is a similar thread about how to get current item id from the URL of a DisplayForm using JavaScript for your reference:
Hey,
Thanks very much - it worket out very well.
Same issue now I have with the ParentID of a subtask..but the other way round. I wand to get from the EditForm of a subtask to the next EditForm of another subtask, but with the same ParentID. So I have to get the parentID of my subtask programmatically.
I'm just not getting it and haven't found a solution in the net yet.
Can you help me?
Thanks very much in advance!