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
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: Khalid Abbas | Posted on: Jan 25, 2023
5
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.
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
So Now your final updated method for the purpose will look like the following one
Replied by: Tabish Usman | Replied on: Feb 01, 2023