1

我正在处理一个涉及 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);
        }
    }

现在的问题是,联系人项目更新第一次从 Outlook 正常工作。联系人得到正确更新,加载项工作正常,更改也能很好地反映在 SharePoint 中。但是,当我尝试再次使用 Outlook 编辑联系人时,会弹出一个弹出窗口,提示“无法更改项目,因为它已被其他用户或在另一个窗口中更改。您想在该项目的默认文件夹中复制一份吗? ?” 并且 oulook 加载项此后停止工作。

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

4

0 回答 0