Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 2 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..

http://somnathmatere.blogspot.in/2011/05/how-to-programmatically-upload-document.html

http://www.sharepointbleached.com/2012/04/upload-document-programmatically.html

http://whitepages.unlimitedviz.com/2011/02/using-document-templates-with-lists-and-content-types-in-sharepoint/

http://www.bfcnetworks.com/pull-sharepoint-2010-document-properties-into-word-quick-parts/

share|improve this answer
add comment

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.