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.
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.
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:
- Create a custom Team web part using the SharePoint server object model and bootstrap
- Create a timer job in SharePoint 2016/2013 Programmatically (Step by Step tutorial)
- Get sharepoint site template PowerShell + (SharePoint Object Model)
- How to get logged in user count in SharePoint
- How to add attachment to SharePoint list item programmatically
Hop this SharePoint tutorial explains, how to get SharePoint approval tasks using the SharePoint server object model in SharePoint 2013/2016.
Rajkiran is currently working as a SharePoint Consultant in India . Rajkiran having 7+ 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).