Get a dataset from Sharepoint containing all changes.
Budget: $15 – $25 USD
I need to have methods like those below , I need to have :
* Methods that gives me a resulttset containg name, folder, refFolder, Time, UserName of a file/folder in Sharepoint that havve changed. It need to work for files, folders and lists in SharePoint online.
Below are the code that need to be rewritten or modfied so all changes are catched.
I like to have a meethod that takes a root folder in Sharepoint and monitor it but also ALL subfolders in the site.
IF YOU DONT HAVE EXPERIENCE DONT WASTE MY TIME :)
using Microsoft.SharePoint.Client;
using MoveFilesToSharePointOnline.Support_Classes;
using System;
using static System.Net.WebRequestMethods;
public static class SharePointChangedItems
{
public static void GetRecentlyChangedItems(Uri baseSiteFromRefNumber, string _tenantName)
{
// Create a ClientContext using the SharePoint site URL
using (ClientContext context = AuthenticationSupport.GetClientContext(baseSiteFromRefNumber.ToString(), _tenantName).Result)
{
// Calculate the start time for the query (5 minutes ago)
DateTime startTime = DateTime.Now.AddMinutes(-5);
// Create a ChangeQuery to filter changes based on time and change types
ChangeQuery changeQuery = new ChangeQuery(); // IncludeAdd and IncludeUpdate
changeQuery.DeleteObject = true;
changeQuery.Add = true;
changeQuery.Update = true;
changeQuery.Folder = true; // Include folder changes
changeQuery.RecursiveAll = true;
changeQuery.Activity = true;
changeQuery.Move = true;
changeQuery.Rename = true;
// Get the change collection for the site
ChangeCollection siteChanges = context.Site.GetChanges(changeQuery);
// Execute the query
context.Load(siteChanges);
context.ExecuteQuery();
// Process changes
ProcessChanges(context, siteChanges, startTime);
}
}
// Helper method to process changes
private static void ProcessChanges(ClientContext context, ChangeCollection changes, DateTime startTime)
{
foreach (Change change in changes)
{
// Check if the change occurred within the last 5 minutes
if (change.Time > startTime)
{
Console.WriteLine($"Change Type: {change.ChangeType}");
Console.WriteLine($"Change Time: {change.Time}");
if (change is ChangeItem itemChange)
{
// Check if the item exists before trying to load it
try
{
// Load the item to get its details
ListItem item = context.Web.Lists.GetById(itemChange.ListId).GetItemById(itemChange.ItemId);
context.Load(item, i => i["FileDirRef"], i => i.DisplayName, i => i["Editor"]);
context.ExecuteQuery();
string libraryName = item["FileDirRef"].ToString();
string objectName = item.DisplayName;
string userName = item["Editor"].ToString();
Console.WriteLine($"Library Name: {libraryName}");
Console.WriteLine($"Object Name: {objectName}");
Console.WriteLine($"Username: {userName}");
}
catch (ServerException ex)
{
Console.WriteLine($"Error retrieving item: {ex.Message}");
}
}
else if (change is ChangeFolder folderChange)
{
// Folder change
Console.WriteLine($"Folder Path: {folderChange}");
}
Console.WriteLine();
}
}
}
// Helper method to convert a string to a SecureString
private static System.Security.SecureString SecureString(string input)
{
System.Security.SecureString secureString = new System.Security.SecureString();
foreach (char c in input)
{
secureString.AppendChar(c);
}
return secureString;
}
}
* Methods that gives me a resulttset containg name, folder, refFolder, Time, UserName of a file/folder in Sharepoint that havve changed. It need to work for files, folders and lists in SharePoint online.
Below are the code that need to be rewritten or modfied so all changes are catched.
I like to have a meethod that takes a root folder in Sharepoint and monitor it but also ALL subfolders in the site.
IF YOU DONT HAVE EXPERIENCE DONT WASTE MY TIME :)
using Microsoft.SharePoint.Client;
using MoveFilesToSharePointOnline.Support_Classes;
using System;
using static System.Net.WebRequestMethods;
public static class SharePointChangedItems
{
public static void GetRecentlyChangedItems(Uri baseSiteFromRefNumber, string _tenantName)
{
// Create a ClientContext using the SharePoint site URL
using (ClientContext context = AuthenticationSupport.GetClientContext(baseSiteFromRefNumber.ToString(), _tenantName).Result)
{
// Calculate the start time for the query (5 minutes ago)
DateTime startTime = DateTime.Now.AddMinutes(-5);
// Create a ChangeQuery to filter changes based on time and change types
ChangeQuery changeQuery = new ChangeQuery(); // IncludeAdd and IncludeUpdate
changeQuery.DeleteObject = true;
changeQuery.Add = true;
changeQuery.Update = true;
changeQuery.Folder = true; // Include folder changes
changeQuery.RecursiveAll = true;
changeQuery.Activity = true;
changeQuery.Move = true;
changeQuery.Rename = true;
// Get the change collection for the site
ChangeCollection siteChanges = context.Site.GetChanges(changeQuery);
// Execute the query
context.Load(siteChanges);
context.ExecuteQuery();
// Process changes
ProcessChanges(context, siteChanges, startTime);
}
}
// Helper method to process changes
private static void ProcessChanges(ClientContext context, ChangeCollection changes, DateTime startTime)
{
foreach (Change change in changes)
{
// Check if the change occurred within the last 5 minutes
if (change.Time > startTime)
{
Console.WriteLine($"Change Type: {change.ChangeType}");
Console.WriteLine($"Change Time: {change.Time}");
if (change is ChangeItem itemChange)
{
// Check if the item exists before trying to load it
try
{
// Load the item to get its details
ListItem item = context.Web.Lists.GetById(itemChange.ListId).GetItemById(itemChange.ItemId);
context.Load(item, i => i["FileDirRef"], i => i.DisplayName, i => i["Editor"]);
context.ExecuteQuery();
string libraryName = item["FileDirRef"].ToString();
string objectName = item.DisplayName;
string userName = item["Editor"].ToString();
Console.WriteLine($"Library Name: {libraryName}");
Console.WriteLine($"Object Name: {objectName}");
Console.WriteLine($"Username: {userName}");
}
catch (ServerException ex)
{
Console.WriteLine($"Error retrieving item: {ex.Message}");
}
}
else if (change is ChangeFolder folderChange)
{
// Folder change
Console.WriteLine($"Folder Path: {folderChange}");
}
Console.WriteLine();
}
}
}
// Helper method to convert a string to a SecureString
private static System.Security.SecureString SecureString(string input)
{
System.Security.SecureString secureString = new System.Security.SecureString();
foreach (char c in input)
{
secureString.AppendChar(c);
}
return secureString;
}
}