So I needed a method of changing the status of a secondary list’s items based on criteria from another list’s item (in this case for checking in and out backup tapes), and didn’t see too much information around on how to go about doing this using a coded workflow and not infopath. So here’s my code I did up this morning to achieve this.

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.Office.Workflow.Utility;

namespace backups_Workflow
{
    public sealed partial class Workflow1 : SequentialWorkflowActivity
    {
        //placeholders for guid's and id's
        private int ItemId;
        private Guid LibId;
        private Guid WebId;
        private Guid SiteId;
        private Guid tapeLibraryId;

        public String HistoryDescription = default(System.String);
        public String HistoryOutcome = default(System.String);

        public Workflow1()
        {
            InitializeComponent();
        }

        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            //all the id's for the site, tape library list and current list/item
            SPSite site = new SPSite(SiteId);
            SPWeb web = site.OpenWeb(WebId);
            SPDocumentLibrary doclib = web.Lists[LibId] as SPDocumentLibrary;
            SPList list = web.Lists[LibId];
            SPListItem li = list.GetItemById(ItemId);
            SPList tapeList = web.Lists[tapeLibraryId];
            SPListItem tapeItem;

            if (li != null)
            {
                object itemValue = li["Checked In"];
                object tapes = li["Tape Serials"];
                
                ArrayList tapeArray = new ArrayList();
                //string[] split = query.Split(new Char[] { ' ' });
                string listOtapes = tapes.ToString();

                //create seperate string for each item based on ; as a seperator
                string[] tapeSplitter = listOtapes.Split(new Char[] { ';' });
                
                foreach (string listedTape in tapeSplitter)
                {
                    //remove unneeded characters and add to array
                    StringBuilder editedTape = new StringBuilder(listedTape);
                    editedTape.Replace("#", "");
                    editedTape.Replace(";", "");
                    tapeArray.Add(editedTape.ToString());
                }
                

                if (itemValue.ToString() == "No")
                {
                    StringBuilder checkTapeEntry = new StringBuilder();
                    foreach (string tape in tapeArray)
                    {
                        try
                        {
                            //grab the tapes from the other list based on there ID
                            int tapeID = int.Parse(tape);
                            tapeItem = tapeList.GetItemById(tapeID);
                            tapeItem.Web.AllowUnsafeUpdates = true; //must be true for update to go through
                            //check out the tape
                            tapeItem["Checked In"] = false;
                            tapeItem.Update();
                        }
                        catch 
                        { 
                            //this is here to list the updated tape serials rather than the ID's
                            checkTapeEntry.Append(tape + " "); 
                        }
                    }
                    this.HistoryOutcome = ItemId.ToString() + " " + li.Fields["Checked In"].ToString() + " - " + checkTapeEntry.ToString();
                }
                else if (itemValue.ToString() == "Yes")
                {
                    StringBuilder checkTapeEntry = new StringBuilder();
                    foreach (string tape in tapeArray)
                    {
                        try
                        {
                            //grab the tapes from the other list based on there ID
                            int tapeID = int.Parse(tape);
                            tapeItem = tapeList.GetItemById(tapeID);
                            tapeItem.Web.AllowUnsafeUpdates = true; //must be true for update to go through
                            //check in the tape
                            tapeItem["Checked In"] = true;
                            tapeItem.Update();
                        }
                        catch 
                        { 
                            //this is here to list the updated tape serials rather than the ID's
                            checkTapeEntry.Append(tape + " "); 
                        }
                    }
                    this.HistoryOutcome = ItemId.ToString() + " " + li.Fields["Checked In"].ToString() + " - " + checkTapeEntry.ToString();
                }
                else
                {
                    this.HistoryOutcome = "Failed";
                }
            }
        }

        private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
        {
            ItemId = workflowProperties.ItemId;
            LibId = workflowProperties.ListId;
            WebId = workflowProperties.WebId;
            SiteId = workflowProperties.SiteId;
		//Second libraries GUID
            tapeLibraryId = new Guid("9360B239-89FD-4092-B0A7-A0DC3C8DD919");
        }
    }
}

trackback image tell a friend image Permalink Image

Post Tags: microsoft  sharepoint  workflow 


Next entry: Need Ideas Previous entry: Sharepoint SP1 Woes



Comments: (1)
youbegod on Thu, Mar 20th, 2008 at 10:34 PM

Thank you!


Page 1 of 1 pages

Post a comment

Name:
(Required)

Email:
(Required)

URL:

Smileys

Remember my personal information

Notify me of follow-up comments?

Submit the word you see below: