Outlook:保存联系时发生冲突

时间:2012-08-31 07:22:40

标签: sharepoint outlook outlook-addin outlook-2010

我正在开发一个涉及Outlook和SharePoint之间的联系人同步的项目。虽然Microsoft提供了一个现成的解决方案,但它不能满足某些特定要求,如自定义列同步。 对于此要求,我们必须创建一个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);
        }
    }

现在问题是,第一次联系人项目更新在outlook中工作正常。联系人正确更新,加载项工作正常,更改反映在SharePoint中就好了。但是当我尝试使用outlook再次编辑联系人时,弹出窗口显示“项目无法更改,因为它已被其他用户或其他窗口更改。是否要在项目的默认文件夹中进行复制?”并且oulook加载项此后停止工作。

有人可以为这个问题建议一个解决方案吗? 我不能使用ctItem.Close(),因为当我使用此函数时,同步过程不起作用,并且更改不会填充到SharePoint。

0 个答案:

没有答案
相关问题