0

I want to add approval 2010 workflow to SharePoint 2013 list using client object model.I tried the below approach but it didn't worked. I am using Angularjs and this code is not working in it.

 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();

2 Answers 2

0

Try following...

    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
    wf.Update();
    list.Update();
    context.Load(wf);
    context.ExecuteQuery();
0
Try out this    

static void attachWorkflow(ClientContext context,string listname)
    {

    try
    {
    Web site = context.Web;
    //var query = context.LoadQuery(site.WorkflowTemplates);
    var query = context.LoadQuery(site.WorkflowTemplates.Where(x => x.Name == “DiscussionTemplate”));
    context.ExecuteQuery();
    WorkflowTemplate wfTemplate = query.Single();
    var wfc = new WorkflowAssociationCreationInformation();
    wfc.HistoryList = site.Lists.GetByTitle(“Workflow History”);
    wfc.Name = “DiscussionTemplate1”;
    wfc.TaskList = site.Lists.GetByTitle(“Tasks”);
    wfc.Template = wfTemplate;
    List list = site.Lists.GetByTitle(listname);
    WorkflowAssociation wf = list.WorkflowAssociations.Add(wfc);
    wf.AllowManual = false; // is never updated
    wf.AutoStartChange = true; // is never
    wf.AutoStartCreate = true; // is never updated
    wf.Enabled = true; // is never updated
    //string assocData = GetAssociationXml(); // internal method
    // wf.AssociationData = assocData; // is never updated
    wf.Update();
    context.Load(wf);
    context.ExecuteQuery();
    }
    catch (Exception ex)
    {
    System.IO.File.AppendAllText(Common.strServiceLog, “Error in attaching the Workflow: ” + ex.Message + ” for ” + boardName + ” at time : ” + DateTime.Now + Environment.NewLine);
    }
    }

Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.