Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, CMS A content management system (CMS) is a software application or set of related programs that are used to create and manage digital content. CMSes are typically used for enterprise content management (ECM) and web content management (WCM) Learn More, Sitefinity Telerik Sitefinity is an ASP.NET 2.0-based Content Management System (CMS) that enables the construction of dynamic, fully editable Web sites. Sitefinity provides the tools for quick and easy construction of a wide variety of attractive and functional Web sites. Learn More, Sitefinity 14.2 Sitefinity is an online website building tool (application), referred to as a Web Content Management System (CMS). It is a .NET based CMS developed by Progress Software. Its version 14.2 is released by Progress on June 28, 2022 Learn More,

"Telerik.Sitefinity.Security.SecurityDemandFailException" exception while trying to create and upload the document to sitefinity's document library

Description:I am getting following exception while trying to upload document to sitefinity's document library

Telerik.Sitefinity.Security.SecurityDemandFailException
Telerik.Sitefinity.Libraries.Model.DocumentLibrary, Telerik.Sitefinity.Model was not granted ManageDocument in Document for principals with IDs 00000000-0000-0000-0000-000000000000


Telerik.Sitefinity.Libraries.Model.DocumentLibrary, Telerik.Sitefinity.Model was not granted ManageDocument in Document for principals with IDs 00000000-0000-0000-0000-000000000000
I am using following method and it is throwing exception while I am trying to set its parent library as seen in the above screenshot

public static void CreateDocumentNativeAPI(Guid masterDocumentId, string parentDocumentLibraryUrlName, string documentTitle, Stream documentStream, string documentFileName, string documentExtension)
{
LibrariesManager librariesManager = LibrariesManager.GetManager();
Document document = librariesManager.GetDocuments().Where(d => d.Id == masterDocumentId).FirstOrDefault();
if (document == null)
{
//The document is created as master. The masterDocumentId is assigned to the master version.
document = librariesManager.CreateDocument(masterDocumentId);

//Set the parent document library.
DocumentLibrary documentLibrary = librariesManager.GetDocumentLibraries().Where(d => d.UrlName.ToLower() == parentDocumentLibraryUrlName).SingleOrDefault();
document.Parent = documentLibrary;

//Set the properties of the document.
document.Title = documentTitle;
document.DateCreated = DateTime.UtcNow;
document.PublicationDate = DateTime.UtcNow;
document.LastModified = DateTime.UtcNow;
document.UrlName = Regex.Replace(documentTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
document.MediaFileUrlName = Regex.Replace(documentFileName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

//Recompiles and validates the url of the document.
librariesManager.RecompileAndValidateUrls(document);

//Upload the document file.
librariesManager.Upload(document, documentStream, documentExtension);

//Save the changes.
librariesManager.SaveChanges();

//Publish the DocumentLibraries item. The live version acquires new ID.
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(Document).FullName);
WorkflowManager.MessageWorkflow(masterDocumentId, typeof(Document), null, "Publish", false, bag);
}
}

Posted by: | Posted on: Jan 25, 2023

1 answers

Replies

5

Hi ,
As per my understanding it is security/unauthorized user performing the action related issue so to fix it you need to make two changes to your code.
1) you need to surround your code with enabling the "SuppressSecurityChecks" to give rights to your librariesManager to perform your librariesManager related operation by using the following lines where first line to enable and second line to disable the "SuppressSecurityChecks" at the starting and ending of the method.

librariesManager.Provider.SuppressSecurityChecks = true;
librariesManager.Provider.SuppressSecurityChecks = false;

2) secondly as you are directly using "WorkflowManager" you need to replace that line with the following line of code to give rights to the "WorkflowManager" to perform the required action

SystemManager.RunWithElevatedPrivilege(d => { WorkflowManager.MessageWorkflow(documentId, typeof(Document), null, "Publish", false, bag); });

So Now your final updated method for the purpose will look like the following one

public static void CreateDocumentNativeAPI(Guid masterDocumentId, string parentDocumentLibraryUrlName, string documentTitle, Stream documentStream, string documentFileName, string documentExtension)
{

LibrariesManager librariesManager = LibrariesManager.GetManager();
librariesManager.Provider.SuppressSecurityChecks = true;
Document document = librariesManager.GetDocuments().Where(d => d.Id == masterDocumentId).FirstOrDefault();
if (document == null)
{
document = librariesManager.CreateDocument(masterDocumentId);
DocumentLibrary documentLibrary = librariesManager.GetDocumentLibraries().Where(d => d.UrlName.ToLower() == parentDocumentLibraryUrlName).SingleOrDefault();
document.Parent = documentLibrary;
document.Title = documentTitle;
document.DateCreated = DateTime.UtcNow;
document.PublicationDate = DateTime.UtcNow;
document.LastModified = DateTime.UtcNow;
document.UrlName = Regex.Replace(documentTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
document.MediaFileUrlName = Regex.Replace(documentFileName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
librariesManager.RecompileAndValidateUrls(document);
librariesManager.Upload(document, documentStream, documentExtension);
librariesManager.SaveChanges();

var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(Document).FullName);
SystemManager.RunWithElevatedPrivilege(d => { WorkflowManager.MessageWorkflow(documentId, typeof(Document), null, "Publish", false, bag); });
}
librariesManager.Provider.SuppressSecurityChecks = false;
}

Replied by: | Replied on: Feb 01, 2023



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview