Sunday, 3 March 2013

Workflow Activity – Part 2

Now that we have a workflow activity defined, the next step is to add a new project to Visual Studio.
Create SharePoint Project


Next, add a project reference to the activity project.
Activity Project Reference
Now to create the workflow. Add a new item to the project and select a Sequential Workflow.
New Sequential Workflow
Give the workflow a name and set it as a list type.
New List Workflow
Next, select the option for associating with libraries and lists. You can of course opt to not auto associate and this is something I’d generally do as it allows you to control where and what the workflow is associated to when activating the feature, but that’s a different topic altogether.
While the new workflow is opened, hover over the toolbox pane and you should see the custom activity that was created in the first part of this post.
Activity Toolbox
Before adding the activity, add the ApplyActivation activity to the designer.
Apply Activation Activity
To configure the apply activation activity, we first need to create and associate a workflow context property. To do this, select the activity in the designer, bring up the properties pane and edit the __Context property.
Edit Context Property
Switch to the Bind to a new member tab and create a new field.
The difference between a property and a field is that the former creates a dependency property while the latter is a simple .NET property with no such binding.
Do the same for the _WorkflowProperties property, only this time instead of binding to a new member, select the workflowProperties item that already exists.
Bind Workflow Properties
Next, add the custom activity to the workflow, immediately after the ApplyActivation activity.
Custom Activity
Switch to the code behind for the workflow and add the following property.
public SPItemKey ListItemId
{
    get
    {
        return new SPItemKey(workflowProperties.ItemId);
    }
}
This will be used to convert the item id for the workflow to an SPItemKey object.
Head back to the designer window and bring up the properties pane for the custom activity. Here you want to bind __Context to applyActivation1_Context1 (or whatever you called the context field property). You should also bind the __ListId property to the ListId child property of applyActivation1_Context1.
Associate List Id Property
Next, you should now associate the __ItemId property with the ListItemId property, which we created via code behind.
Property Assignments
Lastly, you should enter a valid email address for the EmailTo property.
Specify Email Address
If this were a workflow for a live system this would definitely not be the way to go, you’d want to dynamically set the value here either from the parent workflow or by retrieving the value from one of the workflow item’s fields.
We’re now finished with the workflow. Next, open the feature editor – as this is a new project and we only added one workflow there should only be one feature present. To have SharePoint automatically register this workflow change the receiver assembly to Microsoft.Office.Workflow.Feature, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c and the receiver class to Microsoft.Office.Workflow.Feature.WorkflowFeatureReceiver.
Feature Receiver
Before we deploy the solution, there is one last thing we need to do and that’s ensure the assembly for the workflow activity is deployed along with the SharePoint solution. To do this open the package editor and click on the Advanced link.
Advanced Package
Click the Add button and then Add assembly from Project Output….
From the popup, select the workflow activity project and ensure the deployment target is set to the GAC.
Add Project Assembly
Deploy the solution, activate the feature and bob’s your uncle. You should now be able to associate the workflow with any list or library and receive an email after creating a new item. Ensure that your server has an outgoing email server configured and the email address set in the workflow is valid, otherwise you will get errors.

Task locked by Running Workflow and Cannot be Edited

If you’re trying to update a task item that is linked to a running workflow using code similar to
public UpdateTaskItem(SPList taskList, int identifier, string newValue)
{
  taskItem = taskList.GetItemById(identifier);
  // Update task item
  taskItem["fieldname"] = newValue;
  taskItem.Update();
}
and receive an error stating This task is currently locked by a running workflow and cannot be edited, a simple fix is to update the built in field WorkflowVersion to the value 1 prior to calling the Update method.

public UpdateTaskItem(SPList taskList, int identifier, string newValue)
{
  taskItem = taskList.GetItemById(identifier);
  // Update task item
  taskItem["fieldname"] = newValue;
 
  Guid workflowInstanceId = new Guid(taskItem[Microsoft.SharePoint.SPBuiltInFieldId.WorkflowInstanceID].ToString());
  SPWorkflow workflow = taskItem.Workflows[workflowInstanceId];
  if (workflow != null && !workflow.IsLocked)
  {
    taskItem[Microsoft.SharePoint.SPBuiltInFieldId.WorkflowVersion] = 1;
  }
 
  taskItem.Update();
}
Setting this field to 1 allows the task item to be updated. There’s a check performed by SharePoint to see if this field’s value is set to 1 and when it’s not, the This task is currently locked by a running workflow and cannot be edited error is thrown.

SharePoint 2010: Open PDF file in browser



To open the PDF file inside the browser window do the following steps:
1) 
Go to SharePoint central Admin site. Then under Application Management go to Manage Web Applications.
2)
Select the web application where the PDF file needs to be opened in the browser, and click general settings
3)
Under Browse file handling give permissive

Validate Start Date and End Date in Sharepoint

We often get into validating the end date which should be greater than start date...So follow these steps to perform that...


 1. Go to list settings in that to General Settings and Click Validation Settings



2.Then Add this in Formula =[End Date] >= [Start Date]..Also Type in user Message what error to show .And click save.




























3. Now try adding new item for that list.That is it.














For more doubts on sharepoint you can mail me ...premtd@sharepointbasic.com

Dynamic Update in infopath from Sharepoint List

Step 1)
Create a SP list with two columns Country and State.
Step 2)
Now in infopath create 2 fields Country and State as dropdown fields.
Step 3)
Create a data connection to receive from SP list which we created in step 1. Add the 2 fields from list in data connection wizard.
Step 4)
Select the Country dropdown box in infopath form and point the Country data to the list using the data source which we created in step 3.
Most important select the Check box
“Show only entries with unique display names”
Step 5)
Select the State dropdown box in infopath form and point the state data to the list using the data source which we created in step 3 and filter this data to the Country from list to the Country which we created in infopath form
Step 6)
Cascading update in infopath from SP list

Display SQL table in InfoPath form - Part Two

Continuing from Part One....
11)
Select the fields to display in the infopath and click next and next to finish the wizard.

12)
Go to data → And click Show Fields
13)
In the Fields , Select the database fields which we added now.
Step 14)
After selecting the database fields , Click the repeating table (in our case d.emp) and drag it to the design form
step 15)
After drag and dropping select the Repeating Table

Step 16)
All the database columns are displayed in the repeating table format

step 17)
Select the Name text field and click properties and in properties click on the display tab.
Under display tab make it read only.
Like wise do the same to the remaining filelds.

Step 18)
Goto data connections select our database sql connection and click on convert to connection string.
Then give the Data connection library path . Click OK.

Step 19)
Go to the data connection library and approve the new data connection.

Step 20)
Publish the Infopath form to the site.

Step 21)
Click next and publish

Step 22)
After publish view the details of infopath retrieving details from the database
 

Display SQL table in InfoPath form - Part One

To display SQL tables in read only format inside an browser enabled infopath form , follow the below steps.
The Article is divided into 2 parts of posts. 
1)
Create a database and create table in SQL server. I have create a sample employee table

2)
Entered the sample values to the SQL table

3)
Now backend work is done. We need to design the infopath form. Open the Infopath designer
Go to Data → Data Connections → Add new Connection → Check on receive data and click next

4)
Select the source as database and click next

5)
In the data connection wizard click on Select Database.



6)      In Select data source click on New Source.

7)
In Data Connection Wizard Select the Microsoft SQL server and click next

8)
Mention the database Server details and click next

9)
Select the database name and click then select the table inside database..
Click next

10)
Finish the data connection wizard.
Read the remaining post in Part two....!