Subtask programmatily

Hy,
is possible create in a element of a task list a subtask programmatically (event receiver)?

i hope i was explicit.

Thank

September 20th, 2013 12:35pm

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

Free Windows Admin Tool Kit Click here and download it now
September 23rd, 2013 9:02am

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.

September 23rd, 2013 10:05am

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!

Free Windows Admin Tool Kit Click here and download it now
January 14th, 2015 1:57pm

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:

https://social.technet.microsoft.com/Forums/en-US/bc4ab09a-11b2-4536-a1db-08414b4aec7b/how-to-retrieve-the-item-id-displayed-in-dispformaspx?forum=sharepointgeneralprevious

Best regards

January 15th, 2015 4:36am

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!

Free Windows Admin Tool Kit Click here and download it now
February 2nd, 2015 2:53am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics