Sunday, May 27, 2012

Run Workflow from Hyperlink in SharePoint List

There is an  easy way for the users to start a workflow from the link instaed of selecting from item menu and then select the workflow from the available workflows and then start workflow from the workflow start form.

In this example i am going to update the list item with the workflow that is triggred from the hyprtlink in the SharePoint list


1) First create a workflow to update any list item in the SharePoint site List.
In the workflow settings click the "Allow this workflow to be manually started" and leave other checkboxes unchecked.

2) Publish the workflow, then go to the list to which this workflow is associated, select any item and then from the item menu select workflows. This will lead you to the workflows form attached with this list.




3) Click on your workflow that you created earliar to update item and this will redirect you the workflow start form.




4) Copy the url of this form from the address bar. This would be like
yoursite/_layouts/IniWrkflIP.aspx?List={9a6f5d9b-f075-43a3-a940-dafd1191477b}&ID=117&TemplateID={7b6230c2-8e22-4e7d-b167-6e68bce3a22c}&Source=yoursite/yourlist/forms/allitems.aspx

This url contains List={guid of list}, ID item id list item, TemplateID={GUID of the workflow} and source={after completion of the workflow it will redirect to this url}

5) Now create a field in this list called "start workflow" of type hyperlink. We will use this field to trigger the workflow.

6) Create another list workflow on this list called "populate start workflow link". Check the "Start workflow automatically when an item is created" in the properties window of this workflow

a) In step 1 of the workflow, create a variable named link of string type and set its value to the url of the workflow start form we copied in the step 4 as described in the following image.After the url put a comma',' and put a white space and then put a word you want to display in list otherwise it will show whole url. In this example i have used the "Publish". See image below


b) Now create another action "Update List Item". In the Item select current item and the set the field "start workflow" to this varibale as described in the below image. I have used the "Publish" field in this example.



c) Now save and publish the workflow. Whenever new item will be created a link will be created in the list and that link will redirect you to the satrt workflow form.






This is an effective and easy way for the end users to start workflow from the hyperlink field in the list


Thursday, May 3, 2012

Bulk Delete List Items


How to delete multiple Items from a SharePoint List

There are scenarios where you want to delete multiple list items at once. Insetad of using delete method of SPListItem you can use the following code that deletes multiple list items that uses batch process.

  void DeleteBulkItems(SPWeb web,SPList list, String Ids)
        {
            StringBuilder sbDelete = new StringBuilder();

            sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

            string command = "<Method><SetList Scope=\"Request\">" + list.ID + "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar     Name=\"Cmd\">Delete</SetVar></Method>";

            string[] _DeletedIDs = Ids.Split(',');

            foreach (String item in _DeletedIDs)
            {
                sbDelete.Append(string.Format(command, item.ToString()));
            }

            sbDelete.Append("</Batch>");

           web.ProcessBatchData(sbDelete.ToString());
        }

In this method 3 parametrs are there SPWeb, SPList(List name from where you want to delete items) and Ids (List item ids to be deleted).

In above code snippet you can see that i am using a xml batch with command delete to delete list items with the specified List Item ids.

Here is the code snippet from where above function is called ;

SPweb web =SPContext.Current.Web;
SPList list=web.Lists["your list title"];
  SPListItemCollection _coll =  list.Items
  string IDs = string.Empty;
            if (_coll.Count > 0)
            {

                foreach (SPListItem item in _coll)
                {
                    IDs = IDs + string.Format("{0},", item.ID);
}
 

    }


  IDs = IDs.TrimEnd(',');
                DeleteBulkItems(web, list, IDs);

In above code i have used a string variable and that stores the ids of list items separetd by comma and in the end called the "DeleteBulkItems" function with the parameterts and the ids of the items to be deleted.

This is an eaxample of  batch process that deletes the list items.