Friday, March 29, 2013

Associate workflow to Content Type in a feature


Add new Content Type

Open visual studio 2010 | New Project | Sharepoint | 2010 | Choose Content Type then type name is ActiveWorkflowWhenAddContentType


Deploy as a farm solution


Choose content type Document Set (option) from dropdownlist


You see the structure as follows then you can change name of Content Type


Delete name of content type then type other name is DocumentSetContentType

Copy this segment code and paste to here as follows:

<Field ID="{A57B3F82-A8AE-4967-9041-F197560D8FB6}"
Type="Text"
Required="TRUE"
DisplayName="Field1"
Name="Field1"
Indexed="TRUE"
EnforceUniqueValues="TRUE" />

Add new Workflow Sequential

Right click to project | Add | New Item…
Choose Sequential Workflow and type name is WorkflowForContentType


Type name for workflow is WorkflowForContentType


Choose library or list, history list, task list as follows:


Set default then click Finish

Appear Feature2 when Workflow is added
Right click to Feature2 | Add Event Receiver

UI code as follows:
Declare using Microsoft.SharePoint.Workflow;

using Microsoft.SharePoint.Workflow;
////////////////////////////////////////////
Copy this segment code then paste above method FeatureActivated
private string WorkflowName = "WorkflowForContentType";
private string ContentTypeName = "DocumentSetContentType";

Uncommnet method FeatureActivated and paste this segment code to within it

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPSite site = new SPSite("http://quochung-axioo:90/"))//Change your web app
{
using (SPWeb web = site.OpenWeb())
{
SPContentType theCT = web.ContentTypes[ContentTypeName];
SPWorkflowTemplate theWF = null;
foreach (SPWorkflowTemplate tpl in web.WorkflowTemplates)
{
if (tpl.Name == WorkflowName)
{
theWF = tpl;
}
}
SPWorkflowAssociation wfAssociation = theCT.WorkflowAssociations.GetAssociationByName(WorkflowName, web.Locale);
if (wfAssociation != null)
{
theCT.WorkflowAssociations.Remove(wfAssociation);
}
theCT.UpdateWorkflowAssociationsOnChildren(true, true, true, false);

wfAssociation = wfAssociation = SPWorkflowAssociation.CreateWebContentTypeAssociation(theWF, WorkflowName, "Tasks", "Workflow History");

if (theCT.WorkflowAssociations.GetAssociationByName(wfAssociation.Name, web.Locale) == null)
{
theCT.WorkflowAssociations.Add(wfAssociation);
}
else
{
theCT.WorkflowAssociations.Update(wfAssociation);
}
theCT.UpdateWorkflowAssociationsOnChildren(true, true, true, false);
}
}
}
///////////////////
Uncommnet method FeatureDeactivating and paste this segment code to within it

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using (SPSite site = new SPSite("http://quochung-axioo:90/"))//Change your web app
{
using (SPWeb web = site.OpenWeb())
{
SPContentType theCT = web.ContentTypes[ContentTypeName];

SPWorkflowTemplate theWF = null;
foreach (SPWorkflowTemplate tpl in web.WorkflowTemplates)
{
if (tpl.Name == WorkflowName)
{
theWF = tpl;
}
}

SPWorkflowAssociation wfAssociation = theCT.WorkflowAssociations.GetAssociationByName(WorkflowName, web.Locale);
if (wfAssociation != null)
{
theCT.WorkflowAssociations.Remove(wfAssociation);
}
theCT.UpdateWorkflowAssociationsOnChildren(true, true, true, false);
}
}
}

//////////////////////////////////////////////////////// All Code ////////////////////////////////////////////////////////
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Workflow;

namespace ActiveWorkflowWhenAddContentType.Features.Feature2
{
/// <summary>
/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should not be modified.
/// </remarks>

[Guid("c670481c-5032-4200-9096-385bcdaba2f9")]
public class Feature2EventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.

private string WorkflowName = "WorkflowForContentType";
private string ContentTypeName = "DocumentSetContentType";

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPSite site = new SPSite("http://quochung-axioo:90/"))
{
using (SPWeb web = site.OpenWeb())
{
SPContentType theCT = web.ContentTypes[ContentTypeName];
SPWorkflowTemplate theWF = null;
foreach (SPWorkflowTemplate tpl in web.WorkflowTemplates)
{
if (tpl.Name == WorkflowName)
{
theWF = tpl;
}
}
SPWorkflowAssociation wfAssociation = theCT.WorkflowAssociations.GetAssociationByName(WorkflowName, web.Locale);
if (wfAssociation != null)
{
theCT.WorkflowAssociations.Remove(wfAssociation);
}
theCT.UpdateWorkflowAssociationsOnChildren(true, true, true, false);

wfAssociation = SPWorkflowAssociation.CreateWebContentTypeAssociation(theWF, WorkflowName, "Tasks", "Workflow History");

if (theCT.WorkflowAssociations.GetAssociationByName(wfAssociation.Name, web.Locale) == null)
{
theCT.WorkflowAssociations.Add(wfAssociation);
}
else
{
theCT.WorkflowAssociations.Update(wfAssociation);
}
theCT.UpdateWorkflowAssociationsOnChildren(true, true, true, false);
}
}
}


// Uncomment the method below to handle the event raised before a feature is deactivated.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using (SPSite site = new SPSite("http://quochung-axioo:90/"))
{
using (SPWeb web = site.OpenWeb())
{
SPContentType theCT = web.ContentTypes[ContentTypeName];

SPWorkflowTemplate theWF = null;
foreach (SPWorkflowTemplate tpl in web.WorkflowTemplates)
{
if (tpl.Name == WorkflowName)
{
theWF = tpl;
}
}

SPWorkflowAssociation wfAssociation = theCT.WorkflowAssociations.GetAssociationByName(WorkflowName, web.Locale);
if (wfAssociation != null)
{
theCT.WorkflowAssociations.Remove(wfAssociation);
}
theCT.UpdateWorkflowAssociationsOnChildren(true, true, true, false);
}
}
}


// Uncomment the method below to handle the event raised after a feature has been installed.

//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
//{
//}


// Uncomment the method below to handle the event raised before a feature is uninstalled.

//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
//{
//}

// Uncomment the method below to handle the event raised when a feature is upgrading.

//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
//{
//}
}
}


///////////////////////////////////////////////////// End All Code ///////////////////////////////////////////////////

Build Project

Go to Shared Documents | List Settings | Advanced settings
Allow management of content types? : choose yes | OK
Continue, click “Add from existing site content types” link as follows:

Choose DocumentSetContentType then click Add button

Result as

New DocumentSetContentType

Input full infomation

Automatically workflow run as follows:

No comments:

Post a Comment