using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Data;
using System.Globalization;
using System.Threading.Tasks;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
namespace RecentUsedSites
{
public static class Program
{
public static DataTable table = new DataTable();
static void Main(string[] args)
{
List<string> Sites;
table.Columns.Add("Site Title");
table.Columns.Add("Site Url");
table.Columns.Add("List Title");
table.Columns.Add("List Item Title");
table.Columns.Add("List Item Modified");
using (var reader = new StreamReader(@"D:\Kunal\Global Investors\Automation\RecentUsedSites\RecentUsedSites\Data\Sites.csv"))
{
Sites = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
Console.WriteLine(values[0]);
Sites.Add(values[0]);
}
Sites.RemoveAt(0);
}
foreach (var _siteUrl in Sites)
{
string _userName = @"<Username>";
string _passWord = "<Password>";
GetAllSubWebs(_siteUrl, _userName, _passWord);
}
ToCSV(table, @"D:\Kunal\Global Investors\Automation\RecentUsedSites\RecentUsedSites\Reports\Results.csv");
//Console.WriteLine("Enter your password");
//_passWord = Console.ReadLine();
Console.Read();
}
public static int GetMonthDifference(DateTime startDate, DateTime endDate)
{
int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
}
public static void GetAllSubWebs(string path, string username, string password)
{
try
{
using (ClientContext context = new ClientContext(path))
{
context.Credentials = new NetworkCredential(username, password);
Web _web = context.Web;
context.Load(_web, website => website.Webs, website => website.Title, website => website.Url);
context.ExecuteQuery();
Console.WriteLine("Site is " + _web.Title);
Console.WriteLine("---------------------------------------------------------------------------------");
ListCollection _lists = _web.Lists;
context.Load(_lists);
context.ExecuteQuery();
foreach (List list in _lists)
{
CamlQuery query = new CamlQuery();
context.Load(list);
context.ExecuteQuery();
Console.WriteLine("List is " + list.Title + " with Item Count is " + list.ItemCount);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><OrderBy><FieldRef Ascending='False' Name='ID'/></OrderBy></Query><RowLimit>1</RowLimit></View>";
ListItemCollection lastItems = list.GetItems(camlQuery);
context.Load(lastItems, it => it.Include(item => item["ID"], item => item.Id));
context.ExecuteQuery();
int maxId = 0;
foreach (ListItem Item in lastItems)
{
//Console.WriteLine("Max ID:"+Item.Id);
maxId = Item.Id;
}
int lastId = 0;
while (lastId <= maxId)
{
query.ViewXml = "<View Scope='Recursive'><Query><Where><Gt><FieldRef Name='ID'/>" + "<Value Type='Number'>" +
lastId + "</Value></Gt></Where></Query><RowLimit>1000</RowLimit></View>";
ListItemCollection items = list.GetItems(query);
context.Load(items);
context.ExecuteQuery();
if (items.Count == 0)
{
break;
}
foreach (ListItem item in items)
{
var parameterDate = (DateTime)item["Modified"];
DateTime today = DateTime.Now;
int monthDiff = GetMonthDifference(today, parameterDate);
if (monthDiff < 6)
{
Console.WriteLine("ITEM TITLE " + item["Title"] + " ITEM MODIFIED " + item["Modified"]);
var itemName = (string)item["Title"];
var Url = (string)_web.Url;
//var modifiedDate = (string)item["Modified"];
//siteTitle.Add(_web.Title);
//siteUrl.Add(_web.Url);
//listTitle.Add(list.Title);
//listItemTitle.Add(itemName);
//listItemModified.Add(modifiedDate);
table.Rows.Add(_web.Title, _web.Url, list.Title, itemName, parameterDate);
continue;
}
}
if (items.Count > 0)
{
lastId = items.Last().Id;
}
}
}
Console.WriteLine("#####################################################################");
foreach (Web subweb in _web.Webs)
{
string newPath = subweb.Url;
GetAllSubWebs(newPath, username, password);
}
}//End of Using
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void ToCSV(this DataTable dtDataTable, string strFilePath)
{
StreamWriter sw = new StreamWriter(strFilePath, false);
//headers
for (int i = 0; i < dtDataTable.Columns.Count; i++)
{
sw.Write(dtDataTable.Columns[i]);
if (i < dtDataTable.Columns.Count - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dtDataTable.Rows)
{
for (int i = 0; i < dtDataTable.Columns.Count; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
string value = dr[i].ToString();
if (value.Contains(','))
{
value = String.Format("\"{0}\"", value);
sw.Write(value);
}
else
{
sw.Write(dr[i].ToString());
}
}
if (i < dtDataTable.Columns.Count - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Data;
using System.Globalization;
using System.Threading.Tasks;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
namespace RecentUsedSites
{
public static class Program
{
public static DataTable table = new DataTable();
static void Main(string[] args)
{
List<string> Sites;
table.Columns.Add("Site Title");
table.Columns.Add("Site Url");
table.Columns.Add("List Title");
table.Columns.Add("List Item Title");
table.Columns.Add("List Item Modified");
using (var reader = new StreamReader(@"D:\Kunal\Global Investors\Automation\RecentUsedSites\RecentUsedSites\Data\Sites.csv"))
{
Sites = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
Console.WriteLine(values[0]);
Sites.Add(values[0]);
}
Sites.RemoveAt(0);
}
foreach (var _siteUrl in Sites)
{
string _userName = @"<Username>";
string _passWord = "<Password>";
GetAllSubWebs(_siteUrl, _userName, _passWord);
}
ToCSV(table, @"D:\Kunal\Global Investors\Automation\RecentUsedSites\RecentUsedSites\Reports\Results.csv");
//Console.WriteLine("Enter your password");
//_passWord = Console.ReadLine();
Console.Read();
}
public static int GetMonthDifference(DateTime startDate, DateTime endDate)
{
int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
}
public static void GetAllSubWebs(string path, string username, string password)
{
try
{
using (ClientContext context = new ClientContext(path))
{
context.Credentials = new NetworkCredential(username, password);
Web _web = context.Web;
context.Load(_web, website => website.Webs, website => website.Title, website => website.Url);
context.ExecuteQuery();
Console.WriteLine("Site is " + _web.Title);
Console.WriteLine("---------------------------------------------------------------------------------");
ListCollection _lists = _web.Lists;
context.Load(_lists);
context.ExecuteQuery();
foreach (List list in _lists)
{
CamlQuery query = new CamlQuery();
context.Load(list);
context.ExecuteQuery();
Console.WriteLine("List is " + list.Title + " with Item Count is " + list.ItemCount);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><OrderBy><FieldRef Ascending='False' Name='ID'/></OrderBy></Query><RowLimit>1</RowLimit></View>";
ListItemCollection lastItems = list.GetItems(camlQuery);
context.Load(lastItems, it => it.Include(item => item["ID"], item => item.Id));
context.ExecuteQuery();
int maxId = 0;
foreach (ListItem Item in lastItems)
{
//Console.WriteLine("Max ID:"+Item.Id);
maxId = Item.Id;
}
int lastId = 0;
while (lastId <= maxId)
{
query.ViewXml = "<View Scope='Recursive'><Query><Where><Gt><FieldRef Name='ID'/>" + "<Value Type='Number'>" +
lastId + "</Value></Gt></Where></Query><RowLimit>1000</RowLimit></View>";
ListItemCollection items = list.GetItems(query);
context.Load(items);
context.ExecuteQuery();
if (items.Count == 0)
{
break;
}
foreach (ListItem item in items)
{
var parameterDate = (DateTime)item["Modified"];
DateTime today = DateTime.Now;
int monthDiff = GetMonthDifference(today, parameterDate);
if (monthDiff < 6)
{
Console.WriteLine("ITEM TITLE " + item["Title"] + " ITEM MODIFIED " + item["Modified"]);
var itemName = (string)item["Title"];
var Url = (string)_web.Url;
//var modifiedDate = (string)item["Modified"];
//siteTitle.Add(_web.Title);
//siteUrl.Add(_web.Url);
//listTitle.Add(list.Title);
//listItemTitle.Add(itemName);
//listItemModified.Add(modifiedDate);
table.Rows.Add(_web.Title, _web.Url, list.Title, itemName, parameterDate);
continue;
}
}
if (items.Count > 0)
{
lastId = items.Last().Id;
}
}
}
Console.WriteLine("#####################################################################");
foreach (Web subweb in _web.Webs)
{
string newPath = subweb.Url;
GetAllSubWebs(newPath, username, password);
}
}//End of Using
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void ToCSV(this DataTable dtDataTable, string strFilePath)
{
StreamWriter sw = new StreamWriter(strFilePath, false);
//headers
for (int i = 0; i < dtDataTable.Columns.Count; i++)
{
sw.Write(dtDataTable.Columns[i]);
if (i < dtDataTable.Columns.Count - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dtDataTable.Rows)
{
for (int i = 0; i < dtDataTable.Columns.Count; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
string value = dr[i].ToString();
if (value.Contains(','))
{
value = String.Format("\"{0}\"", value);
sw.Write(value);
}
else
{
sw.Write(dr[i].ToString());
}
}
if (i < dtDataTable.Columns.Count - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
}
}
No comments:
Post a Comment