SharePoint 2013 Workflow Context in Custom Code Activity

I'm writing a custom code activity for workflows on the SharePoint 2013 platform and was hoping someone could show me how to access the workflow context in C# to get the current site URL. I know in SP 2010 custom activities you could pass in the __context parameter and then access that in your inherited Activity class, but is this possible in SP 2013? I can see that the execute method has an argument of the CodeActivityContext class but I don't see how to get workflow context specific properties from that variable.

See example code below and comment about getting the current site URL

// Example Code
public sealed class TestActivity : CodeActivity<System.Guid>
    {
        // Define Input Arguments
        public InArgument<System.Guid> TestListId { get; set; }
        public InArgument<System.Guid> TestListItemId { get; set; }

        // Define Output Arguments
        public OutArgument<System.Guid> ListItemId { get; set; }

        // Overidden Excute Method of CodeActivity Class
        protected override System.Guid Execute(CodeActivityContext context)
        {
            using (SPSite Site = new SPSite("http:/host/site")) //This is where I need workflow context to get current site URL
            {
                using (SPWeb Web = Site.OpenWeb())
                {
                    SPDocumentLibrary TestLibrary = Web.Lists[context.GetValue(this.TestListId)] as SPDocumentLibrary;
                    SPFile TestDocument = Web.GetFile(context.GetValue(this.TestListItemId));
                    context.SetValue(this.ListItemId, TestDocument.UniqueId);
                }
            }
            return context.GetValue(this.ListItemId);
        }
    }
Any help is much appreciated!
September 23rd, 2013 11:24pm

I'm writing a custom code activity for workflows on the SharePoint 2013 platform and was hoping someone could show me how to access the workflow context in C# to get the current site URL. I know in SP 2010 custom activities you could pass in the __context parameter and then access that in your inherited Activity class, but is this possible in SP 2013? I can see that the execute method has an argument of the CodeActivityContext class but I don't see how to get workflow context specific properties from that variable.

See example code below and comment about getting the current site URL

// Example Code
public sealed class TestActivity : CodeActivity<System.Guid>
    {
        // Define Input Arguments
        public InArgument<System.Guid> TestListId { get; set; }
        public InArgument<System.Guid> TestListItemId { get; set; }

        // Define Output Arguments
        public OutArgument<System.Guid> ListItemId { get; set; }

        // Overidden Excute Method of CodeActivity Class
        protected override System.Guid Execute(CodeActivityContext context)
        {
            using (SPSite Site = new SPSite("http:/host/site")) //This is where I need workflow context to get current site URL
            {
                using (SPWeb Web = Site.OpenWeb())
                {
                    SPDocumentLibrary TestLibrary = Web.Lists[context.GetValue(this.TestListId)] as SPDocumentLibrary;
                    SPFile TestDocument = Web.GetFile(context.GetValue(this.TestListItemId));
                    context.SetValue(this.ListItemId, TestDocument.UniqueId);
                }
            }
            return context.GetValue(this.ListItemId);
        }
    }
Any help is much appreciated!

Free Windows Admin Tool Kit Click here and download it now
September 24th, 2013 2:23am

First of all, you are trying to use SharePoint Object Model inside workflow activity - which puts a constraint for the workflow: the workflow must have to be executed in SharePoint server. If for any reason, you want to scale the workflow server out to it's own server (or non-SharePoint server), the code will fail to execute. So my suggestion would be think twice before referring SharePoint object model. Workflow Manager itself talks to SharePoint over REST to decouple SharePoint dependency. Alternative to this could be client object model or REST API but I would go for for custom WCF service hosted inside SharePoint invoked by WF. Once such example you can find in my blog: http://ranaictiu-technicalblog.blogspot.com.au/2013/04/sharepoint-2013-workflow-use-httpsend.html.

Anyway, back to your questions, I'm not quite sure if you can access the site url from workflow, but maybe you can pass the site url as Input parameter?

September 24th, 2013 7:12am

Thanks for the reply Sohel and the link to your blog, I have found a lot of great articles on there.

My development environment is all roles installed on one VM so that must be why it works if I hard code the URL but my production environment has a separate workflow server. If I use REST then I'll still need to know the site URL so that I can build URI's that will reach the correct REST endpoints. I guess I'll just pass it in as an input parameter like you suggested for now but there has to be a better way to get the workflow context over to the workflow server.

I noticed that when developing declarative activities there is an activity call LookupWorkflowContextProperty or GetHistoryListId that uses GetConfigurationValue or ExternalVariableValue to call and get workflow context values. Theses appear to be in the namespace Microsoft.Activities which should be in the workflow servers GAC.

Does anyone know if you can use context.GetExtension<ConfigurationExtension>() or GetConfigurationValue to get workflow context values from a SharePoint 2013 workflow custom code activity? 

Free Windows Admin Tool Kit Click here and download it now
September 24th, 2013 6:13pm

In SharePoint 2013 workflows you can get the context to the workflow by using LookupWorkflowContextProperty class. here is the code you can use;

        protected override void Execute(CodeActivityContext executionContext)
        {
            
            try
            {
                Variable<string> CurrentWebUrl = new Variable<string>("CurrentWebUrl");
                LookupWorkflowContextProperty lookupWorkflowContextProperty = new LookupWorkflowContextProperty()
                {
                    PropertyName = "CurrentWebUrl",
                    Result = CurrentWebUrl
                };

                string url = CurrentWebUrl.Get(executionContext);
              }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
            }
        }

Hope this helps.

  • Proposed as answer by Bhakthi Liyanage Wednesday, November 12, 2014 4:23 PM
  • Marked as answer by Tyler Cook Wednesday, November 12, 2014 4:36 PM
November 12th, 2014 4:22pm

I am receiving "Variable 'CurrentWebUrl' of type 'System.String' cannot be used. Please make sure it is declared in an Activity or SymbolResolver." on this line
string url = CurrentWebUrl.Get(executionContext);
Am I missing something?
Free Windows Admin Tool Kit Click here and download it now
May 13th, 2015 6:15pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics