none
SharePoint 2013 Workflow Context in Custom Code Activity

    Question

  • 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!
    Monday, September 23, 2013 11:24 PM

All replies

  • 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!


    Girish

    Tuesday, September 24, 2013 2:23 AM
  • 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?


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    Tuesday, September 24, 2013 7:12 AM
  • 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? 

    Tuesday, September 24, 2013 6:13 PM