Tuesday, 20 November 2012

Create a Workflow Using VS 2010 with an initiation Form and Process the Data in a SP list.

Create a Workflow Using VS 2010 with an initiation Form and Process the Data in a SP list.


1.       Create a Customer List in a SharePoint Site, with required columns.
2.       Create a new VS project File, New, New Project, Select C#, SharePoint, 2010, Sequential Workflow and Give the Project name as Mohan_WFSample1.
3.       Click on Ok,  Enter the Url Http://Madan:8181( Give your site URL). Select Deploy as a farm solution, Click on next, Enter the Workflow name (CustomerWF), Select List Workflow click on next.
4.       The library or list to associate your workflow with = select your list/library (ex:- Customer).
5.       The history list to display … = Workflow history
6.       The task list to display … = Tasks.
7.       Click on next.
8.       Select a User Manually starts the work flow?
9.       Now click on Finish.

*      Now create a new Workflow initiation Form to be used by the workflow.
v  In the solution explorer, with in project select Workflow1, right click on it , in the properties window click on Add, New item, Select Workflow Initiation Form, click Add.
*      Complete the Workflow initiation form
v  Add the following code to the main content placeholder
<br />
<br />
<br />
<asp:CheckBox ID="Checkbox" runat="server" Text="Fast Track Customer" />

v  Now open the WorkflowInitiationForm.aspx.cs
1.       In the GetInitiationData method replace returnstring.Empty; with the following code.

*      Now process Workflow initiation Form data in Workflow1

v  Open the view Designer of Workflow1.cs. From the Toolbox, add a Code activity, after the onWorkflowActivated1 activity.
v  Double click on codeActivity1 to generate an Execute Code event in code behind. Add the following code to the codeActivity1_ExecuteCode method. Set a Breakpoint in the method.
           If (workflowProperties.InitiationData == "True")
{
System.Diagnostics.Debug.WriteLine("This is a fast track customer");
}

*      Now Deploy the Workflow / Debug.
v  Press F5 to deploy and debug the new workflow
v  Navigate to the Customer list and create a new item in the list
v  Click on the edit dropdown for the item and select Workflows, Execute

Sampe API Codes

protected void Button1_Click(object sender, EventArgs e)
    {
        SPSite roosite = new SPSite(TextBox1.Text);
        Label1.Text = "Root collection found at Url " + roosite.Url.ToString() + " <br>  is part of Web Application " + roosite.WebApplication.Name;

    }

protected void Button2_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite("http://madhanpc:8089");
        SPWebCollection sites = site.AllWebs;
        string[] names = sites.Names;
        foreach (string name in names)
            Response.Write(name + "<BR>");
    }

protected void Button3_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite("http://madhanpc:8089");
        SPWeb web = site.AllWebs[0];
        foreach (SPList list in web.Lists)
        {
            Response.Write(list.Title + "  " + list.ItemCount + "<br>");
        }
        Response.Write(web.Lists.Count);
    }


protected void Button4_Click(object sender, EventArgs e)
    {
SPSite site = new SPSite("http://madhanpc:8089");
SPWeb web = site.OpenWeb();       
        web.AllowUnsafeUpdates = true;       
        SPWebCollection subsite = web.Webs;
        try
{
            SPWeb newSubWeb = subsite.Add("Morning123", "Morning", "Morning only", 1033, "STS#1", false, false);
            Response.Write("New site created.");
        }
catch (Exception ex)
        {
            Response.Write("Is not created.." + ex.Message);
        }
    }

protected void Button5_Click(object sender, EventArgs e)
    {
        //Accessing List Item Values
        //Create one site and a task with some items
        SPSite rootSite = new SPSite("Http://madhanpc:8089");
        SPWeb web = rootSite.AllWebs["MyTeam"];
        //SPList :- Represents a list on a SharePoint Web site.
        SPList taskList = web.Lists["tests"];
        //SPListItem :- Represents an item or row in a list. 
        foreach (SPListItem item in taskList.Items)
        {
            Response.Write("Item Name " + item.Name + "Assigner To " + item["AssignedTo"] + "Due Date " + item["DueDate"] + "<br>");
        }
    }

protected void Button6_Click(object sender, EventArgs e)
    {
        SPSite rootSite = new SPSite("Http://madhanpc:8089/myteam");
        SPWeb web = rootSite.OpenWeb();
        //Security setting that allows you to add to / modify the site
        web.AllowUnsafeUpdates = true;
        SPList taskList = web.Lists["Tests"];
        //Adding new task
        SPListItem newTask = taskList.Items.Add();
        newTask["Title"] = "Create WP";
        newTask["PercentComplete"] = 0.1;
        newTask["Assigned To"] = "15;#MADHANPC\\emp1";
        newTask.Update();
        Response.Write("items manupulation completed..");

    }

protected void Button7_Click(object sender, EventArgs e)
    {
        SPSite rootSite = new SPSite("Http://madhanpc:8089/myteam");
        SPWeb web = rootSite.OpenWeb();
        //Security setting that allows you to add to / modify the site
        web.AllowUnsafeUpdates = true;
        SPListTemplate sourceTemplate = web.ListTemplates["Tasts"];
        Guid newListGuid = web.Lists.Add("Task123", "Tasklist1232", sourceTemplate );
        SPList newList = web.Lists[newListGuid];
        newList.Description = "Modified Custom Task List";
        newList.Update();
        Response.Write("Is added succes fully.."); 
    }

protected void Button8_Click(object sender, EventArgs e)
    {
        //Get all document libraries from abcc site
        SPWeb myWeb = new SPSite("http://madhanpc:8089/myTeam/").OpenWeb();
        DropDownList1.DataSource = myWeb.Folders;
        DropDownList1.DataBind();
    }

protected void Button9_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite(@"http://madhanpc:8089/myteam/");
        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;
        try
        {
            // Add the document library.
            Guid newListID = web.Lists.Add("Doc","Test", SPListTemplateType.DocumentLibrary);
            // Set additional properties on the new document library.
            SPList newList = web.Lists[newListID];
            newList.OnQuickLaunch = true;
            newList.EnableVersioning = true;
            newList.Update();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }

protected void Button10_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite(@"http://madhanpc:8089/MyTeam/");
        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;
        string path = "";
        // here filePath is html file control with runat="server" being used to pick the file browsed by user  
        string[] fileName = filePath.PostedFile.FileName.Split('\\');
        int length = fileName.Length;
        // get the name of file from path
        string file = fileName[length - 1];
        //  Get the library object where LibraryName is the name of Document Library.
        SPFolder folder = web.GetFolder("Doc");
        //
        SPFileCollection files = folder.Files;
        // Pick up the file in binary stream
        Stream fStream = filePath.PostedFile.InputStream;
        // Read file in a byte array
        byte[] MyData = new byte[fStream.Length];
        fStream.Read(MyData, 0, (int)fStream.Length);
        fStream.Close();
        SPFile fff = files.Add(file, MyData, true);
        web.AllowUnsafeUpdates = false;
    }

protected void Button11_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite(@"http://madhanpc:8089/MyTeam/");
        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;
        //Get the Document Library and its view
        SPList list = web.Lists["Doc"];
        SPListItemCollection items = list.Items;
        //Now we have to write a code for downloaing information of the document,
        //Read all documents and write those files to Local System
        foreach (SPListItem item in items)
        {
            //Write only names
            Response.Write(item.Name + "<br>");
        }
    }