Example #1
0
        public string VerifyFileToDownload(string filePath, string fileName)
        {
            ToEcm.ClientContext context = null;
            string flag       = "";
            string retMessage = "";

            try
            {
                string siteUrl      = SLMConstant.Ecm.SiteUrl;  //http://ecm/dept/public
                string domain       = SLMConstant.Ecm.Domain;
                string username     = SLMConstant.Ecm.Username;
                string password     = SLMConstant.Ecm.Password;
                string fileFullname = filePath + fileName;

                //Ex.fileFullname = /dept/public/OBTDocument/160083566900/ActPaymentType_25590517_045936.png

                flag = "connection";
                using (context = new ToEcm.ClientContext(siteUrl)
                {
                    Credentials = new NetworkCredential(username, password, domain)
                })
                {
                    context.ExecuteQuery();
                    log.Info("Verify connection: Sucess");
                }

                flag = "file";
                using (FileInformation sharePointFile = ToEcm.File.OpenBinaryDirect(context, fileFullname))
                {
                    log.Info("Verify file: Sucess");
                }
            }
            catch (Exception ex)
            {
                string message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                if (flag == "connection")
                {
                    log.Error("Verify connection: Failed because " + message);
                    retMessage = "ไม่สามารถเชื่อมต่อระบบ ECM ได้";
                }
                else if (flag == "file")
                {
                    log.Error("Verify file: Failed because " + message);
                    retMessage = "ไม่พบไฟล์บนระบบ ECM";
                }
                else
                {
                    log.Error(message);
                }
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }

            return(retMessage);
        }
        /// <summary> Return the preconfigured SP.ClientContext to use. </summary>
        private SP.ClientContext getSpClientContext(bool forceNewContext = false)
        {
            if (forceNewContext)
            {
                if (m_spContext != null)
                {
                    m_spContext.Dispose();
                }
                m_spContext = null;
            }

            if (m_spContext == null)
            {
                if (m_spWebUrl == null)
                {
                    m_spWebUrl = findCorrectWebPath(m_orgUrl, m_userInfo, out m_spContext);
                    if (m_spWebUrl == null)
                    {
                        throw new System.Net.WebException(Strings.SharePoint.NoSharePointWebFoundError(m_orgUrl.ToString()));
                    }
                }
                else
                {
                    // would query: testUrlForWeb(m_spWebUrl, userInfo, true, out m_spContext);
                    m_spContext             = CreateNewContext(m_spWebUrl);
                    m_spContext.Credentials = m_userInfo;
                }
                if (m_spContext != null && m_useContextTimeoutMs > 0)
                {
                    m_spContext.RequestTimeout = m_useContextTimeoutMs;
                }
            }
            return(m_spContext);
        }
Example #3
0
        public bool VerifyConnection()
        {
            ToEcm.ClientContext context = null;
            try
            {
                string siteUrl  = SLMConstant.Ecm.SiteUrl;      //http://ecm/dept/public
                string domain   = SLMConstant.Ecm.Domain;
                string username = SLMConstant.Ecm.Username;
                string password = SLMConstant.Ecm.Password;

                using (context = new ToEcm.ClientContext(siteUrl)
                {
                    Credentials = new NetworkCredential(username, password, domain)
                })
                {
                    log.Info("==============================================================================");
                    context.ExecuteQuery();
                    log.Info("Verify Connection: Sucess");
                }
                return(true);
            }
            catch (Exception ex)
            {
                string message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                log.Error("Verify connection: Failed because " + message);
                throw new Exception("ไม่สามารถเชื่อมต่อระบบ ECM ได้");
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
Example #4
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         context.Dispose();
     }
 }
Example #5
0
 public void Dispose()
 {
     if (_site != null)
     {
         _site = null;
     }
     if (_web != null)
     {
         _web = null;
     }
     if (_clientContext != null)
     {
         _clientContext.Dispose();
         _clientContext = null;
     }
     Credentials = null;
 }
