orchardcmsorchardcms-1.10

Get a content edit URL as a string without UrlHelpers or Orchard tokens


I'm trying to get the edit URL of a content as a string from backend, the catch is I'm inside a workflow activity, so I can't use Url.Action... or Url.ItemEditLink... or other UrlHelpers as if it were a controller or a view. Also, although I'm inside a workflow, the contents I need it for are not part of the workflowContext or the activityContext, so I can't use those or tokens either.

A solution could be to get the content metadata and the site baseUrl and try to build it manually, but I think this way is prone to errors.

Thanks.


Solution

  • This is how I build a Uri in an activity:

    public class MyClass : Task
    {
        private readonly RequestContext _requestContext;
        ...
    
        public MyActivity(RequestContext requestContext, ...)
        {
            _requestContext = requestContext;
            ...
        }
    
        ...
    
        public override IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext)
        {
            var content = ... get using ID
            var helper = new UrlHelper(_requestContext);
            var baseurl = new Uri(_orchardServices.WorkContext.CurrentSite.BaseUrl);
            Uri completeurl = new Uri(baseurl, helper.ItemDisplayUrl(content));
    
            yield return T("Done");
        }
    }
    

    Turns out that I actually do build the Uri semi-manually, but I haven't had issues with this method. You may be able to use just the ItemDisplayUrl for navigation inside of Orchard; I had to get the full URL because the string gets sent to an outside program (Slack).