0

This Code is working very fine with admin rights but when i tried using it with normal user didnt work.

I have also provided user full control on the virtual directory folder.

public override void ItemAdded(SPItemEventProperties properties)
     {
    base.ItemAdded(properties);    
    SPSecurity.RunWithElevatedPrivileges(delegate()
     {
    if(String.Compare(properties.ListTitle, ConstantCLS.LIB_NAME, true) == 0)
    {
     strFileName = properties.ListItem.Name;
     paramMissing =Type.Missing;
if (properties.AfterUrl.IndexOf('/') == properties.AfterUrl.LastIndexOf('/'))    
     {
     folderPath =string.Empty;    
     folderPath =HostingEnvironment.ApplicationPhysicalPath;    
     }else
     {    
     folderPath = properties.AfterUrl.Substring(properties.AfterUrl.IndexOf(
    '/') + 1, properties.AfterUrl.LastIndexOf("/") - (properties.AfterUrl.IndexOf('/') + 1));    
     folderPath = folderPath.Replace("/", "\\");
     folderPath =HostingEnvironment.ApplicationPhysicalPath + folderPath;
if (System.IO.Directory.Exists(folderPath))    
     {
     }else
     {
     System.IO.Directory.CreateDirectory(folderPath);
     }    
     folderPath = folderPath + "\\";
 }
object paramSourcePath = folderPath + strFileName;    
     paramSourceBookPath = folderPath + strFileName;    
     WebClientFunction(properties.WebUrl, properties.ListItem.Url, strFileName);
if (properties.ListItem.Name.EndsWith(".docx"))    
     { paramExportFilePath = paramSourceBookPath.Replace(".docx", "_docx.pdf");
     }else if (properties.ListItem.Name.EndsWith(".doc"))
     {
     paramExportFilePath = paramSourceBookPath.Replace(".doc", "_doc.pdf");
     }
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
    bool paramOpenAfterExport = false;
    WdExportOptimizeFor paramExportOptimizeFor =WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
    bool paramBitmapMissingFonts = true;
    bool paramUseISO19005_1 = false;
    try    
     {    
     wordDocument = wordApplication.Documents.Open(ref paramSourcePath, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing);
    if (wordDocument != null)    
     wordDocument.ExportAsFixedFormat(paramExportFilePath,paramExportFormat, paramOpenAfterExport,paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1,ref paramMissing);    
     }catch (Exception ex)
     {// Respond to the error    
     }
    finally    
     {
    if (wordDocument != null)
     { wordDocument.Close(ref paramMissing, ref paramMissing,ref paramMissing);    
     wordDocument = null;    
     }
if (wordApplication != null)    
     {    
     wordApplication.Quit(ref paramMissing, ref paramMissing,ref paramMissing);    
     wordApplication = null;    
     }
GC.Collect();
GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();    
     }
 sharePointSite = ConstantCLS.SITEURL;    
     properties.AfterUrl.LastIndexOf('/');
    string uploadPath = properties.AfterUrl.Substring(0, properties.AfterUrl.LastIndexOf('/') + 1);
 documentLibraryName = properties.ListTitle;
    using (SPSite oSite = new SPSite(sharePointSite))
     {
    using (SPWeb oWeb = oSite.OpenWeb())
     {
    if (!System.IO.File.Exists(paramExportFilePath))    
    throw new FileNotFoundException("File not found.", paramExportFilePath);
    SPFolder myLibrary = oWeb.Folders[documentLibraryName];
    Boolean replaceExistingFiles = true;
    String fileName = System.IO.Path.GetFileName(paramExportFilePath);
    FileStream fileStream = File.OpenRead(paramExportFilePath);
    SPFile spfile = myLibrary.Files.Add(uploadPath + fileName, fileStream, replaceExistingFiles);
     myLibrary.Update();
     }}}
    });}

Can somebody tell me what is problem in this code. I have also read somewhere that for some reason writing to text file inside RunWithElevatedPrevilages method had issues.

Thanks and Regards

3
  • If you want answers, try and describe what you are trying to do, and what works and what doesnt. Commented Dec 22, 2011 at 7:46
  • i am trying to convert the doc file to pdf file whenever a new file is uploaded using microsoft interlop.
    – Mohit
    Commented Dec 22, 2011 at 7:49
  • 1
    log files from ULS? Where does it fail? Have you attached a debugger? If so in what line does it fail? In the current state you probably wont get a good answer to your question. Think about this: the more you prepare the question for the users of this site, the better the chance that somebody cares and understands enough about the question that they might actually answer :) Commented Dec 22, 2011 at 19:10

2 Answers 2

0

A good enterprise solution to convert Office documents on SharePoint is a thrid party tool from AdLib: http://www.adlibsoftware.com/

If that is too much for your purpose you can find lots of other converters on the Internet, e.g. Bamboo and Muhimbi.

5

Ouch! Are you trying to use Word Interop in an event receiver! You should not! This will instantiate Word (the actual application) on the server side and this is a really scary and bad operation! Just don't do it! It's not even supported, but that's not the issue here - you server will eventually fail miserably...

Now, on to how to do it! Converting from Word to PDF is natively supported in SharePoint Server using the Word Automation Services. How to do it is documented on MSDN and a plethora of places, but I recommend you to read Mr. Zimmergrens (did you know he's wearing yellow pants :-) post on the subject: http://zimmergren.net/archive/2010/08/30/sp-2010-developing-with-the-word-automation-services-in-sharepoint-server-2010.aspx

2
  • I have tried word automation service but further i need to convert excel and ppt to pdf, that is the reasoni chose interlop. Do let me know if some other options are available.
    – Mohit
    Commented Dec 22, 2011 at 10:07
  • Use OpenXML in combination with Word Automation Services. Use an AltChunk to insert Excel or PPT content into a Word document... Commented Dec 22, 2011 at 10:33

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.