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