Showing posts with label Sharepoint Development. Show all posts
Showing posts with label Sharepoint Development. Show all posts

Tuesday, September 18, 2012

Sharepoint Custom Workflow Activity

Dear Friends,

           This blog is regarding creating "Sharepoint Custom Workflow Activity" through visual studio 2010. First, lets us have look on what is Custom Workflow Activity, what is need to create custom workflow activity?

          When creating workflow, Sharepoint designer supports out-of-box actions which can be easily used. Examples are Sending email, Creating item in List of same site, Log to history list, etc. Below are set of actions provided in Sharepoint designer:

Workflow Actions Out-of-box
          SharePoint Designer supports only a subset of the workflow activities available from the workflow foundation. The two most notable workflow activities that SharePoint does not support are the WhileActivity, which is responsible for looping, and the ReplicatorActivity, which is responsible for iterating through a collection. Other activities, though supported, have limited functionality. For instance, the LogToHistoryActivity, which is used to log messages to the Workflow History list, cannot write to the
Other Data field; therefore, messages from a SharePoint Designer–created workflow sent to the Workflow History list are capped at 256 characters. 

           If the limitations of SharePoint Designer are too restrictive, Visual Studio is the answer. With Visual Studio, you have complete access to the capabilities of the SharePoint Workflow host and workflow runtime. The designer in Visual Studio allows for looping and iteration—the two key reasons why most solutions require a Visual Studio workflow. 

           We need to go for creating Sharepoint Custom Workflow activity when Sharepoint designer does not support out-of-box inbuilt actions for workflow. We had requirement to add/create new item in sub site's list when workflow is triggered. Sharepoint designer workflow actions support adding item in same site list but when it comes to adding item in sub site list it doesn't support, so we need to create Sharepoint Custom Workflow activity. 

   Let's see how can we create Custom Workflow Activity:

  1.    Create a new project, select “Empty SharePoint Project” as the template.
  2. Creating Empty Sharepoint Project

    2. Give the project a cool name and click “OK.”
    3.  Select “Deploy as farm solution” in the wizard window.

    Sharepoint Deployment as Farm Solution

    4.   Add a reference to “Microsoft.SharePoint.WorkflowActions” from the .NET tab in the Add Reference dialog. 
     Add a reference to “System.Workflow.Activities”, “System.Workflow.ComponentModel” and “System.Workflow.Runtime” under “Browse” in the folder “Program Files (x86) Reference AssembliesMicrosoftFrameworkv3.0“

    Sharepoint Adding Reference

Once the project is configured, we will create the activity code as shown below:

Creating the Activity Code

  1. You now have a SharePoint solution with the proper references set so that you can write an Activity. It is time to add the class and the first bit of code.
  2. Add a new empty Class file to the project.                                                               In my case I given Class Name is “CustomSubsSiteAction” and that class Inherit with “Activity” Class.
Sharepoint Creating Custom Activity

   In order to allow properties to be passed in from SharePoint designer, you have to set up Dependency Properties.

2. Add a Property and Dependency Property for values that will come in from the Workflow. Below is code: 

public static DependencyProperty DepartMentsProperty = DependencyProperty.Register("DepartMents", typeof(string), typeof(CustomSubsSiteAction));
        [Category("Cross-Site Actions"), Browsable(true)]
        public string DepartMents
                return Convert.ToString(base.GetValue(DepartMentsProperty));
                base.SetValue(DepartMentsProperty, value);

When your activity starts, the Execute method is fired. This is where you will do the work in the Activity. So when can override Execute Method and Perform ours Operation.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
             // do stuff here.
            return ActivityExecutionStatus.Closed;

But If we want to Access Current Item Like: List ID, List Item, Current Web than add Dependency Property for that. Access current site Context then Register Property as shown in code below:

public static DependencyProperty __ContextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(CustomSubsSiteAction));

        [Description("The site context")]
        public WorkflowContext __Context
                return ((WorkflowContext) (base.GetValue(CrossSubSiteAction.__ContextProperty)));
                base.SetValue(CrossSubSiteAction.__ContextProperty, value);

  Same as Current List Item’s List ID:

public static DependencyProperty __ListIdProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ListId", typeof(string), typeof(CustomSubsSiteAction));

        public string __ListId
                return ((string)(base.GetValue(CrossSubSiteAction.__ListIdProperty)));
                base.SetValue(CrossSubSiteAction.__ListIdProperty, value);

For Current ListItem’s ID:

public static DependencyProperty __ListItemProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ItemID", typeof(int), typeof(CustomSubsSiteAction));

        public int __ ItemID
                return ((int)(base.GetValue(CrossSubSiteAction.__ListItemProperty)));
                base.SetValue(CrossSubSiteAction.__ListItemProperty, value);

Also,Register Workflow Activationproperties for Action.

public static DependencyProperty __ActivationPropertiesProperty = DependencyProperty.Register("__ActivationProperties", typeof(Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties), typeof(CustomSubsSiteAction));


        public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties __ActivationProperties




                return (Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties)base.GetValue(CrossSubSiteAction.__ActivationPropertiesProperty);




                base.SetValue(CrossSubSiteAction.__ActivationPropertiesProperty, value);



Override Execute Method & have custom action code as shown below:

Sharepoint - Override Execute method of Action

 In order for SharePoint designer to know about the Activity, we have to add a special XML file to the Workflow folder in the SharePoint hive.

    1. Right-click on the Project and select Add -> SharePoint Mapped Folder…”

Adding Action to Sharepoint
                   From the folder list, select “TEMPLATE/1033/Workflow“

