Sharepoint Outlook:保存联系人时发生冲突

Sharepoint Outlook:保存联系人时发生冲突,sharepoint,outlook,outlook-addin,outlook-2010,Sharepoint,Outlook,Outlook Addin,Outlook 2010,我正在从事一个涉及Outlook和SharePoint之间联系人同步的项目。虽然微软提供了一个开箱即用的解决方案,但是它不能满足一些特定的要求,比如自定义列同步。 对于这个要求,我们必须创建一个outlook加载项。此加载项处理由于同步而创建的联系人文件夹的ItemAdd和ItemChange事件 在ItemChange事件中,我检查Notes字段中的一个标志,确定更改是从SharePoint还是Outlook进行的,并相应地更新联系人项目 下面是我的ItemChange事件的代码 void I

我正在从事一个涉及Outlook和SharePoint之间联系人同步的项目。虽然微软提供了一个开箱即用的解决方案,但是它不能满足一些特定的要求,比如自定义列同步。 对于这个要求,我们必须创建一个outlook加载项。此加载项处理由于同步而创建的联系人文件夹的ItemAdd和ItemChange事件

在ItemChange事件中,我检查Notes字段中的一个标志,确定更改是从SharePoint还是Outlook进行的,并相应地更新联系人项目

下面是我的ItemChange事件的代码

void Items_ItemChange(object Item)
    {

        try
        {

            ContactItem ctItem = Item as Outlook.ContactItem;






            string customFieldsAndFlag = ctItem.Body;



            Dictionary<string, string> customColumnValueMapping = new Dictionary<string, string>();

            if (!string.IsNullOrEmpty(customFieldsAndFlag))
            {
                string[] flagAndFields = customFieldsAndFlag.Split(';');
                if (flagAndFields.Length == 2)
                {
                    //SharePoint to Outlook
                    if (flagAndFields[0] == "1")
                    {


                        foreach (string customColumnAndValue in flagAndFields[1].Split('|'))
                        {
                            string[] KeyAndValue = customColumnAndValue.Split('=');

                            if (KeyAndValue.Length == 2)
                            {
                                if (ctItem.UserProperties[KeyAndValue[0]] == null)
                                {
                                    ctItem.UserProperties.Add(KeyAndValue[0], OlUserPropertyType.olText, true, OlUserPropertyType.olText);
                                    ctItem.UserProperties[KeyAndValue[0]].Value = KeyAndValue[1];
                                }
                                else
                                {
                                    ctItem.UserProperties[KeyAndValue[0]].Value = KeyAndValue[1];
                                }
                            }

                        }

                        ctItem.Body = "2;" + flagAndFields[1];


                    }
                    //Outlook to SharePoint
                    else
                    {



                        foreach (string customColumnAndValue in flagAndFields[1].Split('|'))
                        {
                            string[] KeyAndValue = customColumnAndValue.Split('=');

                            if (KeyAndValue.Length == 2)
                            {
                                if (ctItem.UserProperties[KeyAndValue[0]] != null && ctItem.UserProperties[KeyAndValue[0]].Value != null)
                                {
                                    KeyAndValue[1] = ctItem.UserProperties[KeyAndValue[0]].Value.ToString();
                                }
                                customColumnValueMapping.Add(KeyAndValue[0], KeyAndValue[1]);
                            }

                        }


                        string newBody = string.Empty;

                        foreach (KeyValuePair<string, string> kvp in customColumnValueMapping)
                        {
                            newBody += kvp.Key + "=" + kvp.Value + "|";
                        }

                        if (newBody == flagAndFields[1])
                        {
                            return;
                        }
                        else
                        {
                            ctItem.Body = "0;" + newBody;
                        }


                    }

                }



            }
            ctItem.Save();

        }
        catch(System.Exception ex)
        {
            // log the error always
            Trace.TraceError("{0}: [class]:{1} [method]:{2}\n[message]:{4}\n[Stack]:\n{5}",
                DateTime.Now, // when was the error happened
                MethodInfo.GetCurrentMethod().DeclaringType.Name, // the class name
                MethodInfo.GetCurrentMethod().Name,  // the method name
                ex.Message, // the error message
                ex.StackTrace // the stack trace information
            );
            // now display a friendly error to the user
            MessageBox.Show("There was an application error, you should save your work and restart Outlook.",
              "TraceAndLog",
              MessageBoxButtons.OK,
              MessageBoxIcon.Error);
        }
    }
