How to update/edit SharePoint classic pages content/aspx using CSOM - c#

We did successfully migrate the content from MediaWiki into the SharePoint classic page. However, some of the content that came from the MediaWiki has extra information that we don't need
Is there a way through CSOM to update all the ASPX pages we generated when migrated content from MediaWiki to SharePoint?
All we need is to remove some footers from all the pages that we generated.
Thank you in advance for everyone's help

Here is the code that worked for me
static void Main(string[] args)
{
try
{
ClientContext ctx = GetClientContext("https://tenant.sharepoint.com/sites/sitename",
"usernaname#tenant.onmicrosoft.com", "password");
var listTitle = "Site Pages";
var list = ctx.Web.Lists.GetByTitle(listTitle);
var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items, icol => icol.Include(i => i["WikiField"], i => i["CanvasContent1"], i => i["FileRef"], i => i.ContentType));
ctx.ExecuteQuery();
foreach (var item in items)
{
switch (item.ContentType.Name)
{
case "Site Page":
if (item["CanvasContent1"] != null)
{
if (item["CanvasContent1"].ToString().Contains("certain pattern"))
{
ProcessSiteHTML(item["CanvasContent1"].ToString(), item["FileRef"].ToString(), item, ctx, "CanvasContent1");
}
}
break;
case "Wiki Page":
if (item["WikiField"] != null)
{
if (item["WikiField"].ToString().Contains("certain pattern"))
{
ProcessSiteHTML(item["WikiField"].ToString(), item["FileRef"].ToString(), item, ctx, "WikiField");
}
}
break;
}
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Main " + e.Message);
WriteLog(fileName, "Main " + e.Message);
}
finally
{
Console.ForegroundColor = ConsoleColor.White;
}
}
private static void ProcessSiteHTML(string page, string pageName, ListItem item, ClientContext ctx, string pageType)
{
string pattern = "Regular expression pattern";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.Multiline);
System.Text.RegularExpressions.MatchCollection matched = rg.Matches(page.ToLower());
if (matched.Count > 0)
{
System.Text.RegularExpressions.Match m =
System.Text.RegularExpressions.Regex.Match(page, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string updatedField = System.Text.RegularExpressions.Regex.Replace(page,
m.Value, string.Empty, System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
item[pageType] = updatedField;
item.Update();
ctx.ExecuteQuery();
Console.WriteLine(pageName + " has been updated");
}
}

Related

How to Display the details of a single person from a text file onto the console in C# from a PhoneBook for example?

static void AddContact()
{
string path = #"C:\Users\...\Desktop\Projekte\C# Übungen\ContactList/contacts.txt";
string name, address;
int phoneNumber;
bool addingContact = true;
string response;
List<string> ContactList = File.ReadAllLines(path).ToList();
while(addingContact)
{
System.Console.Write("Name: ");
name = Console.ReadLine();
System.Console.Write("Nummer: ");
phoneNumber = int.Parse(Console.ReadLine());
System.Console.Write("Adresse: ");
address = Console.ReadLine();
ContactList.Add($"Name: {name}");
ContactList.Add($"Nummer: {phoneNumber}");
ContactList.Add($"Adresse: {address} \n---------------------------------");
foreach (var contact in ContactList)
{
File.WriteAllLines(path, ContactList);
}
Console.Clear();
System.Console.WriteLine("Contact added successfully! Would you like to Add another Contact? (y)(n)");
response = Console.ReadLine().ToLower();
if(response == "y")
{
Console.Clear();
}
else
{
addingContact = false;
}
}
Console.WriteLine("Good bye!");
}
static void ShowContact()
{
string path = #"C:\Users\...\Desktop\Projekte\C# Übungen\ContactList/contacts.txt";
string response;
System.Console.WriteLine("Would you like to see all contacts (0) or a specific one (1)?");
response = Console.ReadLine();
switch (response)
{
case "0":
File.ReadAllLines(path);
break;
case "1":
System.Console.WriteLine("Type the name of the user: ");
string username = Console.ReadLine();
File.ReadAllLines(path)
break;
}
}
Thats everything. In the switch Statement at case 1 is where the problem is. I want it to look something like this if the user types John for example:
Name: John
Number: 233
Address: lolol 23
I already tried out something but i stopped in the middle of writing it because i realized that i dont actually know how to code that so i made my way over to stackoverflow to ask for help, thanks. Please be polite and i know that this code isnt the best, im a beginner.
Edit: I just found out that ShowContacts doesnt work at all
Edit 2: Fixed it with a foreach loop and List
List<string> contacts = File.ReadAllLines(path).ToList();
System.Console.WriteLine("Would you like to see all contacts (0) or a specific one (1)?");
response = Console.ReadLine();
switch (response)
{
case "0":
foreach (var contact in contacts)
{
Console.WriteLine(contact);
}
break;
}
Okay, if each item is broken down by three values consider chunking the data by three than iterate the results.
Change the Debug.WriteLine to Console.WriteLine below for your code.
Extension method for chunking
public static class ListExtensions
{
public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize)
=> source
.Select((value, index) => new { Index = index, Value = value })
.GroupBy(x => x.Index / chunkSize)
.Select(grp => grp.Select(v => v.Value).ToList())
.ToList();
}
File contents
John
111
John's address
Mary
222
Mary's address
Bill
333
Bill's address
Code to first assert data is divisible by three than read the data, no assertion of data types or if null values are performed.
var contents = File.ReadAllLines("TextFile1.txt");
if (contents.Length % 3 == 0)
{
var results = contents.ToList().ChunkBy(3);
foreach (var list in results)
{
Debug.WriteLine($" Name: {list[0]}");
Debug.WriteLine($" Number: {list[1]}");
Debug.WriteLine($"Address: {list[2]}");
}
}
else
{
Debug.WriteLine("Malformed");
}
Edit: placed into a method
private static void ViewAllContacts()
{
var fileName = "TextFile1.txt";
if (File.Exists(fileName))
{
var contents = File.ReadAllLines(fileName);
if (contents.Length % 3 == 0)
{
var results = contents.ToList().ChunkBy(3);
foreach (var list in results)
{
Debug.WriteLine($" Name: {list[0]}");
Debug.WriteLine($" Number: {list[1]}");
Debug.WriteLine($"Address: {list[2]}");
}
}
else
{
Debug.WriteLine("Malformed");
}
}
else
{
Debug.WriteLine($"{fileName} not found");
}
}
Save your contact to json format, like this:
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.IO;
namespace JsonTester
{
// Open Package Manager Console:
// Tools >> Nuget Package Manager >> Package Manager Console
//
// Install libraries through Package Manager Console:
// PM > Install-Package System.Text.Json -Version 6.0.1
internal class Program
{
static List<Contact> contactList = new List<Contact>();
static readonly string MY_CONTACT = #"D:\MyContact.txt";
class Contact {
public string Name { get; set; }
public string Number { get; set; }
public string Address { get; set; }
}
static void EnterContact() {
var contact = new Contact();
Console.WriteLine("Name: ");
contact.Name = Console.ReadLine();
Console.WriteLine("Number: ");
contact.Number = Console.ReadLine();
Console.WriteLine("Address: ");
contact.Address = Console.ReadLine();
contactList.Add(contact);
}
static void SaveContact() {
if (contactList.Count > 0) {
foreach (Contact contact in contactList) {
String strContactJson = JsonSerializer.Serialize(contact);
File.AppendAllText(MY_CONTACT, strContactJson + "\n");
}
}
}
static void ReadContact()
{
try
{
Console.WriteLine("Read contact list:\n");
String[] strContactJsonArr = File.ReadAllLines(MY_CONTACT);
if (strContactJsonArr.Length > 0)
{
foreach (String s in strContactJsonArr)
{
Contact contact = JsonSerializer.Deserialize<Contact>(s);
if (contact != null)
{
Console.WriteLine("Name: " + contact.Name);
Console.WriteLine("Number: " + contact.Number);
Console.WriteLine("Address: " + contact.Address);
Console.WriteLine("");
}
}
Console.WriteLine("Found {0} contacts\n\n", strContactJsonArr.Length);
}
}
catch (Exception) {
Console.WriteLine("Contact list is empty\n");
}
}
static bool SearchContact(String name)
{
try
{
String[] strContactJsonArr = File.ReadAllLines(MY_CONTACT);
if (strContactJsonArr.Length > 0)
{
foreach (String s in strContactJsonArr)
{
Contact contact = JsonSerializer.Deserialize<Contact>(s);
if (contact != null)
{
if (contact.Name.Equals(name)) {
Console.WriteLine("");
Console.WriteLine("Name: " + contact.Name);
Console.WriteLine("Number: " + contact.Number);
Console.WriteLine("Address: " + contact.Address);
Console.WriteLine("");
return true;
}
}
}
}
}
catch (Exception)
{
}
return false;
}
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("- Press S for search contact.\n- Press N for new contact.\n- Press R for read contact list.\n");
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.KeyChar == 's' || key.KeyChar == 'S')
{
Console.WriteLine("Searching contact.");
Console.Write("Enter name: ");
String name = Console.ReadLine();
bool isFounded = SearchContact(name);
if (isFounded)
{
Console.WriteLine("Contact is found\n");
}
else
{
Console.WriteLine("Contact isn't found!\n");
}
}
else if (key.KeyChar == 'n' || key.KeyChar == 'N')
{
EnterContact();
SaveContact();
}
else if (key.KeyChar == 'r' || key.KeyChar == 'R') {
ReadContact();
}
}
}
}
}
Console Output:
MyContact.txt (contact save in JSON format):

