Pregunta

Tuve que generar un documento de una plantilla.Tengo un tipo de contenido con una plantilla asociada (un archivo .Dotx).¿Cómo puedo hacer para crear un documento (programáticamente) de esa plantilla?

He intentado de esta manera:

                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();

Encontré un documento en mi DOCLIB que equivale a la plantilla de origen, pero sigue siendo una plantilla, por lo que si intenté abrirlo, el sistema me contó hay un error.Si lo renombro a Dotx, funciona como plantilla.

¿Alguna sugerencia?¿Hay otra manera de "implementar" un doc de una plantilla dada doctora programáticamente?

Gracias

¿Fue útil?

Solución

a continuación es el código ...

que funciona perfectamente en mi sistema ...

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
        }

    }

espero que funcione.

Para obtener más detalles, consulte los enlaces a continuación ..

debajo de un par de enlaces que te ayudan:

Licenciado bajo: CC-BY-SA con atribución
scroll top