Proper Way to Publish Sitefinity MediaContent
Asked Answered
G

1

0

I want to publish Sitefinity MediaContent (Document, Image, and Video in Sitefinity Library). As i look on the documentation, they gave an example using WorkflowManager:

//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);

Code snippet above is taken from their documentation.

The problem is, the underlying implementation of WorkflowManager uses HttpContext to check whether the current user has the required privilege. The snippet below is from decompiling the Telerik.Sitefinity.dll:

    public static string MessageWorkflow(System.Guid itemId, System.Type itemType, string providerName, string operationName, bool isCheckedOut, System.Collections.Generic.Dictionary<string, string> contextBag)
    {
        ....
        dictionary.Add("operationName", operationName);
        dictionary.Add("itemId", itemId);
        dictionary.Add("providerName", providerName);
        dictionary.Add("isCheckedOut", isCheckedOut);
        dictionary.Add("contextBag", contextBag);
        dictionary.Add("culture", System.Globalization.CultureInfo.CurrentCulture.Name);
        dictionary.Add("httpContext", System.Web.HttpContext.Current);
        if (workflowDefinition != null)
        {
            dictionary.Add("workflowDefinitionId", workflowDefinition.Id);
        }
        contextBag.Add("userHostAddress", System.Web.HttpContext.Current.Request.UserHostAddress);
        ...
    }

As shown above it at least calls System.Web.HttpContext.Current twice which is bad. Especially if i have exection deferred using HostingEnvironment.QueueBackgroundWorkItem or even using Quartz that runs outside HttpContext.Current obviously.

My question is, is there another way to publish Sitefinity MediaContent in Elevated Mode and did not depend on HttpContext at all?

Currently i am using Sitefinity 9.2, as far as i am aware, the APIs above also exists on Sitefinity 7.3.

Gleeson answered 18/2, 2018 at 2:59 Comment(0)
B
1

"As shown above it at least calls System.Web.HttpContext.Current twice which is bad." - this is most probably an issue with the decompiler - I am sure they are not calling it twice.

For Elevated Mode you can use this, but it will still require HttpContext

...
SystemManager.RunWithElevatedPrivilege(p =>
{
    WorkflowManager.MessageWorkflow(id, itemType, null, "Publish", false, bag);
});
Bullshit answered 19/2, 2018 at 3:36 Comment(3)
is this SystemManager.RunWithElevatedPrivilege uses the previous HttpContext? i just googled it a bit and found this not sure how it will behave under QueueBackgroundWorkItem or Quartz.. it also plagued with this issue apparently, and reflection (accessing the website url from the application itself) is not an option..Gleeson
It uses SystemManager.CurrentHttpContext which I think you can try to set yourself (I've never done this though). Can't you use the Sitefinity's ScheduledTask for any background tasks?Bullshit
unfortunately, no. i can't use it as the current framework already using Quartz everywhere..Gleeson

© 2022 - 2024 — McMap. All rights reserved.