SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
1  
Can that code be used within an event receiver with O365? – user16470 Apr 19 '13 at 4:30
    
I don't know about this feature.. do you have some docs to link me? Thank you! – Ziba Leah Apr 19 '13 at 8:01
up vote 4 down vote accepted

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 :

share|improve this answer
    
Hi, this looks very interesting but where would I go to use this code? Do I need to create a Custom Workflow Action, a custom Web Part ...? Please give me some pointers and links if possible. Thanks! – hhoud Sep 9 '15 at 9:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.