void Items\u ItemChange(对象项)
{
尝试
{
ContactItem ctItem=项目作为Outlook.ContactItem;
字符串customFieldsAndFlag=ctItem.Body;
Dictionary customColumnValueMapping=新建字典();
如果(!string.IsNullOrEmpty(customFieldsAndFlag))
{
字符串[]flagAndFields=customFieldsAndFlag.Split(“;”);
if(flagAndFields.Length==2)
{
//SharePoint到Outlook
如果(flagAndFields[0]=“1”)
{
foreach(flagAndFields[1]中的字符串customColumnAndValue)。拆分(“|”))
{
字符串[]KeyAndValue=customColumnAndValue.Split('=');
if(KeyAndValue.Length==2)
{
if(ctItem.UserProperties[KeyAndValue[0]]==null)
{
添加(KeyAndValue[0],OlUserPropertyType.olText,true,OlUserPropertyType.olText);
ctItem.UserProperties[KeyAndValue[0]].Value=KeyAndValue[1];
}
其他的
{
ctItem.UserProperties[KeyAndValue[0]].Value=KeyAndValue[1];
}
}
}
ctItem.Body=“2;”+flagAndFields[1];
}
//Outlook到SharePoint
其他的
{
foreach(flagAndFields[1]中的字符串customColumnAndValue)。拆分(“|”))
{
字符串[]KeyAndValue=customColumnAndValue.Split('=');
if(KeyAndValue.Length==2)
{
if(ctItem.UserProperties[KeyAndValue[0]]!=null和&ctItem.UserProperties[KeyAndValue[0]].Value!=null)
{
KeyAndValue[1]=ctItem.UserProperties[KeyAndValue[0]].Value.ToString();
}
添加(KeyAndValue[0],KeyAndValue[1]);
}
}
string newBody=string.Empty;
foreach(customColumnValueMapping中的KeyValuePair kvp)
{
新手+=kvp.Key+“=”+kvp.Value+“|”;
}
if(newBody==flagAndFields[1])
{
回来
}
其他的
{
ctItem.Body=“0;”+新手;
}
}
}
}
ctItem.Save();
}
catch(System.Exception-ex)
{
//始终记录错误
Trace.TraceError(“{0}:[类]:{1}[方法]:{2}\n[消息]:{4}\n[堆栈]:\n{5}”,
DateTime.Now,//错误是什么时候发生的
MethodInfo.GetCurrentMethod().DeclaringType.Name,//类名
MethodInfo.GetCurrentMethod().Name,//方法名称
例如Message,//错误消息
ex.StackTrace//堆栈跟踪信息
);
//现在向用户显示一个友好的错误
MessageBox.Show(“出现应用程序错误,您应该保存工作并重新启动Outlook。”,
“跟踪日志”,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
现在的问题是,联系人项目更新从outlook第一次开始就可以正常工作。联系人得到正确更新,外接程序工作正常,更改在SharePoint中得到反映。但是,当我尝试再次使用outlook编辑联系人时,会弹出一个对话框,提示“无法更改项目,因为它已被其他用户或其他窗口更改。是否要在项目的默认文件夹中创建副本?”然后,oulook加载项停止工作

谁能为这个问题提出一个解决方案? 我无法使用ctItem.Close(),因为当我使用此函数时,同步过程不起作用,并且更改不会填充到SharePoint