Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Error creating document from using list using Document Template

$
0
0

Hi All,

I am trying to create a document from sharepoint list and upload it to a document library using item added and item updated event receiver methods. I can create metadata from list update in document library but unable to open the document. The error message I get is "we're sorry. we can't open "Filename" because we found a problem with its contents. no error detail available". Please see the below code.

SPSecurity.RunWithElevatedPrivileges(delegate()
               {
                   try
                   {

                       string TemplateUrl = string.Empty;
                       SPWeb web = properties.OpenWeb();
                       web.AllowUnsafeUpdates = true;
                       SPDocumentLibrary olist = web.Lists["NewLibList"] as SPDocumentLibrary;
                       String url = olist.RootFolder.ServerRelativeUrl.ToString();
                       SPFile file = null;
                       file = web.GetFile(olist.DocumentTemplateUrl);

                       if (file != null)
                       {
                           SPListItem oItems = properties.ListItem;
                           web.AllowUnsafeUpdates = true;
                           byte[] readStream = file.OpenBinary();
                           SPFile uploadedFile = olist.RootFolder.Files.Add("http://site/subsite/Libraryname/" + properties.ListItemId.ToString() + ".docx", readStream, true);
                           SPListItem listitem = uploadedFile.Item;
                           listitem["Firstname"] = oItems["Firstname"];
                           listitem["Lastname"] = oItems["Lastname"];
                           if (uploadedFile.CheckOutType != SPFile.SPCheckOutType.None)
                           {
                               uploadedFile.CheckIn(string.Empty);
                           }
                           listitem.Update();
                           uploadedFile.Update();

                           web.AllowUnsafeUpdates = false;
                       }

                   }
                   catch (Exception ex)
                   {
                       EventLog.WriteEntry(EventLogName, ex.Message, EventLogEntryType.Error);
                   }
               });


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>