现在的位置: 首页 > 综合 > 正文

如何用编程的方式 在sharepoint里新建一个task后! 给 task 指定的人发送一封邮件!

2012年06月15日 ⁄ 综合 ⁄ 共 2841字 ⁄ 字号 评论关闭

http://social.microsoft.com/Forums/zh-CN/partnercndevsharepoint/thread/8966ea4e-ce6c-444a-9619-b7a6731a41d3

A:

当用sharepoint创建一个task后! sharepoint会自动给 task的接收者发送一封邮件,

且发送者是:company Sharepoint [sharepoint@company .com]
邮件接收者就是:Assigned To: XX

邮件内容就是:任务的详细信息 上面表格内容!

 using (SPSite site = new SPSite(webUrl))
            {
                using (SPWeb web = site.OpenWeb(new Guid(groupGuid)))
                {
                    web.AllowUnsafeUpdates = true;
                    //添加一个任务
                       SPList taskList = web.Lists["Tasks"];
                    SPListItemCollection items = taskList.Items;
                    SPListItem listitem = items.Add();
                    listitem["Title"] = tbtitle.Text;
                    listitem["Priority"] = ddlPriority.Text;
                    listitem["Status"] = ddltatus.Text;
                    listitem["% Complete"] = Convert.ToDouble(tbcomplete.Text) / 100.00;
                    listitem["Assigned To"] = hfAssignedTo.Value;
                    listitem["Description"] = tbDescription.Text;
                    listitem["Start Date"] = tbStartDate.Text;
                    listitem["Due Date"] = tbDueDate.Text;
                    if (FileUpload1.HasFile)
                        listitem.Attachments.Add("attachments", FileUpload1.FileBytes);
                    listitem.Update();
                    taskList.Update();
                }

            }

以上代码为新增一个task!  希望在新增任务成功也发送一封邮件给任务接收者
如何实现!

Q:

您好,
关于这个问题,我们需要在代码中首先要确认这个新增的task是否添加成功,我们可以通过为Task这种List创建Event Receiver去实现,在Event Reveiver中监听ItemAdded事件,这样的话,在task创建之后ItemAdded就会被触发,然后在这个事件中发送邮件。
在整个过程中涉及到两个方面,第一个方面是创建和部署Event Receiver, 第二个是如果发送邮件:
Event Receiver (以下步骤默认您已安装了WSS Visual Studio Extensions)
1.创建Event Receiver
a. 打开Visual Studio创建一个空的(Empty)Sharepoint项目
b. 添加新的Event Receiver Item (Project -> Add new Item -> Event Receiver)
c. 选择Event Receiver的List模板,就是在您选择Event Receiverd之后,出现一个下拉框,这时您可以选择"Tasks"。
2. 修改代码
打开ItemEventReceiver.cs, 然后把ItemAdded方法前面的注释符号去掉,然后在这个方法里面添加发送邮件的代码:
为了使程序可以发送邮件,您需要去配置传出电子邮件设置(outgoing e-mail settings)

        public override void ItemAdded(SPItemEventProperties properties)
        {
            //SPList taskList = properties.ListId web.Lists["SampleTaskList"];

            SPListItem taskListItem = properties.ListItem;
            SPFieldUser assignedTo = (SPFieldUser)taskListItem.Fields[SPBuiltInFieldId.AssignedTo];
            SPFieldUserValue user = (SPFieldUserValue)assignedTo.GetFieldValue(taskListItem[SPBuiltInFieldId.AssignedTo].ToString());
            SPUser userObject = user.User;

            using (SPWeb web = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
            {

                bool isEmailServerSet = SPUtility.IsEmailServerSet(web);

                if (isEmailServerSet)
                //If this returns false, you should not bother trying to send the email. Instead, show an error message or notify the SharePoint administrator, to check the settings of the server. If it returns true, you are good to go:
                {
                    bool appendHtmlTag = false;
                    bool htmlEncode = false;
                    string toAddress = userObject.Email;
                    string subject = "Subject";
                    string message = "任务的详细信息 上面表格内容!";

                    bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);

                    ////In some cases, you may need to run this code with elevated privileges:
                    //SPSecurity.RunWithElevatedPrivileges(delegate()
                    //{
                    //    bool result1 = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);
                    //});
                }
            }
        }

3. Deploy
a.目属性中设置部署到的网站,Project->Properties->Debug->Start broswer with URL
b.部署Feature, Project->Deploy.

 

感谢微软版主 Jiang Tao Liu 一直给我的帮助!

 

Technorati 标签: ,

抱歉!评论已关闭.