0

I am getting an error while pragmatically creating .docx file from template. Here is my code:

 SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        string TemplateUrl = string.Empty;
                        try
                        {
                            TemplateUrl = url + "/Projects/Forms/template.dotx";
                        }
                        catch { }


                        web.AllowUnsafeUpdates = true;
                        SPList olist = web.Lists["Projects"];
                        SPFolder newfolder = olist.RootFolder;
                        SPFile file = null;
                        SPContentTypeCollection currentOrder = olist.ContentTypes;
                        List<SPContentType> result = new List<SPContentType>();
                            foreach (SPContentType ct in currentOrder)
                            {
                                if (ct.Name.Contains("Document"))
                                {
                                    result.Add(ct);
                                }
                            } 

                            file = web.GetFile(TemplateUrl);

                            if (file != null)
                            {
                                web.AllowUnsafeUpdates = true;
                                     Stream readStream =         file.OpenBinaryStream(SPOpenBinaryOptions.None); //file is SPFile type
                                SPFile uploadedFile = newfolder.Files.Add(newfolder.Url + @"/" + prjName + ".docx", readStream, true);
                                uploadedFile.CheckOut();
                                SPListItem listitem = uploadedFile.Item;
                                listitem["Title"] = prjName;
                                listitem["Team_x0020_Members"] = fv1;
                                listitem.Update();
                                uploadedFile.Update();
                                uploadedFile.CheckIn(string.Empty);
                                olist.RootFolder.UniqueContentTypeOrder = result;
                                olist.RootFolder.Update(); 
                                web.AllowUnsafeUpdates = false;
                            }
                    }
                }
            });

And here's the error I get when opening the new Word document: enter image description here

5
  • What's the size of the new file, as reported by SharePoint when you display the file properties?
    – Evariste
    Mar 4, 2016 at 11:14
  • as of now there is no content i have written in the file. As i want blank file to be created. The blank file size is 22.0 KB
    – DRS
    Mar 4, 2016 at 11:22
  • Document is getting uploaded properly. but when i download from the library and try to open it i am getting an error
    – DRS
    Mar 4, 2016 at 11:35
  • I understand you want the new file to be a blank one, copied from Template.dotx. But can you check the size of the new file? My assumption is it's 0-byte, thus the error when you open it Word.
    – Evariste
    Mar 4, 2016 at 12:36
  • Yes its 22 KB not 0 byte. And if your assumption is such then can you tell me how to add some content into the file so that it can open properly. As i don't have knowledge about it.
    – DRS
    Mar 4, 2016 at 12:55

1 Answer 1

0

Add the content type as "Document" in your code . File created through the code does not inherit the content type required for a file .

1
  • I have updated my code as above. but no effect. still the same error
    – DRS
    Mar 4, 2016 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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