Forum Discussion

Gabriel Smoljar's avatar
Gabriel Smoljar
Brass Contributor
Apr 19, 2017

CreateDocumentFromTemplate CSOM methods does not seem to work

I've been trying to create documents from template in SharePoint Online using methods that were added in a more recent update of CSOM. However I can't get it to work. All methods fails in different ways. Is there a working example?

 

// TODO: Assign the following strings
//private static string Username = "";
//private static string Password = "";
//private static string WebUrl = "";
//private static string TemplateFileUrl = "";

private static void Main(string[] args)
{
    var password = new SecureString();
    foreach (var c in Password) password.AppendChar(c);

    var credentials = new SharePointOnlineCredentials(Username, password);
    var clientContext = new ClientContext(WebUrl)
    {
        Credentials = credentials
    };

    using (clientContext)
    {
        Console.WriteLine("CreateDocument");

        try
        {
            CreateDocument(clientContext);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message); // Creates empty file
            throw;
        }

        Console.WriteLine("CreateDocumentFromTemplate");

        try
        {
            CreateDocumentFromTemplate(clientContext);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message); // Creates invalid file. Word say "There was a problem with its contents"
            throw;
        }

        Console.WriteLine("CreateDocumentFromTemplateStream");

        try
        {
            CreateDocumentFromTemplateStream(clientContext); // Creates invalid file. Word say "There was a problem with its contents"
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw;
        }

        Console.WriteLine("CreateDocumentFromTemplateBytes");

        try
        {
            CreateDocumentFromTemplateBytes(clientContext);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message); // Throws "Unknown error"
        }

        Console.ReadLine();
    }
}

private static void CreateDocument(ClientContext clientContext)
{
    var documents = clientContext.Web.DefaultDocumentLibrary();
    clientContext.Load(documents);
    clientContext.ExecuteQuery();

    var document = documents.CreateDocument($"{DateTime.Now.Ticks.ToString()}.docx", documents.RootFolder, DocumentTemplateType.Word);
    clientContext.ExecuteQuery();
}

private static void CreateDocumentFromTemplate(ClientContext clientContext)
{
    var documents = clientContext.Web.DefaultDocumentLibrary();
    clientContext.Load(documents);
    clientContext.ExecuteQuery();

    var document = documents.CreateDocumentFromTemplate($"{DateTime.Now.Ticks.ToString()}.docx", documents.RootFolder, TemplateFileUrl);
    clientContext.ExecuteQuery();
}

private static void CreateDocumentFromTemplateStream(ClientContext clientContext)
{
    var template = clientContext.Web.GetFileByUrl(TemplateFileUrl);
    clientContext.Load(template);
    clientContext.Load(template, t => t.ListId);
    clientContext.ExecuteQuery();

    var documents = clientContext.Web.Lists.GetById(template.ListId);
    clientContext.Load(documents);
    clientContext.ExecuteQuery();

    var stream = template.OpenBinaryStream();
    clientContext.ExecuteQuery();

    var document = documents.CreateDocumentFromTemplateStream($"{DateTime.Now.Ticks.ToString()}.docx", documents.RootFolder, "docx", stream.Value);
    clientContext.Load(document);
    clientContext.ExecuteQuery();
}

private static void CreateDocumentFromTemplateBytes(ClientContext clientContext)
{
    var template = clientContext.Web.GetFileByUrl(TemplateFileUrl);
    clientContext.Load(template);
    clientContext.Load(template, t => t.ListId);
    clientContext.ExecuteQuery();

    var documents = clientContext.Web.Lists.GetById(template.ListId);
    clientContext.Load(documents);
    clientContext.ExecuteQuery();

    var stream = template.OpenBinaryStream();
    clientContext.ExecuteQuery();

    byte[] bytesarray = null;

    using (var mStream = new System.IO.MemoryStream())
    {
        if (stream != null)
        {
            stream.Value.CopyTo(mStream);
            bytesarray = mStream.ToArray();
        }

        var document = documents.CreateDocumentFromTemplateBytes($"{DateTime.Now.Ticks.ToString()}", documents.RootFolder, bytesarray, "dotx");
        clientContext.ExecuteQuery();
    }
}
  • Joshua Knapp's avatar
    Joshua Knapp
    Copper Contributor

    If these questions are just going to sit here for a year without resolution or a response from microsoft, why are these forums even here? I'm tired of searching for these answers and only being met with a dead end. 

     

    This is unacceptable Microsoft.

Resources