设为首页 收藏本站
查看: 879|回复: 0

[经验分享] [SharePoint 2010]开发与SharePoint2010 数据同步的Outlook2007 add-in

[复制链接]
累计签到:8 天
连续签到:1 天
发表于 2015-9-28 11:12:13 | 显示全部楼层 |阅读模式
  随着VS2010和SharePoint2010的推出,微软对与office产品线的协同工作开发的支持越来越强大了,开发一些office产品的插件也变得方便了很多,这里介绍一下啊Outlook2007 add-in的开发,使得能够和SharePoint2010完成同步。
  企业在使用SharePoint的同时,有时候可以和Outlook集成起来,比如在SharePoint中创建了一个Training meeting,但是在SharePoint中无法象Outlook那样Book meeting room,添加Attendee也不如Outlook里来的方便,因此希望同时能够在Outlook中也创建meeting request,这样不仅可以完成以上功能,同样也可以使用Outlook的TO-DO list的功能,管理个人的Calendar,同时和meeting有关的信息又可以在SharePoint site上管理维护。要完成这样的功能,我们首先需要开发一个Outlook add-in。下面介绍详细步骤:
  1.打开visual studio 2010 (2008亦可)->New Project, 在office栏中选择Outlook 2007 Add-in 模板,创建完项目后,Add New Item, 选择Ribbon(Visual Designer)此item.
  2.双击刚才创建的Ribbon item,可以看见一个Ribbon的设计视图,根据需要修改Tab,Group的名字,并在旁边的toolbox中拖一个button到group中。修改button的name,根据自己需要设置一些UI,这样UI部分就完成了,如下图:
DSC0000.jpg
  3.现在要完成功能部分,在Ribbon视图中右键->Property, 修改Behavior节中的Ribbon Type,这个type设置的意思是要将你的Ribbon应用在Outlook的什么地方,这里我们选择 DSC0001.jpg 确保可以在打开Outlook的meeting request时使用这个Ribbon Item。
  这里使用SharePoint的list web service来完成和SharePoint同步,在项目中添加web reference,url中输入 http://server/_vti_bin/lists.asmx?wsdl,添加Service。
  同时添加IListOperation和IAuthorization接口,完成对List操作和用户验证的抽象,类SyncAuthorization和ListServiceProxy实现了上述接口
  

DSC0002.gif DSC0003.gif 代码

  interface IAuthorization
    {
        bool Authorize(MeetingRequest request);
    }
interface IListOperation
    {
        XmlNode GetListItems(string listName, XmlNode query, XmlNode viewField);
        bool UpdateListItems(string listName, XmlNode batchElement);
    }
    class SyncAuthorization:IAuthorization
    {
        private string _currentUser;
        private string _listName;
        public SyncAuthorization(string currentUser, string listName)
        {
            this._currentUser = currentUser;
            this._listName = listName;
        }
        public bool Authorize(MeetingRequest request)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
            XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
            ndViewFields.InnerXml = "<FieldRef Name=\"Author\"/>";
            ndQuery.InnerXml = "<Where><Eq><FieldRef Name='Training_x0020_ID'/><Value Type='Text'>" + request.TrainingGuid + "</Value></Eq></Where>";
            XmlNode targetItem = ListServiceProxy.listServiceProxy.GetListItems(_listName,ndQuery,ndViewFields);
            XmlTextReader xmlTextReader = new XmlTextReader(targetItem.OuterXml, XmlNodeType.Element, null);
            while (xmlTextReader.Read())
            {
                if (xmlTextReader.Name == "z:row")
                {
                    string lookupFieldValue = xmlTextReader.GetAttribute("ows_Author").ToString();
                    string createrName = lookupFieldValue.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1];
                    if (createrName == _currentUser)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return false;
        }     
    }
}
  class ListServiceProxy:IListOperation,IDisposable
    {
        private TrainingListService.Lists trainingListServiceProxy;
        private ListServiceProxy()
        {
            trainingListServiceProxy = new TrainingListService.Lists();
            trainingListServiceProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
        }
        public static readonly ListServiceProxy listServiceProxy = new ListServiceProxy();
        public XmlNode GetListItems(string listName, XmlNode query, XmlNode viewFields)
        {
           return trainingListServiceProxy.GetListItems(listName, "", query, viewFields, null,null,null);
        }
        public bool UpdateListItems(string listName, XmlNode batchElement)
        {
            XmlNode resultNode = trainingListServiceProxy.UpdateListItems(listName, batchElement);
            string errorCode = resultNode.FirstChild.InnerText;
            if (errorCode == "0x00000000")
                return true;
            else
                return false;   
        }
        public void Dispose()
        {
            trainingListServiceProxy.Dispose();
        }
    }  
  这里的验证操作比较简单,只有SharePoint training meeting list中某个training meeting的创建者才可以在Outlook的meeting request中完成同步。
  TrainingInfoSynchronization类完成了我们需要的业务操作。

