Search text in document: HTTP 403 Forbidden
-
05-02-2021 - |
Pregunta
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?
Solución
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
});