0

i have created a solution in SharePoint from visual studio C#. I want to create a Job Timer so i have created a c# class too. but now I'm adding a new row to task list but what i want really to do is to update a specific row not to add new one. the code belongs as follow:

public override void Execute(Guid targetInstanceId)
{
SPWebApplication webApp = this.Parent as SPWebApplication;
SPList taskList = webApp.Sites[0].RootWeb.Lists["Tasks"];
SPListItem newTask = taskList.Items.Add();
newTask["Title"] = "New Task" + DateTime.Now.ToString();
newTask.Update();
}

I repeat again instead of add new row i want to update. Could anyone help me please.

  • Well, taskList.Items is a collection, so you will have to somehow identify the item you want to update. Exactly how do you need to do that? Which item are you targeting? – Hitodama Jul 8 '15 at 8:16
  • @SPArchaeologist on my project I have a column named "Ditet" and that column i want to update for all rows. I don't know how to achieve this any suggestion – Nderon Hyseni Jul 8 '15 at 8:20
0

If you want to update the same column for ALL items in the list, then the following code should work

public override void Execute(Guid targetInstanceId)
{
    SPWebApplication webApp = this.Parent as SPWebApplication;
    SPList taskList = webApp.Sites[0].RootWeb.Lists["Tasks"];
    SPListItemCollection taskListItems = taskList.Items;
    int increment = 2;

    foreach (SPListItem taskItem in taskListItems)
    {
        // Insert you logic here to get the value you want to insert in the "Ditet" column
        // If you want to increase the previous value you can do the following
        int oldDitetValue = Convert.ToInt32(taskItem["Ditet"].ToString());
        taskItem["Ditet"] = oldDitetValue + increment;
        taskItem.Update();
    }
}
  • than you so much this worked perfectly. thank you so much. But now i want taskItem["Ditet"] =2; Ditet to be equal 2 and for everytime the value of ["Ditet"] to be 4, 6, 8, 10..etc to sum the value of 2 + 2 do you understand me? – Nderon Hyseni Jul 8 '15 at 9:34
  • @NderonHyseni Please check the updates I added to the code. You save the old value from the column and then you just add 2 to it. – user2536 Jul 8 '15 at 9:44
  • Ziga yes sir but look, i have made a JobTimer that executes every 15 minutes. I set the value for ["Ditet"] = 2; after 15 minutes I want that value to be 4 again after 15 min to be 6 and after again after minutes to be 8 and so on. Do you understand now me? – Nderon Hyseni Jul 8 '15 at 9:47
  • taskList["Ditet"] = 2 + convert.Toint32(taskList["Ditet"]) + 2 this is what i wanted to do but anyway thank you soooo much i dont know what to do if you did not tell me! thank you so much sun i love you haha :P keep going brother keep posting you answers – Nderon Hyseni Jul 8 '15 at 10: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.