Saturday, August 6, 2011

How To Update Workflow Task Status

Sometimes in sharepoint, you might have faced an issue, that workflow being completed, but the task status is still shows "In Progress".

In that scenario, a simple workaround would be updating the task programmatically for that particular item.

Here you go,
Make sure you update the task for the required item. the below sample updates the task for item id=3
If the item has more than one task, make sure you update the task status to the appropriate task.


       string SiteCollectionUrl = "http://SiteURL/";

            using (SPSite site = new SPSite(SiteCollectionUrl))
            {
                using (SPWeb oWeb  = site.OpenWeb())
                {
                    SPList oList = oWeb.Lists["LISTNAME"];
                    SPListItem item = oList.Items.GetItemById(3);
                    SPWorkflowTaskCollection taskCol = item.Tasks;
                    SPWorkflowTask taskedit = item.Tasks[0];

                    if (taskedit == null)
                        return;
                    Hashtable ht = new Hashtable(); 
                    ht["TaskStatus"] = "#";    // To make the task "approved"
                    SPWorkflowTask.AlterTask((taskedit as SPListItem), ht, true);
                   
                }
            }





ht["TaskStatus"] = "#";   // To Approve the the task use "#"
ht["TaskStatus"] = "@";   // To Reject the the task use "@"



Happy Coding :)

Forms Based Authentication in Sharepoint

How to configure FBA in MOSS 2007

To configure FBA in MOSS 2007, you basically need to extend your webapplication and add your membership & role provider.

Here you go  http://msdn.microsoft.com/en-us/library/bb975136.aspx

How to configure FBA in Sharepoint 2010

To configure FBA in SP 2010, you need not extend the current webapplication. You can create a brand new webapplication with claims enabled and can have FBA. Additionally you can have Windows Authentication too under claims.

Here you go http://technet.microsoft.com/en-us/library/ee806890.aspx

Programmatically change Workflow Auto Clean up days

Well, if you associate a workflow to any sharepoint list/ library & if the workflow instance is completed, then its association with its tasks and history list details will lasts for only 60 days. This is because we have a sharepoint out of the box "Workflow Auto clean up" timer job running daily for every webapplication.

If you want to change the default 60 days of clean up, then you are the right stop. The below sample will help you out to achieve the behaviour.

static void Main()
        {
            using (SPSite site = new SPSite("http://site_URL"))
            {
              usingSPWeb web = site.OpenWeb())
              {
                SPWorkflowTemplateCollection collection = web.WorkflowTemplates;
                SPList list = web.Lists["LIST_NAME"];//List name
                SPWorkflowAssociation _asso = null;
                foreach (SPWorkflowAssociation asso in list.WorkflowAssociations)
                {
                    if (asso.Name == "WORKFLOW_NAME")//Workflow name
                    {
                        asso.AutoCleanupDays = 7;//clean up days
                        _asso = asso;
                    }
                }
               list.UpdateWorkflowAssociation(_asso);
            }}
        }

Happy Coding !!!!!