Click here to Download Document for this Post
In this post we will learn how to SharePoint List Items Update Using Console Application Programming and Object Models (Adding number to all Columns).
1.Calculating no of Months form Employee Date of Joining.
1.Calculating no of Months form Employee Date of Joining.
2.Calculating no of Days from Employee Date of Joining.(With including Saturday and Sunday).
3.Calculating no of Days from Employee Date of Joining.( Without including Saturday and Sunday).
4.In this programs converting string to DateTime and string to Double also.
4.In this programs converting string to DateTime and string to Double also.
Steps for SharePoint List Items Update Using Console Application Programming and Object Models.
step 1.Createing List "EMP Master List" Like bellow.
Here in this scenario I have chosen Console Application template because it doens’t contains any input fields to show as webpart and also to avoid GAC registration, safe controls etc., I felt it will be easy to execute the code in Console.
Step2. Creating console Application using visual studio.
Step3. Add microsoft sharepoint reference
and Namespace using Microsoft.SharePoint
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace Casual_Leave_Updating
{
class
CasualLeaveUpdating
{
static void
Main(string[] args)
{
int icount
= 0, count = 0;
using
(SPSite site = new SPSite("http://kapples:2931/sites/PTC/TS"))
{
using (SPWeb web =
site.OpenWeb())
{
SPList list = web.Lists["EMP Master List"];
web.AllowUnsafeUpdates = true;
DateTime ToDayDate = DateTime.Now;
foreach (SPListItem item in list.Items)
{
try
{
string strActive = item["Active"].ToString();
DateTime DateOfJoining = Convert.ToDateTime(item["Date of
joining"]);
//If User is Active Satate=Yes then EMP Master List is Update
if (strActive == "Yes")
{
//Calculating
no of Month form Date of Joining (Employee Joining Date)
int iNoOfMonths
= 12 * (ToDayDate.Year - DateOfJoining.Year) + ToDayDate.Month -
DateOfJoining.Month;
Console.WriteLine("\nNo
Of Months:" + iNoOfMonths);
//Calculating
no of Days from Date of Joining (Employee Joining Date)(With including Saturday
and Sunday)
TimeSpan
tNoOfDays = ToDayDate.Subtract(DateOfJoining);
Console.WriteLine("\nNo Of Days" + tNoOfDays.TotalDays);
//Calculating
no of Days from Date of Joining (Employee Joining Date)( Without including
Saturday and Sunday)
while
(DateOfJoining <ToDayDate)
{
if
(DateOfJoining.DayOfWeek != DayOfWeek.Saturday &&
DateOfJoining.DayOfWeek != DayOfWeek.Sunday)
icount++;
DateOfJoining = DateOfJoining.AddDays(1);
}
Console.WriteLine("\nNo Of Days Without including Saturday and
Sunday:" +icount);
icount = 0;
//Updating EMP
Master List items,if Employee Joining more then 6 months List Items is Updated.
if (iNoOfMonths
>= 6)
{
//If Casual
Leave is 0 or null it's added 2
if
(item["Casual Leave"] == null)
{
item["Casual Leave"] =
"2.0";
item.Update();
}
else
{
try
{
double casualLeaves = Convert.ToDouble(item["Casual
Leave"].ToString());
casualLeaves = casualLeaves + 2.0;
string casualLeaveStr =
casualLeaves.ToString();
item["Casual Leave"] = casualLeaveStr;
item.Update();
}
catch
(Exception ex)
{
Console.Write("ex.message");
}
}
count++;
}
}
list.Update();
//Console.WriteLine(item["Casual Leave"]);
}
catch (Exception)
{
//throw;
}
}
Console.WriteLine(count);
web.Update();
web.AllowUnsafeUpdates
= false;
}
}
}
}
}
-->Before "EMP Master List" List Item Updating see above image.