代码

class TrainingInfoSynchronization
    {
        public static readonly Outlook.Application application = application = new Outlook.Application();
        private string traininglistName = null;
        public TrainingInfoSynchronization(string traingListName)
        {
            traininglistName = traingListName;
        }
        public MeetingRequest GetCurrentOutlookRequest()
        {
            Outlook.AppointmentItem mail = application.ActiveInspector().CurrentItem;
            Regex rg = new Regex(@"[A-Fa-f0-9]{8}(-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}");
            using (mail as IDisposable)
            {
                MeetingRequest request = new MeetingRequest();
                request.Topic = mail.Subject;
                request.StartTime = mail.Start;
                request.EndTime = mail.End;
                request.Location = mail.Location;
                request.TrainingGuid = rg.Match(mail.Body).Value.ToString();
                string currentRequestID = GetItemIDByTrainingGuid(request);
                int resultID = 0;
                bool isGetCorrectID = Int32.TryParse(currentRequestID, out resultID);
                if (isGetCorrectID)
                    request.RequestId = resultID;
                else
                    request.RequestId = 0;
                return request;
            }
        }
        private string GetItemIDByTrainingGuid(MeetingRequest request)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
            XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
            ndViewFields.InnerXml = "<FieldRef Name=\"Author\"/>";
            ndQuery.InnerXml = "<Where><Eq><FieldRef Name='Training_x0020_ID'/><Value Type='Text'>" + request.TrainingGuid + "</Value></Eq></Where>";
            XmlNode targetItem = ListServiceProxy.listServiceProxy.GetListItems(traininglistName, ndQuery, ndViewFields);
            XmlTextReader xmlTextReader = new XmlTextReader(targetItem.OuterXml, XmlNodeType.Element, null);
            string requestID = String.Empty;
            while (xmlTextReader.Read())
            {
                if (xmlTextReader.Name == "z:row")
                {
                    requestID = xmlTextReader.GetAttribute("ows_ID").ToString();
                    break;
                }
            }
            return requestID;
        }
        public bool UpdateTrainingInfoInSharePoint(MeetingRequest request)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlElement batchElement = xmlDoc.CreateElement("Batch");
            string startDateTime = request.StartTime.ToString("s") + "Z";
            string endDateTime = request.EndTime.ToString("s") + "Z";
            string command = "<Method ID=\"1\" Cmd=\"Update\">" + "<Field Name=\"ID\">" + request.RequestId.ToString() + "</Field><Field Name=\"Title\">" + request.Topic.ToString() + "</Field><Field Name=\"Start_x0020_Time\">" + startDateTime + "</Field><Field Name=\"End_x0020_Time\">" + endDateTime + "</Field><Field Name=\"Location\">" + request.Location + "</Field></Method>";
            batchElement.SetAttribute("OnError", "Continue");
            batchElement.SetAttribute("ListVersion", "1");
            batchElement.InnerXml = command;
            return ListServiceProxy.listServiceProxy.UpdateListItems(traininglistName, batchElement);
        }
    }  
  GetCurrentOutlookRequest() 方法提取当前打开的outlook meeting request中的数据,而UpdateTrainingInfoInSharePoint方法完成从Outlook到SharePoint的同步。
  双击Ribbon Item上我们添加的Button,在事件处理方法中添加

代码

try
            {
                tabSharePoint.Visible = false;
                TrainingInfoSynchronization tSync = new TrainingInfoSynchronization(TRAININGLISTNAME);
                DialogResult result = MessageBox.Show("This operation will change the training information on SharePoint training center, do you want to continue?", "Training Info Sync", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    string currentOutlookUser = TrainingInfoSynchronization.application.Inspectors.Session.CurrentUser.Name;
                    IAuthorization authorization = new SyncAuthorization(currentOutlookUser, TRAININGLISTNAME);
                    MeetingRequest request = tSync.GetCurrentOutlookRequest();
                    if (request.RequestId == 0)
                    {
                        MessageBox.Show("We cannot find the related training info in Training Center, please keep the [Training GUID] correct.");
                        return;
                    }
                    if (authorization.Authorize(request))
                    {
                        bool isSuccess = tSync.UpdateTrainingInfoInSharePoint(request);
                        if (isSuccess)
                        {
                            MessageBox.Show("Meeting information is successfully sychronized  with Training Center on SharePoint.");
                        }
                        else
                        {
                            MessageBox.Show("Errors happened in the process of syhchronization, please try it again or contact with the administrator of Training Center site.");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Only the training creater can synchronize the information.");
                    }
                }
            }
            catch
            {
                MessageBox.Show("Only the training creater can synchronize the information.");
            }
            finally
            {
                ListServiceProxy.listServiceProxy.Dispose();
            }  
  这样一个从Outlook到SharePoint的同步就完成了。Build项目完成后,右键项目->publish,选择一个本地路径,这样VS2010将为我们在本地产生一个Setup.exe,运行这个文件,可以在客户端的Outlook上安装上我们开发的ribbon add-in,当我们安装完成后,在Outlook中新建一个meeting request或者打开一个meeting request,就可看到我们的Ribbon Tab,如图:
DSC0004.jpg
  接下来,用SharePoint designer在Training meeting list 的newform和editform两个页面中添加js,确保当每一个meeting item 保存的时候,都可以弹出一个Outlook的meeting request 窗口。如下面的js:

代码

function OpenOutlookMeetingRequest(strTo, strCC, strSubject, strHTMLBody, strStart, strEnd, strLocation)
{
    var outlookApp = new ActiveXObject("Outlook.Application");
    try
    {
        var mailItem= outlookApp.createItem(1); //Appointment
        mailItem.MeetingStatus = 1;//Set Appointment to Meeting
        mailItem.Subject=strSubject;
        mailItem.Body = strHTMLBody;
        mailItem.Start = strStart;
        mailItem.End = strEnd;
        mailItem.Location = strLocation;
        if(strTo != "")
            mailItem.recipients.add(strTo);
        mailItem.display (0);
    }
    finally
    {
        outlookApp =null;
    }   
}
  
  这样,当我们在SharePoint的Training Meeting List中创建一个Meeting item结束的时候,都会弹出一个Outlook meeting request的窗口,我们把当前在Item中添加的一些相关信息,如Topic,Agenda等等先自动填入meeting request窗口的Subject和Body中,一旦我们在meeting request的窗口中完成了Room booking和Agendee的添加后,我们可以点击Ribbon tab中的同步按钮,完成信息写回到SharePoint的过程。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-119917-1-1.html 上篇帖子: SharePoint开发笔记-SharePoint2010的WebPart和Linq 下篇帖子: SharePoint 2013 Designer系列之数据视图筛选
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表