Question

I had to generate a document from a template. I have a Content Type with associated a Template (a .dotx file). How can I do to create a document (programmatically) from that template?

I tried this way:

                SPList clienti = web.Lists["LISTNAME"];

                SPContentType ct = web.ContentTypes["CTNAME"];


                var file = web.GetFile(ct.DocumentTemplateUrl);
                SPFile fileAppenaAggiunto = clienti.RootFolder.Files.Add("nomeDaProg12.docx", file.OpenBinaryStream());
                SPListItem appenaAggiunto = fileAppenaAggiunto.Item; 
                appenaAggiunto["ContentTypeId"] = clienti.ContentTypes["CTNAME"].Id;
                appenaAggiunto["FIELD1"] = "asd";
                appenaAggiunto["FIELD2"] = "Bozza";

                appenaAggiunto.Update();

I found a document in my DocLib that equals to the source template but it is still a Template so if I tried to open it the system tolds me there is an error. If I rename it to dotx, it works as template.

Any suggestion? There is another way to "deploy" a doc from a given template doc programmatically?

Thank you

Was it helpful?

Solution

below is the code...

which working perfectly in my system..

public void UpdateAndCreateFile(SPWeb web)
    {
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                string TemplateUrl = string.Empty;
                try
                {
                    TemplateUrl = "Your Document templated full path";
                }
                catch { }


                web.AllowUnsafeUpdates = true;
                SPList olist = web.Lists["Document Library"];
                String url = olist.RootFolder.ServerRelativeUrl.ToString();


                string foldername = Convert.ToString("Foldername in document library");
                SPFolder newfolder = web.GetFolder(url + "/" + foldername);

                if (!newfolder.Exists)
                {
                    SPFolderCollection folders = web.GetFolder(url).SubFolders;
                    //Create new folder
                    folders.Add(foldername);
                }

                SPFile file = null;

                file = web.GetFile("Your Document templated full path");

                if (file != null)
                {
                    web.AllowUnsafeUpdates = true;
                    Stream readStream = file.OpenBinaryStream(); //file is SPFile type
                    SPFile uploadedFile = newfolder.Files.Add(newfolder.Url + @"/" + "NewDocName.docx", readStream, true);
                    uploadedFile.CheckOut();
                    SPListItem listitem = uploadedFile.Item;
                    // Details is mapped in document
                    listitem["Details"] = "this content will add in document";
                    listitem.Update();
                    uploadedFile.Update();
                    uploadedFile.CheckIn(string.Empty);
                    web.AllowUnsafeUpdates = false;
                }
            });






        }
        catch (Exception ex)
        {
            // handle exception here
        }

    }

Hope it works..

For more details, refer below links..

below couple of links which helps you :

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top