Bagus Tesa
Bagus Tesa

Reputation: 1695

Proper Way to Publish Sitefinity MediaContent

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.

Upvotes: 0

Views: 305

Answers (1)

Veselin Vasilev
Veselin Vasilev

Reputation: 3793

"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);
});

Upvotes: 1

Related Questions