Example #6
0
        private void DownloadFile(string filePath, string fileName)
        {
            ToEcm.ClientContext context = null;
            try
            {
                string siteUrl      = SLMConstant.Ecm.SiteUrl;  //http://ecm/dept/public
                string domain       = SLMConstant.Ecm.Domain;
                string username     = SLMConstant.Ecm.Username;
                string password     = SLMConstant.Ecm.Password;
                string fileFullName = filePath + fileName;

                context = new ToEcm.ClientContext(siteUrl)
                {
                    Credentials = new NetworkCredential(username, password, domain)
                };

                //Ex.filename = /dept/public/OBTDocument/160083566900/ActPaymentType_25590517_045936.png

                _log.Info("Download File Path : " + fileFullName);
                using (FileInformation sharePointFile = ToEcm.File.OpenBinaryDirect(context, fileFullName))
                {
                    //-------------------------

                    //Response.ContentType = "text/plain";    //StreamHelpers.GetContentType(fileInf.Extension);
                    Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));
                    // Write the file to the Response
                    const int bufferLength = 1000000;
                    byte[]    buffer       = new Byte[bufferLength];
                    int       length       = 0;
                    try
                    {
                        do
                        {
                            if (Response.IsClientConnected)
                            {
                                length = sharePointFile.Stream.Read(buffer, 0, bufferLength);
                                Response.OutputStream.Write(buffer, 0, length);
                                buffer = new Byte[bufferLength];
                            }
                            else
                            {
                                length = -1;
                            }
                        }while (length > 0);

                        //Response.Flush();
                        //Response.End();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Error("Download File Error : " + ex);
                lblError.Text = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                throw ex;
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }

                Response.Flush();
                Response.End();
            }
        }
Example #7
0
        public bool AddAttachment(string paramFileName, string strFileName, string ticketId, string fileType, string loginId)
        {
            try
            {
                string siteUrl  = SLMConstant.Ecm.SiteUrl;      //--http://ecm/dept/public
                string sitePath = SLMConstant.Ecm.SitePath;     //--/dept/public/OBTDocument/
                string listName = SLMConstant.Ecm.ListName;     //OBTDocument
                string domain   = SLMConstant.Ecm.Domain;
                string userName = SLMConstant.Ecm.Username;
                string password = SLMConstant.Ecm.Password;

                string fileUrl        = string.Empty;
                string fileNameUpload = strFileName;
                bool   result         = false;
                //DateTime expDate = Convert.ToDateTime(expireDate);
                //string now = DateTime.Now.ToString("dd/MM/yyyy", new CultureInfo("en-US"));
                //DateTime today = Convert.ToDateTime(now);

                bool res = AddNewFolder(sitePath, listName, "Folder", ticketId);
                if (res)
                {
                    if (!string.IsNullOrEmpty(paramFileName))
                    {
                        fileUrl = paramFileName;
                    }
                    if (fileNameUpload == string.Empty)
                    {
                        fileNameUpload = sitePath + ticketId + "/" + strFileName;
                    }
                    else
                    {
                        fileNameUpload = sitePath + ticketId + "/" + fileNameUpload;
                    }
                    var context = new ToEcm.ClientContext(siteUrl)
                    {
                        Credentials = new NetworkCredential(userName, password, domain)
                    };

                    try
                    {
                        //log.Info("AddAttachment : Ecm path to save = " + fileNameUpload);
                        //log.Info("AddAttachment : Local fileUrl to save = " + fileUrl);

                        using (File.FileStream fs = File.File.Open(fileUrl, File.FileMode.Open))
                        {
                            ToEcm.File.SaveBinaryDirect(context: context, serverRelativeUrl: fileNameUpload, stream: fs, overwriteIfExists: true);
                        }
                        result = true;
                        log.Info("AddAttachment : Success");

                        //ToEcm.File.SaveBinaryDirect(context: context, serverRelativeUrl: fileNameUpload, stream: fs, overwriteIfExists: true);

                        //if (!UpdateContent(result, campaignId, campName, description, expDate, username, campGroup, attachedType))
                        //{
                        //    DeleteFile(fileNameUpload);
                        //}
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error AddAttachment : " + ex);
                        result = false;
                        throw ex;
                    }
                    finally
                    {
                        context.Dispose();
                    }
                }
                else
                {
                    result = false;
                }
                return(result);
            }
            catch (Exception ex)
            {
                log.Error("Error AddAttachment : " + ex.Message);
                throw ex;
            }
        }