I use ".net core" .i want to search any keyword in a pdf file but this code is not working

static void Main(string[] args) {
var file = File.Open(Directory.GetCurrentDirectory() + "/Net.pdf", FileMode.Open);
var pattern = new Regex("kullan", RegexOptions.IgnoreCase);
//this line is not working
TextExtractor textExtractor = new TextExtractor();
var dddd = ReadToEnd(file);
var textStrings = textExtractor.Extract(dddd);
var matches = pattern.Matches(textStrings.Text);
foreach (var item in matches)
{
Console.WriteLine(item);
}
}
You can try something like this:
File file = new File(myPDF);
//pattern
var pattern = new Regex("kullan", RegexOptions.IgnoreCase);
var textExtractor = new TextExtractor();
foreach (var page in file.Document.Pages)
{
var strings = textExtractor.Extract(page);
var matchingText = pattern.Matches(TextExtractor.ToString(strings));
}
Tika:
try
{
var result = new TextExtractor().Extract(yourPDF.pdf).Text;
Console.WriteLine(result.Text.Length);
foreach(var line in result)
{
if(line.contains("kullen"))
/** Do Something **/
}
}
catch(Exception e)
{
Console.WriteLine("Error occurred: " + e);
}
I did it like that, thank you your answer
namespace pro
{
class Program
{
static void Main(string[] args)
{
string b = pdfText(Directory.GetCurrentDirectory()+ "/Net.pdf");
string a= "kullan";
int sonuc;
sonuc = b.IndexOf(a,0, b.Length);
if(sonuc==-1)
{
Console.WriteLine("not found");
}
else
{
Console.WriteLine("found from " + sonuc.ToString() + ". character");
}
}
public static string pdfText(string path)
{
PdfReader reader = new PdfReader(path);
var dd = reader.GetPageContent(1);
string text = string.Empty;
for (int page = 1; page <= reader.NumberOfPages; page++)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
text += PdfTextExtractor.GetTextFromPage(reader, page);
}
reader.Close();
return text;
}
}
}

