protected void _btnSyncObject_Click(object sender, EventArgs e)
        {
            this.Controls.Add(new Literal {
                Text = "<hr/><strong>Results:</strong><br/>"
            });

            try
            {
                var syncAD2Portal = new SyncAD2Portal();

                // impersonate to currently logged on windows user, to use its credentials to connect to AD
                WindowsImpersonationContext impersonationContext = null;
                if (this.UseImpersonate)
                {
                    var windowsIdentity = ((User)User.Current).WindowsIdentity;
                    if (windowsIdentity == null)
                    {
                        this.Controls.Add(new Literal {
                            Text = "Windows identity impersonation failed for current user."
                        });
                        return;
                    }
                    impersonationContext = windowsIdentity.Impersonate();
                }

                int? logid    = null;
                bool noerrors = false;
                try
                {
                    logid = AdLog.SubscribeToLog();
                    syncAD2Portal.SyncObjectFromAD(_tbLdapPath.Text);
                    noerrors = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                    this.Controls.Add(new Literal {
                        Text = string.Format("An error occurred during execution ({0}).<br/>See adsync log below and eventlog for details.<br/><br/>", ex.Message)
                    });
                }
                finally
                {
                    if (impersonationContext != null)
                    {
                        impersonationContext.Undo();
                    }

                    if (logid.HasValue)
                    {
                        var logStr = AdLog.GetLogAndRemoveSubscription(logid.Value);
                        this.Controls.Add(new Literal {
                            Text = logStr.Replace(Environment.NewLine, "<br/>")
                        });
                    }

                    // add link to object to bottom
                    if (noerrors)
                    {
                        var syncInfo = syncAD2Portal.GetSyncInfo(_tbLdapPath.Text);
                        if (syncInfo.PortalNodeExists)
                        {
                            this.Controls.Add(new Literal {
                                Text = string.Format("<hr/>Check the results: <a href='/Explore.html#{0}' target='_blank'>{0}</a>", syncInfo.TargetPortalPath)
                            });
                        }
                    }
                }
            }
            catch (SecurityException ex)
            {
                Logger.WriteException(ex);
                this.Controls.Add(new Literal {
                    Text = string.Format("A security exception occurred ({0}).<br/>See eventlog for details.", ex.Message)
                });
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex);
                this.Controls.Add(new Literal {
                    Text = string.Format("An exception occurred ({0}).<br/>See eventlog for details.", ex.Message)
                });
            }
        }
        protected void _btnSyncObject_Click(object sender, EventArgs e)
        {
            this.Controls.Add(new Literal {
                Text = string.Format("<hr/><strong>{0}:</strong><br/>", SenseNetResourceManager.Current.GetString("SyncObjectFromADPortlet", "Results"))
            });

            try
            {
                var syncAD2Portal = new SyncAD2Portal();

                // impersonate to currently logged on windows user, to use its credentials to connect to AD
                WindowsImpersonationContext impersonationContext = null;
                if (this.UseImpersonate)
                {
                    var windowsIdentity = ((User)User.Current).WindowsIdentity;
                    if (windowsIdentity == null)
                    {
                        this.Controls.Add(new Literal {
                            Text = SenseNetResourceManager.Current.GetString("SyncObjectFromADPortlet", "IdentityImpersonationFailed")
                        });
                        return;
                    }
                    impersonationContext = windowsIdentity.Impersonate();
                }

                int? logid    = null;
                bool noerrors = false;
                try
                {
                    logid = AdLog.SubscribeToLog();
                    syncAD2Portal.SyncObjectFromAD(_tbLdapPath.Text);
                    noerrors = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteException(ex);
                    this.Controls.Add(new Literal {
                        Text = string.Format(SenseNetResourceManager.Current.GetString("SyncObjectFromADPortlet", "SyncError"), ex.Message)
                    });
                }
                finally
                {
                    if (impersonationContext != null)
                    {
                        impersonationContext.Undo();
                    }

                    if (logid.HasValue)
                    {
                        var logStr = AdLog.GetLogAndRemoveSubscription(logid.Value);
                        this.Controls.Add(new Literal {
                            Text = logStr.Replace(Environment.NewLine, "<br/>")
                        });
                    }

                    // add link to object to bottom
                    if (noerrors)
                    {
                        var syncInfo = syncAD2Portal.GetSyncInfo(_tbLdapPath.Text);
                        if (syncInfo.PortalNodeExists)
                        {
                            this.Controls.Add(new Literal {
                                Text = string.Format(SenseNetResourceManager.Current.GetString("SyncObjectFromADPortlet", "CheckResults"), syncInfo.TargetPortalPath)
                            });
                        }
                    }
                }
            }
            catch (SecurityException ex)
            {
                Logger.WriteException(ex);
                this.Controls.Add(new Literal {
                    Text = string.Format(SenseNetResourceManager.Current.GetString("SyncObjectFromADPortlet", "SyncErrorSecurity"), ex.Message)
                });
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex);
                this.Controls.Add(new Literal {
                    Text = string.Format(SenseNetResourceManager.Current.GetString("SyncObjectFromADPortlet", "SyncErrorException"), ex.Message)
                });
            }
        }