Get All approval task through template ID using SharePoint Server object model

get sharepoint approval task using server object model
SharePoint deveopment training course

This SharePoint server object model tutorial explains, how to get all approved task from a different type of task list and added in one list. Here, we will use the SharePoint server object model code to get all approval tasks from a list based on the template ID in SharePoint 2013/2016.

Get All approval task through template ID using SharePoint Server object model

Here I have created a console application where I have written a code to fetch all approval task from SharePoint task list using Template ID (107) and stored in a new custom List.

Step 1: Here I have created a new console application.

get sharepoint approval task using server object model
get sharepoint approval task using server object model

Step 2: Below is the code to get all approval task and add in a new custom list in SharePoint.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using System.Data;
using Microsoft.SharePoint.Administration;

namespace Get_All_Approval_Task
{
    class Program
    {

        static void Main(string[] args)
        {
            string siteURL = "/sites/KMIntranet";
            using (SPSite stsite = new SPSite(siteURL))
            {
                SPWebApplication spWebApp = stsite.WebApplication;

                foreach (SPSite spSit in spWebApp.Sites)
                {
                    using (SPWeb siteWeb = spSit.OpenWeb())
                    {
                        SPSiteDataQuery query = new SPSiteDataQuery();
                        query.Webs = "<Webs Scope=\"SiteCollection\" />";
                        query.Lists = "<Lists ServerTemplate=\"107\" />";
                        query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Priority' /><FieldRef Name='AssignedTo' /><FieldRef Name='StartDate' /><FieldRef Name='DueDate' /><FieldRef Name='Status' /><FieldRef Name='FileDirRef' />";
                        query.Query = @"<Where>
                                            <And>
                                            <Gt>
                                            <FieldRef Name='ID' />
                                            <Value Type='Counter'>0</Value>
                                            </Gt>
                                            <And>
                                            <Eq>
                                            <FieldRef Name='Status' />
                                            <Value Type='Choice'>Completed</Value>
                                            </Eq>
                                            <IsNotNull>
                                            <FieldRef Name='AssignedTo' />
                                            </IsNotNull>
                                            </And>
                                            </And>
                                            </Where>";
                        DataTable dtTasks = new DataTable();
                        dtTasks = siteWeb.GetSiteData(query);

                        SPFieldUserValue userValue = null;
                        SPSite Mastersite = new SPSite(siteURL);
                        SPWeb MasterWeb = Mastersite.OpenWeb();
                        SPList AppprovedList = MasterWeb.Lists["AddedTask"];
                        SPUser userTest = null;
                        foreach (DataRow row in dtTasks.Rows)
                        {
                            string listName = Convert.ToString(row["FileDirRef"].ToString().Substring(row["FileDirRef"].ToString().IndexOf('#') + 1));

                            SPListItem Item = AppprovedList.AddItem();
                            Item["Title"] = row["Title"];
                            Item["StartDate"] = row["StartDate"];
                            Item["DueDate"] = row["DueDate"];
                            Item["Status"] = row["Status"];
                            Item["URL"] = siteURL + listName + "/DispForm.aspx?ID=" + Convert.ToString(row["ID"]) + "&Source=" + siteURL;
                            Item["Priority"] = row["Priority"];
                            string assignedTo = Convert.ToString(row["AssignedTo"].ToString().Substring(row["AssignedTo"].ToString().IndexOf('#') + 1));
                            try
                            {
                                userTest = MasterWeb.EnsureUser(assignedTo);
                                userValue = new SPFieldUserValue(MasterWeb, userTest.ID, userTest.LoginName);
                                Item["AssignedTo"] = userValue;
                                Item.Update();
                            }
                            catch
                            {

                            }
                        }
                    }
                }
            }
        }
    }
}

Step 3: Here all approval task has been added automatically.

get sharepoint approval task using server object model
get sharepoint approval task using server object model

Step 4: Below is the code for delete task from SharePoint using SharePoint Server Object Model.

public static void deleteTask()
        {
            string siteURL = "http://hqinternetdev16/";
            SPSite Mastersite = new SPSite(siteURL);
            SPWeb MasterWeb = Mastersite.OpenWeb();
            SPList AppprovedList = MasterWeb.Lists["RequestedTask"];
            for (int i = 0; i < AppprovedList.ItemCount; i++)
            {
                SPListItem oListItem = AppprovedList.Items[i];
                oListItem.Delete();
            }
        }

You may like following the SharePoint server object model tutorial:

Hop this SharePoint tutorial explains, how to get SharePoint approval tasks using the SharePoint server object model in SharePoint 2013/2016.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Rajkiran Swain

Rajkiran is currently working as a SharePoint Consultant in QATAR . Rajkiran having 6 + years of experience in Microsoft Technologies such as SharePoint 2019/2016/2013/2010, MOSS 2007,WSS 3.0, Migration, Asp.Net, C#.Net, Sql Server, Ajax, jQuery etc.He is C#Corner MVP (2 Times).

View all posts by Rajkiran Swain →