3

Using the SharePoint Object Model (SP 2010), how can you associate a workflow with a given list?

I've been able to associate a workflow, but the configuration settings do not get saved back to SharePoint. In other words, the basicWorkflowAssociationCreationInformation is saved back to SharePoint, but any further configuration settings using the WorkflowAssociation are not saved.

Here is the code I've been working on:

var context = new ClientContext( url );
Web site = context.Web;

var query = context.LoadQuery( site.WorkflowTemplates.Where( x => x.Name == "My Template Name" ) );
context.ExecuteQuery();
WorkflowTemplate wfTemplate = query.Single();

var wfc = new WorkflowAssociationCreationInformation();
wfc.HistoryList = site.Lists.GetByTitle( "Workflow History" );
wfc.Name = "My Workflow Name";
wfc.TaskList = site.Lists.GetByTitle( "Tasks" );
wfc.Template = wfTemplate;

List list = site.Lists.GetByTitle( "List Name" );

WorkflowAssociation wf = list.WorkflowAssociations.Add( wfc );
wf.AllowManual = false; // is never updated
wf.AutoStartChange = false; // is never updated
wf.AutoStartCreate = true; // is never updated
wf.Enabled = true; // is never updated
string assocData = GetAssociationXml(); // internal method
wf.AssociationData = assocData; // is never updated

context.Load( wf );
context.ExecuteQuery(); // does not update the SP workflow with any of the new wf settings
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
  • Don't you need to list.WorkflowAssociations.Update(wf) after setting your config items? – vinny Feb 15 '11 at 23:15
  • 1
    @vinny, yes indeed. Figured that out an hour after posting and forgot to update the question. Very good catch! Go ahead and add an answer and I'll accept it. – Metro Smurf Feb 16 '11 at 01:41

2 Answers2

1

list.WorkflowAssociations.Update(wf) after setting your config items will update the config items on your WorkflowAssociation.

vinny
  • 1,810
  • 15
  • 21
  • Are you sure that list.WorkflowAssociations.Update() is a method? I only see the update method on the individual association, and it doesn't seem to be working in my case. –  Nov 12 '15 at 17:13
  • Yeah - it's a [method](https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.workflow.spworkflowassociationcollection.update.aspx) of the list [workflowassociationcollection](https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.splist.workflowassociations.aspx). – vinny Nov 18 '15 at 16:56
  • That's not using the client object model so this wouldn't work. It's a bit misleading to have this marked as an answer especially because the closest client-side equivalent of it doesn't seem to work. –  Nov 19 '15 at 18:42
0
    ...
    wf.AssociationData = assocData;
    wf.Update();//Update association config
    list.Update();//Update the list

    context.Load(wf);
    context.ExecuteQuery();