Parsing out URLs in email messages

I'm having an issue in some of my code, i cannot seem to think of the best way to do this, all i want to do is extract a URL from a pop3 email message depending on if the domain is in the "to check" array, for example if "stackoverflow.com" is in the email message, i would extract all urls in the message body that contains "stackoverflow.com" in it, and just perform a quick WebClient() request with that url.
Code:
private void BgwEmails_DoWork_1(object sender, DoWorkEventArgs e)
{
try
{
bool finished = false;
Pop3Client pop3 = new Pop3Client();
if (pop3.Connected)
{
pop3.Disconnect();
}
pop3.Connect(txtBoxMailServer.Text, int.Parse(txtBoxPort.Text), chkSSL.Checked);
pop3.Authenticate(txtBoxUsername.Text, txtBoxPassword.Text, AuthenticationMethod.UsernameAndPassword);
int messageCount = pop3.GetMessageCount();
if (messageCount == 0)
{
return;
}
Helpers.ReturnMessage("[ " + messageCount + " ] Message(s) in your inbox.");
int count = 0;
for (int d = 1; d <= messageCount; d++)
{
bgwEmails.WorkerSupportsCancellation = true;
if (bgwEmails.CancellationPending)
{
e.Cancel = true;
Helpers.ReturnMessage($"Cancelling link activator ...");
return;
}
if (finished)
{
return;
}
OpenPop.Mime.Message message = null;
message = pop3.GetMessage(d);
if (null == message || null == message.MessagePart || null == message.MessagePart.MessageParts) {
continue;
}
string textFromMessage = null;
try
{
textFromMessage = message?.MessagePart?.MessageParts[0]?.GetBodyAsText();
} catch (Exception) {
continue;
}
MessagePart messagePart = message.MessagePart.MessageParts[0];
if (null == messagePart || null == messagePart.BodyEncoding || null == messagePart.Body || null == messagePart.BodyEncoding.GetString(messagePart.Body)) {
continue;
}
string linkToCheck;
using (var wc = new WebClient())
{
linkToCheck = wc.DownloadString("https://www.thesite.com/api.php?getActivationLinks=1");
}
var linksToClickArray = linkToCheck.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
//Helpers.ReturnMessage(textFromMessage);
for (var i = 0; i < linksToClickArray.Length; i++)
{
var regex = new Regex(linksToClickArray[i], RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);
var matches = regex.Matches(textFromMessage);
foreach (Match match in matches)
{
if (match.Success)
{
count++;
Invoke(new MethodInvoker(() => { listBoxEmailsClicked.Items.Add("Clicked: " + match.Value); }));
using (WebClient wc = new WebClient())
{
Helpers.ReturnMessage(match.Value);
wc.DownloadString(match.Value);
}
}
}
}
if (null != textFromMessage)
{
Invoke(new MethodInvoker(() => { txtStatusMessages.AppendText(textFromMessage); }));
}
}
Helpers.ReturnMessage($"Emails downloaded successfully! You clicked [ " + count + " ] activation links.");
} catch (Exception ex) {
Helpers.ReturnMessage($"POP3 Error: " + ex.Message + ".");
}
}
I do a request here: https://www.thesite.com/api.php?getActivationLinks=1 which contains a list of domains to check for in the messages, they just come back one line at a time like:
thesite1.com
thesite2.com
etc
The code works as far as connetcing and downloading, i just cannot seem to think of a way to parse of the urls only if it matches a domain in the target list, any help or advice would be appreciated.

Is my PLINQ code threadsafe?

I have the following code:
static void Main(string[] args)
{
DateTime currentDay = DateTime.Now;
List<Task> taskList = new List<Task>();
List<string> markets = new List<string>() { "amex", "nasdaq", "nyse", "global" };
Parallel.ForEach(markets, market =>
{
Downloads.startInitialMarketSymbolsDownload(market);
}
);
Console.WriteLine("All downloads finished!");
}
public static void startInitialMarketSymbolsDownload(string market)
{
try
{
object valueTypeLock = new object();
List<string> symbolList = new List<string>() { "GOOG", "YHOO", "AAP" }
var historicalGroups = symbolList.AsParallel().Select((x, i) => new { x, i })
.GroupBy(x => x.i / 100)
.Select(g => g.Select(x => x.x).ToArray());
historicalGroups.AsParallel().ForAll(g => {
lock (valueTypeLock) {
getHistoricalStockData(g, market);
}
});
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
public static void getHistoricalStockData(string[] symbols, string market)
{
// download data for list of symbols and then upload to db tables
Uri uri;
string url, line;
decimal open = 0, high = 0, low = 0, close = 0, adjClose = 0;
DateTime date;
Int64 volume = 0;
string[] lineArray;
List<string> symbolError = new List<string>();
Dictionary<string, string> badNameError = new Dictionary<string, string>();
System.Net.ServicePointManager.DefaultConnectionLimit = 1000;
Parallel.ForEach(symbols, symbol =>
{
url = "http://ichart.finance.yahoo.com/table.csv?s=" + symbol + "&a=00&b=1&c=1900&d=" + (DateTime.Now.Month - 1) + "&e=" + DateTime.Now.Day + "&f=" + DateTime.Now.Year + "&g=d&ignore=.csv";
uri = new Uri(url);
using (ooplesfinanceEntities entity = new ooplesfinanceEntities())
using (WebClient client = new WebClient())
using (Stream stream = client.OpenRead(uri))
using (StreamReader reader = new StreamReader(stream))
{
entity.Database.Connection.Open();
while (reader.EndOfStream == false)
{
line = reader.ReadLine();
lineArray = line.Split(',');
// if it isn't the very first line
if (lineArray[0] != "Date")
{
switch (market)
{
case "amex":
DailyAmexData amexData = new DailyAmexData();
var amexQuery = from r in entity.DailyAmexDatas.AsParallel().AsEnumerable()
where r.Date == date
select new StockData { Close = r.AdjustedClose };
List<StockData> amexResult = amexQuery.AsParallel().ToList();
if (amexResult.AsParallel().Count() > 0) // **never hits this breakpoint line**
{
// add the row to the table if it isn't there
// now checks for stock splits and updates if necessary
if (amexResult.AsParallel().FirstOrDefault().Close != adjClose)
{
// this means there is a stock split so it needs to have the other adjusted close prices updated
amexResult.AsParallel().FirstOrDefault().Close = adjClose;
}
else
{
continue;
}
}
else
{
// set the data then add it
amexData.Symbol = symbol;
entity.DailyAmexDatas.Add(amexData);
}
break;
default:
break;
}
}
}
// now save everything
entity.SaveChanges();
Console.WriteLine(symbol + " added to the " + market + " database!");
}
}
);
}
Everything starts fine and I get no exceptions but I'm getting no results and the code gets stuck one the line that I marked and the memory keeps shooting up. I figured the problem was with the above code because maybe I was doing something wrong. I just don't know where to start as this is my first time dealing with plinq/parallel processing and the tutorials don't show anything this complex.

Looping trought a list of objects in C# in a better way?

I have a foreach loop that loops trought a list of objects. The meaning of it is to set a NavigateUrl to a Hyperlink. My code looks like this:
foreach (var con in contacts)
{
if (con.ContactTypeID == 1)
{
FacebookIcon.NavigateUrl = "http://facebook.com/" + con.ContactURL;
}
}
I wonder if their is some better way to do it. I will have about 10 other ContactTypeID and I rather don't write nine more if else..
You could use LINQ:
var facebookURL = contacts.Where(c => c.ContactTypeID == 1)
.Select(c => c.url)
.FirstOrDefault();
if(facebookURL != null)
FacebookIcon.NavigateUrl = "http://facebook.com/" + facebookURL;
Edit: Actually you could benefit of LINQ's deferred execution to reuse the same for every type of contact-type:
var contactType = 1; // facebook
var url = contacts.Where(c => c.ContactTypeID == contactType)
.Select(c => c.url);
if (url.Any())
FacebookIcon.NavigateUrl = "http://facebook.com/" + url.First();
contactType = 2; // google
if (url.Any())
GoogleIcon.NavigateUrl = "http://Google.com/" + url.First();
Edit 2: Here's another approach using a Dictionary mapping all types with their URLs which should be more efficient in case you have millions of types ;-) (#MAfifi):
var urlTypeMapping = contacts.GroupBy(c => c.ContactTypeID)
.ToDictionary(grp => grp.Key, grp => grp.Select(c => c.url));
foreach (var type in urlTypeMapping)
{
var typeUrl = type.Value.FirstOrDefault();
if (typeUrl != null)
{
switch (type.Key)
{
case 1:
FacebookIcon.NavigateUrl = "http://facebook.com/" + typeUrl;
break;
case 2:
GoogleIcon.NavigateUrl = "http://Google.com/" + typeUrl;
break;
default:
break; //or throw new Exception("Invalid type!");
}
}
}
You could use LINQ in order to do what you want.
var x = contacts.FirstOrDefault (c => c.ContactTypeID == 1);
if( x != null )
{
FacebookIcon.NavigateUrl = String.Format ("http://facebook.com/{0}", x.ContactURL);
}
You can use switch
switch (caseSwitch)
{
case 1:
FacebookIcon.NavigateUrl = "http://facebook.com/" + con.ContactURL;
break;
case 2:
//
break;
default:
//
break;
}
you can use linq.
var con = contacts.FirsOrDefault(c => c.ContactTypeID.Equals(1));
if (con == null)
{
return;
}
con.NavigateUrl = "http://facebook.com/" + con.ContactURL;
Or if you have more id's
List<int> ids = new List<int> {1,2,5,7};
contacts.Where(c => ids.Containt(c.ContactTypeID)).ToList().ForEach(item => item.NavigateUrl = "http://facebook.com/" + item.ContactURL);
If you use Linq You should use First or FirstOrDefault
var url = contacts.FirstOrDefault(c => c.ContactTypeID == 1).NavigateUrl;
Use a switch:
foreach (var con in contacts)
{
switch (con.ContactTypeID)
{
case 1:
FacebookIcon.NavigateUrl = "http://facebook.com/" + con.ContactURL;
break;
case 2:
. . .
break;
. . .
}
}
Perhapse a condensed LINQ:
contacts.ForEach(c => { if (c.ContactTypeID == 1) FacebookIcon.NavigateUrl = "http://facebook.com/" + con.ContactURL; });
If you want to do "it" for each ContactTypeID == 1.
I assume that each contact is not necessarily Facebook and you need to dynamically set different attributes based on what the contact is?
Your best bet is a Dictionary<int, Action> or similar, where you just do something like,
var setCorrectUrl = new Dictionary<int, Action<Contact>>
{
// Appropriate entries in here, e.g. (syntax not quite right)
{
1,
(contact) => FacebookIcon.NavigateUrl = contact.ContactURL;
}
}
foreach (var con in contacts)
{
setCorrectUrl[con.ContactTypeID](con);
}
Well, I would do some refactoring of the code.
Imagine he would have 10 other types to implement :)
The solutions provided above are workable but not very elegant in terms of extensibility.
So, here is my solution:
1) Implement a base class with the common contact properties
public abstract class BaseContact
{
public string Name { get; set; }
public abstract string Url { get; set; }
}
2) Implement the concrete types
public class FbContact : BaseContact
{
private string _baseUrl = "http://facebook.com/{0}";
private string _url = string.Empty;
public override string Url
{
get { return _url; }
set { _url = string.Format(_baseUrl, value); }
}
}
public class LinkedInContact : BaseContact
{
private string _baseUrl = "http://linkedin.com/{0}";
private string _url = string.Empty;
public override string Url
{
get { return _url; }
set { _url = string.Format(_baseUrl, value); }
}
}
3) This is just a helper class for setting the navigation url
public static class NavigationCreator
{
public static void SetUrl(BaseContact contact, HyperLink link)
{
link.NavigateUrl = contact.Url;
}
}
4) Some test code to visualize the result
List<BaseContact> items = new List<BaseContact>();
for (int i = 0; i < 5; i++)
{
BaseContact item;
if (i % 2 == 0) item = new FbContact(); else item = new LinkedInContact();
item.Url = "My name " + i;
items.Add(item);
}
foreach (var contact in items)
{
HyperLink link = new HyperLink();
NavigationCreator.SetUrl(contact, link);
Console.WriteLine(link.NavigateUrl);
}
Console.Read();

Categories

Resources