0

I wrote the code for: searching (replace) for a word in a document that works without problems in the Console Application, but does not work in Visual Web Part SharePoint 2013, when I click the button, IIS crashes with:

System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005. System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType) в System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType) в System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj) в System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) в System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) в System.Activator.CreateInstance(Type type, Boolean nonPublic) в System.Activator.CreateInstance(Type type) в SearchKeyWordDocuments.VisualWebPart1.VisualWebPart1UserControl.FindKeyWords()

Code:

string docfile = @"C:\Log\Base.docx";
if (System.IO.File.Exists((string)docfile))
{
     using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(docfile, true))
     {
          string docText = null;
          using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
          {
               docText = sr.ReadToEnd();
          }

          Regex regexText = new Regex("Hello world!");
          docText = regexText.Replace(docText, "[[Tags]]");

          using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
          {
               sw.Write(docText);
          }
     }
}

Full access rights, tried from different browsers and different accounts does not help, maybe something is not activated on the side of IIS?

  • Did you run the console app on the same server or on your computer? It seems that you did not install Office or PIA on the server or not using DocumentFormat.OpenXml nuget in your project. Last option is preferred. – Lukas Nespor Feb 6 at 13:31
0

The problem was that I only had to call code with top-level rights on my ASP.NET application (SharePoint), using method SPSecurity.RunWithElevatedPrivileges.

SPSecurity.RunWithElevatedPrivileges(delegate() {
//code is here
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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