Hey there,
this is my first time developing on a sharepoint and what I am trying to do is opening a Powerpoint presentation and editing a slide in it. Since I just started everything I have done so far is to log into the sharepoint, open the presentation and then close it again. Sad thing is though, that my presentation always is corrupted after the program ran. Can somebody help me out and point to what I am doing wrong?
Here is my code so far:
class Program
{
private static NetworkCredential credential = new NetworkCredential("login", "pwd", "domain");
static private void CopyStream(Stream source, Stream destination)
{
byte[] buffer = new byte[32768];
int bytesRead;
do
{
bytesRead = source.Read(buffer, 0, buffer.Length);
destination.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
}
static void Main(string[] args)
{
try
{
ClientContext clientContext =
new ClientContext("https://sharepoint.adress.com/kunden/subfolder/");
clientContext.Credentials = credential;
Web oWebsite = clientContext.Web;
ListCollection collList = oWebsite.Lists;
clientContext.Load(collList);
clientContext.ExecuteQuery();
Console.WriteLine("Opened Sharepoint.");
Guid id = new Guid("2BBF9030-66C6-4F71-86E6-DFE41F57B9F3");
List sharedDocumentsList = clientContext.Web.Lists.GetById(id);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>TestSlide.pptx</Value></Eq></Where><RowLimit>1</RowLimit></Query></View>";
ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
clientContext.Credentials = credential;
clientContext.Load(sharedDocumentsList);
clientContext.ExecuteQuery();
clientContext.Credentials = credential;
clientContext.Load(listItems);
clientContext.ExecuteQuery();
Console.WriteLine("Opened Document.");
if (listItems.Count == 1)
{
ListItem item = listItems[0];
clientContext.Credentials = credential;
FileInformation fileInformation = ClientOM.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);
using (MemoryStream memoryStream = new MemoryStream())
{
CopyStream(fileInformation.Stream, memoryStream);
using (PresentationDocument ppt = PresentationDocument.Open(memoryStream, true))
{
}
ClientOM.File.SaveBinaryDirect(clientContext, (string)item["FileRef"], memoryStream, true);
}
}
else
{
Console.WriteLine("Document not found.");
}
Console.WriteLine("Done.");
Console.Read();
}
catch (Exception ex) when (ex is ServerException || ex is InvalidCastException || ex is OpenXmlPackageException || ex is WebException)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
}