Sharepoint Mapped Folder

In the Solution Explorer, right-click on the new Workflow folder and add an XML file (Add -> New Item -> DataXML File). And File name and file extension Must be “.actions”


Custom Actions XML
 Open CrossSiteAction.actions File and Add RuleDesigner,Parameter

Action Name :  Name="Access Cross Subs-Sites List"

Action showing In category  :  Category="Cross Sub-Site List"

In my case .actions file is

xml version="1.0" encoding="utf-8" ?>



    <Action Name="Access Cross Subs-Sites List"


    Assembly="CustomSubsSiteAction, Version=, Culture=neutral, PublicKeyToken=7341b1706fa0e582"

   AppliesTo="all" Category="Cross Sub-Site List">

      <RuleDesigner Sentence="Add Current-Listitem to DepartMentName = %1 Task Listitem">       

        <FieldBind Field="DepartMents" DesignerType="TextArea" Id="1" />    



        <Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext" Direction="In" />

        <Parameter Name="__ListId" Type="System.String, mscorlib" Direction="In" />

        <Parameter Name="__ItemID" Type="System.Int32, mscorlib" Direction="In" />

 <Parameter Name="DepartMents" Type="System.String, mscorlib"  Direction="In" /> 
<Parameter Name="__ActivationPropertiesType="Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties, Microsoft.SharePoint" Direction="Out" />

Deploying & Register your Namespace for Authorization 

Deploy the solution & to register Custom Activites to Webapplication's Web.Config File

between tab

Web config

Than Restart IIS.

Open run and just write IISreset and Ok.

Now Open Sharepoint Designer 2010 & Go to workflow

Sharepoint Custom workflow Action

Please let me know if anyone has any queries.

Thanks & Regards, 

Paras Sanghani

Source Code

Friday, September 7, 2012

Sharepoint 2010 Platform

Dear Friends,

                We will start today with Sharepoint 2010 Application Development and will have series of blogs on the same considering Sharepoint for the beginners. This will be good starting point for what is Sharepoint and how to begin with Sharepoint Development. 

Before we going further on the SharePoint 2010 platform, let have look on some key concepts and architectural details. For example, if we understand what SharePoint 2010 provides out of the box, and the types of solutions that are typically developed on that platform, we can focus your development efforts and not waste time reinventing features that already exist. Instead, you can focus on extending or customizing SharePoint to meet your solution requirements. Additionally, if you understand the technology stack upon which SharePoint 2010 is built, you can use your existing .NET development skills appropriately, and can apply your previously gained knowledge and experience to SharePoint development projects. You can also understand how your solution fits into the SharePoint ecosystem.

Below are some of the Advantages of Sharepoint 2010 as Application development:
  1. Easy Data Management
  2. Document Management
  3. Web content Management
  4. Page and Data Rendering
  5. Collaborative Framework
  6. Search, Fast Search & Enterprise search
  7. Metadata Management
  8. Business Process Framework
  9. Social Framework
Most enterprise solutions are typically designed by specifying functional requirements and how they will be met. For example, storing data is a functional requirement, as is ensuring data can be searched. When you build your solutions on the SharePoint 2010 platform, you automatically benefit from some of the functional requirements that are already met. For instance, if your solution has a requirement for ensuring that users check out and check in documents in a collaborative environment, you would have to design
storage and source control mechanisms if you developed your own solution. However, by basing your solution on SharePoint, you can take advantage of the fact that the platform already meets these requirements. The slide shows many examples of functional requirements that are already met by SharePoint 2010.

 Common types of Sharepoint Development Projects:

 Below diagram represents Sharepoint 2010 Technology Stack:

                                         Sharepoint 2010 Technology Stack

    Market research by Microsoft has shown that most development projects for SharePoint can be categorized into one of four major types.

Single Web Part Solutions
These types of solutions can range from something as simple as rendering data from a SharePoint list in an innovative way, to complex solutions such as learning management systems and courseware authoring toolsets.

Collaborative Business Solutions
These types of solutions typically include multiple features of SharePoint, such as lists, workflows, forms, and event receivers that work together to implement a collaborative process. For example, you could build a recruitment solution or a product launch application in this way.

Portals for Line-of-Business Data
Many organizations realize that employees use SharePoint on a daily basis for features such as document storage, collaboration, blogs, wikis, and search. It is a natural progression to want information workers to access, add, update, and delete data in external line-of-business applications directly from SharePoint sites. This is especially important where that external data is used in collaborative business processes within SharePoint. SharePoint 2010 provides powerful capabilities that enable you to implement full read-write access to data that is stored and managed by external applications. For example, you can use SharePoint Designer 2010 to set up full read-write access to external data with a minimum number of clicks. Information workers can then work with that data in a way very similar to how they work with native SharePoint list data. You can also use Visual Studio 2010 to implement scenarios where an external data source has complex rules, and again the result is that information workers can interact with that data as if it were in SharePoint lists.

Customizations to SharePoint Workloads
As a developer, you can customize SharePoint workloads to meet specific business or solution requirements. Examples include creating workflows to implement a specific business process; adding event receivers to lists so that complex data validation can be performed; or even customizing the look and- feel of intranet or Internet sites to reflect corporate branding and styles.

We will start new post following this series on Sharepoint Object Hierarchy on getting started with Sharepoint 2010 development.

For when to use Asp.Net and Sharepoint. Please visit:
When to use sharepoint and when

Next in the series is Sharepoint object Hierarchy.
Please let me know in case of queries, comments.............

Thanks & Regards,
Paras Sanghani