SpSite in Nintex Workflow Custom Action

So today I needed to get the URL of the current SharePoint site my Nintex Custom Action Visual Studio solution is executed under, using the SPSite class. Problem was, was not working on the server of the SPSite. Nor would I want to have to install Visual Studio on the production instance where the solution was to live.

Makes sense, just need to grab the httpcontext. SPContext.Current.Site seemed to fit the bill, but alas, it did not work. Lost some time hunting for a solution to the following error:

Could not create activity of type 'CustomActions.MyCustomActivity'. System.IO.FileNotFoundException: The Web application at http://xxx.xxx.com could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl)

There’s a number of possible causes and it took going through some pain in order to resolve which route to take. The standard contingencies are explained quite well on http://www.thorntontechnical.com/tech/sharepoint/new-spsite-filenotfoundexception. None of those, possibly valid error causes, solved my beef.

In the end I found that we don’t actually need to be local with VS on the SharePoint server the solution is compiled against- it was just a matter of finding out how to reference the application context: the Nintex way. In this case, it was being handled by Nintex and the method used was not seemingly explained in the SDK or documented. Looking a bit deeper it became clear the application context was being fed in by Nintex.

Working from the DeployWorkflowActionWithFeature example in the Nintex Workflow 2010 SDK, we can see that in the EventLogAdapter.cs in the project, the context is initially set up in the following function:

public override CompositeActivity AddActivityToWorkflow(PublishContext context)
        {
            EventLogActivity activity = new EventLogActivity();

            Dictionary<string, ActivityParameterHelper> parameters = context.Config.GetParameterHelpers();

            parameters[Parameter_Message].AssignTo(activity, EventLogActivity.MessageProperty, context);
            parameters[Parameter_EventSource].AssignTo(activity, EventLogActivity.EventSourceProperty, context);

            activity.SetBinding(EventLogActivity.__ContextProperty, new ActivityBind(context.ParentWorkflow.Name, StandardWorkflowDataItems.__context));
            activity.SetBinding(EventLogActivity.__ListItemProperty, new ActivityBind(context.ParentWorkflow.Name, StandardWorkflowDataItems.__item));
            activity.SetBinding(EventLogActivity.__ListIdProperty, new ActivityBind(context.ParentWorkflow.Name, StandardWorkflowDataItems.__list));

            ActivityFlags f = new ActivityFlags();
            f.AddLabelsFromConfig(context);
            f.AssignTo(activity);

            context.ParentActivity.Activities.Add(activity);
            
            return null;
        }

That function sets up the __context for use in EventLogActivity.cs as follows:

public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(EventLogActivity));

Now that we have that rolling, we can use the __Context object with SPSite to get our current application URL.
SO, instead of using this:

SPSite site = new SPSite(“http://mysite/sitename”);

Use this:

SPSite site = new SPSite(this.__Context.Site.Url);

You now have the basis to perform all the wonderful SharePoint API stuff you need to do within the context of the Nintex Custom Action.

Tags: , , , ,

Trackback from your site.

Keith Tuomi

Keith is SharePoint Consultant and problem solver with itgroove. He's been focused on Microsoft technologies as a Developer and Web Applications specialist since the beginning of the internet era in the early '90s, with formal training coming from the esteemed British Columbia Institute of Technology. He is a SharePoint MVP, which is a coveted Microsoft award for community contributions and technical excellence. Keith has wide array of international experience and is also adapt at translating your business needs into bits & bytes.

Leave a comment

Recent Comments

  • Keith Tuomi

    Sure, this was the same question we were pondering in our internal corp infrastructure- can we consolidate these servers to maximize efficiency and resource usage? Since the official guidance is that yes, you could install Workflow Manager on a SharePoint server- I see no reason and have no evidence that it would not work for an OWA server as well.

    OWA is basically a pumped-up web application that’s sole purpose is to answer the phone on it’s web service. SharePoint is massively more complex with many more moving parts and dependencies – so logic stands that if you can make WFM work on the same box with SP, getting WFM to work on OWA should be even easier.

    I would suggest you give it a shot, monitor & test carefully, and let us know how it goes!

Follow me on Twitter

Office 365 Service Health

  • Service: Exchange OnlineIncident Type: E-Mail and […]
    Fri, Aug 22, 2014
    Source: Office 365 Service Health Status
  • Service: SharePoint OnlineIncident Type: Tenant Ad […]
    Thu, Aug 21, 2014
    Source: Office 365 Service Health Status
  • Service: SharePoint OnlineIncident Type: Tenant Ad […]
    Thu, Aug 21, 2014
    Source: Office 365 Service Health Status
  • Service: SharePoint OnlineIncident Type: Tenant Ad […]
    Thu, Aug 21, 2014
    Source: Office 365 Service Health Status
  • Service: Exchange OnlineIncident Type: E-Mail time […]
    Thu, Aug 21, 2014
    Source: Office 365 Service Health Status

SharePoint & Office Patches

VisualStudio.com Service Health