Home > Sharepoint Exam > Document Library Workflow

Document Library Workflow

December 31, 2011 5:52 pm Leave a comment Go to comments

In general there are four start options for SharePoint workflows:

  • Start manual
  • Start when an item changed
  • Start when a new item is created
  • Start workflow to approve publishing a major version of an item

image

When you deploy a workflow in site definition and associate the workflow to a document library programmatically the option Start workflow to approve publishing a major version of an item is not enabled and you cannot select the same. If you use the following code you can see the issue

SPWorkflowTemplate template = web.WorkflowTemplates.GetTemplateByBaseID(new Guid("AB81DB45-4CDD-4650-80CD-6EFC53D2D9EC"));
  template.AllowDefaultContentApproval = true;
              SPWorkflowAssociation workflow =  SPWorkflowAssociation.CreateListAssociation(template,       
                                                  "Document Approval",
                                                  taskList,
                                                  historyList);
              dlist.Update();

How can you associate the Workflow correctly?

SPList dlist = web.Lists["ListName"]; //Gets the doc library to attach the workflow

           SPDocumentLibrary docslib = (SPDocumentLibrary)(dlist);
           SPList historyList;
           // Try to get workflow history list
           try
           {
               historyList = web.Lists["Workflow History"];
           }
           catch (ArgumentException exc)
           {
               // Create workflow history list
               Guid listGuid = web.Lists.Add("Workflow History", "", SPListTemplateType.WorkflowHistory);
               historyList = web.Lists[listGuid];
               historyList.Hidden = true;
               historyList.Update();
           }

           SPList taskList;
           // Try to get workflow tasks list
           try
           {
               taskList = web.Lists["Workflow Tasks"];
           }
           catch (ArgumentException exc)
           {
               // Create workflow tasks list
               Guid listGuid = web.Lists.Add("Workflow Tasks", "", SPListTemplateType.Tasks);
               taskList = web.Lists[listGuid];
               taskList.Hidden = true;
               taskList.Update();
           }
           try{

               SPWorkflowTemplate template = web.WorkflowTemplates.GetTemplateByBaseID(new Guid("AB81DB45-4CDD-4650-80CD-6EFC53D2D9EC"));
               template.AllowDefaultContentApproval = true;
               SPWorkflowAssociation workflow =  SPWorkflowAssociation.CreateListAssociation(template,       
                                                   "Document Approval",
                                                   taskList,
                                                   historyList);
               docslib.WorkflowAssociations.Add(workflow);
               docslib.DefaultContentApprovalWorkflowId = workflow.Id; // set property
               dlist.Update();
               web.AllowUnsafeUpdates = false;
           }
           finally
           {
           }

Advertisements
Categories: Sharepoint Exam Tags:
  1. April 8, 2013 6:16 am at 6:16 am

    Hi Chandra ,Its here any option to uncheck the “Start workflow to approve publishing a major version of an item” option for the existing publishing approval workflow ? By default it is in check mode, i need to uncheck it programatically upon a feature activation.

    any idea ?

    Thanks in advance..!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: