SPS2003中的Workflow

提到Workflow,总觉得似乎是很高深,很奇妙的东西。其实,不就是一个普通的程序吗?SPS2003中也有Workflow的支持,GotDotNet上还有一个例子,大家可以去看看:

https://www.gotdotnet.com/workspaces/releases/viewuploads.aspx?id=c07f64e8-8229-49a1-b160-b24c89122894

还有人做了一段vedio:

https://blogs.leadit.be/files/WorkflowInstallation.wmv

这段vedio应该是基于比较老的版本做的,不太准确,但是很清晰。

下面,我们简单看一下这个程序的功能:

===========================

 

上载一篇文档,然后设置一个属性—status。如果设置为Ready,那么就转移到另外一个文件夹,并且发一封信出来。

说实话,这个功能很简单,我都会写,不就是一个简单的拷贝/删除,再加上发信吗?而且我还真得写过诶!^_^好,把代码贴出来,供大家参考。不过,代码写得不符合软件功能要求,换句话说,比较丑陋。

 

using System;

using System.IO;

using System.Net;

using System.ComponentModel;

using System.Web.UI;

using System.Web.Mail;

using System.Web.UI.WebControls;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

namespace sampleworkflow

{

      public class docworkflow: IListEventSink

      {

           void IListEventSink.OnEvent(Microsoft.SharePoint.SPListEvent evt)

           {

                

                 SPWeb SharePointWeb = evt.Site.OpenWeb();

                 SPFile SharePointEventItem = SharePointWeb.GetFile(evt.UrlAfter);

                 string sourceurl = SharePointWeb.Url +"/" + SharePointEventItem.Url;

                 SPListItem oItem = SharePointEventItem.Item;

                 string url;

                 string approvalurl;

                

                 if(evt.Type == SPListEventType.Insert)

                 {

                      try

                      {

                           

                            url=oItem["DesUrl"].ToString();

                            System.Uri myURi = new System.Uri(sourceurl);

                            WebRequest req = WebRequest.Create(myURi);

                            req.Method = "GET";

                            req.Timeout = System.Threading.Timeout.Infinite;

                            req.Credentials = CredentialCache.DefaultCredentials;

                            WebResponse res = req.GetResponse();

                            Stream inStream = res.GetResponseStream();

                            long length = res.ContentLength;

                

                            byte[] contents = new byte[length];

                            int readnum;

                            int num;

                            readnum=0;

                            while (readnum<length)

                            {

                                  num= inStream.Read(contents, readnum, (int) (length- readnum));

                                  readnum += num;

                            }

                            inStream.Close();

                            SPWeb site = new SPSite(url).OpenWeb();

                            site.AllowUnsafeUpdates = true;

                           

                            SPFile upfile=site.Files.Add(url, contents);

                            SPListItem destitem = upfile.Item;

                

                 destitem.ModerationInformation.Status=SPModerationStatusType.Pending;

                           

                            destitem.Update();

                           

                   

                            SharePointEventItem.ParentFolder.Files.Delete(SharePointEventItem.Url);

 

                           

                            string foldername = url.Substring(0, url.LastIndexOf("/"));

                            approvalurl = "<" + foldername + "/Forms/EditForm.aspx?ID=" + destitem["ID"].ToString() + "&ChangeApproval=TRUE&SOURCE=" + foldername + "/Forms/mod-view.aspx&RootFolder=" + foldername + ">";

                            SPGlobalAdmin globAdmin = new SPGlobalAdmin();

                            string smtpserver = globAdmin.OutboundSmtpServer;

                            string MailFromAddress = globAdmin.MailFromAddress;

                            string MailToAddress = "oliverlu@microsoft.com";

                            string MailSubject = "Please approval the item!";

                            SmtpMail.SmtpServer = smtpserver;

                            SmtpMail.Send(MailFromAddress, MailToAddress, MailSubject, approvalurl);

                      }

                      catch (Exception exp)

                      {

                            System.Windows.Forms.MessageBox.Show(exp.Message);

                      }

                 }

           }

      }

}