随着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部分就完成了,如下图:
3.现在要完成功能部分,在Ribbon视图中右键->Property, 修改Behavior节中的Ribbon Type,这个type设置的意思是要将你的Ribbon应用在Outlook的什么地方,这里我们选择
确保可以在打开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实现了上述接口
代码
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,如图:
接下来,用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的过程。