Thursday, October 29, 2009

Custom activity with full code for creating folder in list/Library with action file code

Code:
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;



namespace CreateFolder
{
public partial class Activity1 : Activity
{

public static DependencyProperty FolderNameProperty
= DependencyProperty.Register(
"FolderName", typeof(string), typeof(Activity1));

[DescriptionAttribute("FolderName")]
[CategoryAttribute("FolderName Category")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public string FolderName
{
get
{
return ((string)(base.GetValue(Activity1.FolderNameProperty)));
}
set
{
base.SetValue(Activity1.FolderNameProperty, value);
}
}

public static DependencyProperty BaseUrlProperty
= DependencyProperty.Register("BaseUrl", typeof(string), typeof(Activity1));

[DescriptionAttribute("BaseUrl")]
[CategoryAttribute("BaseUrl Category")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public string BaseUrl
{
get
{
return ((string)(base.GetValue(Activity1.BaseUrlProperty)));
}
set
{
base.SetValue(Activity1.BaseUrlProperty, value);
}
}

public static DependencyProperty ListNameProperty
= DependencyProperty.Register("ListName", typeof(string), typeof(Activity1));

[DescriptionAttribute("ListName")]
[CategoryAttribute("ListName Category")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public string ListName
{
get
{
return ((string)(base.GetValue(Activity1.ListNameProperty)));
}
set
{
base.SetValue(Activity1.ListNameProperty, value);
}
}


public static DependencyProperty InvokeEvent
= DependencyProperty.Register("Invoke", typeof(EventHandler), typeof(Activity1));

[DescriptionAttribute("Invoke")]
[CategoryAttribute("Invoke Category")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public event EventHandler Invoke
{
add
{
base.AddHandler(Activity1.InvokeEvent, value);
}
remove
{
base.RemoveHandler(Activity1.InvokeEvent, value);
}
}

public Activity1()
{
InitializeComponent();
}

protected override ActivityExecutionStatus
Execute(ActivityExecutionContext executionContext)
{
// Raise Invoke Event to execute custom code in the workflow.
this.RaiseEvent(Activity1.InvokeEvent,
this, EventArgs.Empty);

string strIndrajeet = FolderName;
string strBaseUrl = BaseUrl;
string strDocLibName = ListName;

SPSite spsite = new SPSite(strBaseUrl);

spsite = new SPSite(strBaseUrl, spsite.SystemAccount.UserToken);
using (SPWeb spweb = spsite.OpenWeb())
{
spsite.AllowUnsafeUpdates = true;
spweb.AllowUnsafeUpdates = true;

SPFolderCollection folders = spweb.GetFolder(strBaseUrl + "/" + strDocLibName).SubFolders;

bool boolFolderNotCreated = true;

if (!spweb.GetFolder(strBaseUrl + "/" + strDocLibName + "/" + strIndrajeet).Exists)
{
folders.Add(strIndrajeet);
boolFolderNotCreated = false;
spweb.Update();
}

int count = 1;
while (boolFolderNotCreated)
{
string folderName = strIndrajeet + "_" + Convert.ToString(count);
SPFolder spfTemp = spweb.GetFolder(strBaseUrl + "/" + strDocLibName + "/" + folderName);
if (!spfTemp.Exists)
{
folders.Add(folderName);
boolFolderNotCreated = false;
spweb.Update();
}
else
{
count++;
boolFolderNotCreated = true;
}
}
}

// Indicate that the activity has completed.
return ActivityExecutionStatus.Closed;
}
}
}

<strong>.Aciton file entry (under C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\1033\Workflow)</strong>
<blockquote></blockquote>""""


<workflowinfo>
<actions parallel="and" sequential="then">
<action category="List Actions" appliesto="all" assembly="CreateFolder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=effbcf4c7d0dab8e" classname="CreateFolder.Activity1" name="Create Folder">
<ruledesigner sentence="Create Folder in List %2 with folder name %1 under site %3">
<fieldbind id="1" designertype="TextArea" field="FolderName">
<fieldbind id="2" designertype="TextArea" field="ListName">
<fieldbind id="3" designertype="TextArea" field="BaseUrl">
</ruledesigner>
<parameters>
<parameter name="FolderName" direction="In" type="System.String, mscorlib">
<parameter name="ListName" direction="In" type="System.String, mscorlib">
<parameter name="BaseUrl" direction="In" type="System.String, mscorlib">
</parameters>
</action>
</actions>
</workflowinfo>

<strong>Web.Config Entry</strong>
<authorizedtype assembly="CreateFolder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=effbcf4c7d0dab8e" authorized="True" typename="*" namespace="">

No comments: