Exemplo n.º 1
0
        public void InsertLibraryItem(string documentsFullPath, string imagesFullPath, string imageFileName, string documentFileName, Boolean ignoreFieldsNotFound)
        {
            #region Upload document information to SharePoint Library

            string documentFullFileName = documentsFullPath + "\\" + documentFileName;

            clientContext.Load(libraryList);
            clientContext.Load(libraryList.Fields);
            clientContext.ExecuteQuery();

            ArrayList arlMetadatos = new ArrayList();
            using (CsvFileReader reader = new CsvFileReader(documentFullFileName))
            {
                CsvRow row = new CsvRow();
                while (reader.ReadRow(row))
                {
                    //if (row[0] == "Etiqueta_36")
                    //{
                    #region Ciclo etiqueta

                    ArrayList arlEtiquetas    = new ArrayList();
                    bool      etiquetasLibros = false;
                    if (row[0] == "Etiqueta_15")
                    {
                        string x = "";
                    }

                    if (row[0] == "Etiqueta_08" || row[0] == "Etiqueta_37" || row[0] == "Etiqueta_44")
                    {
                        if (row[0] == "Etiqueta_08")
                        {
                            arlEtiquetas.Add("Etiqueta_08_01");
                            arlEtiquetas.Add("Etiqueta_08_02");
                            arlEtiquetas.Add("Etiqueta_08_03");
                        }

                        if (row[0] == "Etiqueta_37")
                        {
                            arlEtiquetas.Add("Etiqueta_37_01");
                            arlEtiquetas.Add("Etiqueta_37_02");
                            arlEtiquetas.Add("Etiqueta_37_03");
                        }

                        if (row[0] == "Etiqueta_44")
                        {
                            arlEtiquetas.Add("Etiqueta_44_01");
                            arlEtiquetas.Add("Etiqueta_44_02");
                            arlEtiquetas.Add("Etiqueta_44_03");
                        }

                        etiquetasLibros = true;
                    }
                    else
                    {
                        arlEtiquetas.Add(row[0]);
                    }

                    foreach (string etiquetaActual in arlEtiquetas)
                    {
                        ArrayList arlValores = new ArrayList();

                        arlValores.Add(BuscarCoincidenciaEtiqueta(etiquetaActual));     // Field Label

                        string iName = string.Empty;
                        string iType = string.Empty;
                        foreach (Field f in libraryList.Fields)
                        {
                            if (f.Title.Trim().ToUpper().Contains(arlValores[0].ToString().Trim().ToUpper()))
                            {
                                iName = f.InternalName;
                                iType = f.TypeAsString;
                                break;
                            }
                        }

                        string valor = "";
                        if (row.Count < 3)
                        {
                            if (etiquetaActual == "Etiqueta_31" || etiquetaActual == "Etiqueta_32" || etiquetaActual == "Etiqueta_33" || etiquetaActual == "Etiqueta_34" || etiquetaActual == "Etiqueta_25" || etiquetaActual == "Etiqueta_27" || etiquetaActual == "Etiqueta_28" || etiquetaActual == "Etiqueta_29" || etiquetaActual == "Etiqueta_30" || etiquetaActual == "Etiqueta_48")
                            {
                                valor = "";
                            }
                            else
                            {
                                throw new System.Exception("El renglon no cuenta con los campos adecuados. Etiqueta:" + row[0]);
                            }
                        }
                        else
                        {
                            valor = row[2];
                        }

                        if (etiquetasLibros)
                        {
                            valor = "";
                            if (etiquetaActual == "Etiqueta_08_01" || etiquetaActual == "Etiqueta_37_01" || etiquetaActual == "Etiqueta_44_01")
                            {
                                if (row.Count > 3)
                                {
                                    valor = row[3];
                                }
                            }

                            if (row.Count >= 5)
                            {
                                if (etiquetaActual == "Etiqueta_08_02" || etiquetaActual == "Etiqueta_37_02" || etiquetaActual == "Etiqueta_44_02")
                                {
                                    valor = row[4];
                                }

                                if (row.Count >= 7)
                                {
                                    if (etiquetaActual == "Etiqueta_08_03" || etiquetaActual == "Etiqueta_37_03" || etiquetaActual == "Etiqueta_44_03")
                                    {
                                        valor = row[6];
                                    }
                                }
                            }
                        }

                        if (iType == "Text")
                        {
                            if (row.Count > 3)
                            {
                                if (etiquetaActual != "Etiqueta_04")
                                {
                                    for (int i = 3; i < row.Count; i++)
                                    {
                                        valor += row[i].ToString();
                                    }
                                }
                            }
                        }

                        if (iType == "Number")
                        {
                            if (valor == ".")
                            {
                                valor = "0";
                            }

                            string[] tempVal = valor.Split('.');
                            if (tempVal.Length > 2)
                            {
                                valor = "";
                                for (int i = 0; i < tempVal.Length - 1; i++)
                                {
                                    valor += tempVal[i];
                                }
                                valor += "." + tempVal[tempVal.Length - 1];
                            }

                            if (!Util.IsNumeric(valor.ToString()))
                            {
                                valor = "0";
                            }
                        }

                        if (iType == "Currency")
                        {
                            if (valor == ".")
                            {
                                valor = "0";
                            }

                            string[] tempVal = valor.Split('.');
                            if (tempVal.Length > 2)
                            {
                                valor = "";
                                for (int i = 0; i < tempVal.Length - 1; i++)
                                {
                                    valor += tempVal[i];
                                }
                                valor += "." + tempVal[tempVal.Length - 1];
                            }

                            if (!Util.IsNumeric(valor.ToString()))
                            {
                                valor = "0";
                            }
                        }

                        arlValores.Add(valor);          // Field Value
                        arlValores.Add(iName);          //Field Internal Name
                        arlValores.Add(iType);          //Field Type
                        arlValores.Add(etiquetaActual); //Etiqueta
                        arlMetadatos.Add(arlValores);
                    }

                    #endregion

                    //}
                }
            }

            if (ignoreFieldsNotFound == false)
            {
                string msgError = "";
                foreach (ArrayList valores in arlMetadatos)
                {
                    if (valores[3].ToString() == string.Empty)
                    {
                        msgError += valores[0] + ", ";
                    }
                }

                if (msgError.Length > 0)
                {
                    throw new System.Exception("No se encontró coincidencia para los campos: " + msgError.Substring(0, msgError.Length - 2));
                }
            }

            #endregion

            #region Upload image file to SharePoint Library

            string tifImageFileNameFullPath = imagesFullPath + "\\" + imageFileName;

            var _file = new FileCreationInformation();
            _file.Content   = System.IO.File.ReadAllBytes(tifImageFileNameFullPath);
            _file.Overwrite = true;
            _file.Url       = this.mSiteURL + "/" + this.mLibraryName + "/" + imageFileName;

            Microsoft.SharePoint.Client.File uploadFile = libraryList.RootFolder.Files.Add(_file);


            #endregion

            #region Validate document metadata

            foreach (ArrayList valores in arlMetadatos)
            {
                if (valores[2].ToString() != string.Empty)
                {
                    switch (valores[3].ToString())
                    {
                    case "Text":
                    case "Note":
                        string tmpText = valores[1].ToString();
                        if (tmpText.Length > 250)
                        {
                            tmpText = tmpText.Substring(0, 250);
                        }
                        uploadFile.ListItemAllFields[valores[2].ToString()] = tmpText;
                        break;

                    case "Number":
                    case "Currency":
                        if (Util.IsNumeric(valores[1].ToString()))
                        {
                            uploadFile.ListItemAllFields[valores[2].ToString()] = valores[1].ToString();
                        }
                        else
                        {
                            uploadFile.ListItemAllFields[valores[2].ToString()] = "0";
                        }
                        //throw new System.Exception("El valor númerico en el campo: " + valores[4].ToString() + " del documento" + documentFileName + " es inválido");
                        break;

                    case "DateTime":
                        DateTime valorFecha = DateTime.MinValue;
                        try
                        {
                            valorFecha = DateTime.Parse(valores[1].ToString());
                        }
                        catch {
                            throw new System.Exception("El valor de fecha en el campo: " + valores[0].ToString() + " es inválido");
                        }

                        uploadFile.ListItemAllFields[valores[2].ToString()] = valorFecha;
                        break;

                    case "File":
                    case "URL":
                        break;

                    default:
                        string x1 = "";
                        break;
                    }
                }
            }

            uploadFile.ListItemAllFields["Pagina"] = _file.Url;

            //uploadFile.CheckOut();
            uploadFile.ListItemAllFields.Update();
            //uploadFile.CheckIn("", CheckinType.MajorCheckIn);

            clientContext.Load(uploadFile);

            clientContext.ExecuteQuery();

            #endregion
        }
Exemplo n.º 2
0
        public void ValidateLibraryItem(string documentsFullPath, string documentFileName, Boolean ignoreFieldsNotFound)
        {
            #region Upload document information to SharePoint Library

            string documentFullFileName = documentsFullPath + "\\" + documentFileName;

            clientContext.Load(libraryList);
            clientContext.Load(libraryList.Fields);
            clientContext.ExecuteQuery();

            ArrayList arlMetadatos = new ArrayList();
            using (CsvFileReader reader = new CsvFileReader(documentFullFileName))
            {
                CsvRow row = new CsvRow();
                while (reader.ReadRow(row))
                {
                    ArrayList arlEtiquetas = new ArrayList();
                    if (row[0] == "Etiqueta_08" || row[0] == "Etiqueta_37" || row[0] == "Etiqueta_44")
                    {
                        if (row[0] == "Etiqueta_08")
                        {
                            arlEtiquetas.Add("Etiqueta_08_01");
                            arlEtiquetas.Add("Etiqueta_08_02");
                            arlEtiquetas.Add("Etiqueta_08_03");
                        }

                        if (row[0] == "Etiqueta_37")
                        {
                            arlEtiquetas.Add("Etiqueta_37_01");
                            arlEtiquetas.Add("Etiqueta_37_02");
                            arlEtiquetas.Add("Etiqueta_37_03");
                        }

                        if (row[0] == "Etiqueta_44")
                        {
                            arlEtiquetas.Add("Etiqueta_44_01");
                            arlEtiquetas.Add("Etiqueta_44_02");
                            arlEtiquetas.Add("Etiqueta_44_03");
                        }
                    }
                    else
                    {
                        arlEtiquetas.Add(row[0]);
                    }


                    foreach (string etiquetaActual in arlEtiquetas)
                    {
                        ArrayList arlValores = new ArrayList();
                        arlValores.Add(BuscarCoincidenciaEtiqueta(etiquetaActual)); // Field Label

                        string iName = string.Empty;
                        string iType = string.Empty;
                        foreach (Field f in libraryList.Fields)
                        {
                            if (f.Title.Trim().ToUpper().Contains(arlValores[0].ToString().Trim().ToUpper()))
                            {
                                iName = f.InternalName;
                                iType = f.TypeAsString;
                                break;
                            }
                        }


                        string valor = "";

                        if (row.Count < 3)
                        {
                            if (etiquetaActual == "Etiqueta31" || etiquetaActual == "Etiqueta32" || etiquetaActual == "Etiqueta33" || etiquetaActual == "Etiqueta34")
                            {
                                valor = "";
                            }
                            else
                            {
                                throw new System.Exception("El renglon no cuenta con los campos adecuados. Etiqueta:" + etiquetaActual);
                            }
                        }
                        else
                        {
                            valor = row[2];
                        }

                        if (iType == "Text")
                        {
                            if (row.Count > 3)
                            {
                                if (etiquetaActual != "Etiqueta_04")
                                {
                                    for (int i = 3; i < row.Count; i++)
                                    {
                                        valor += row[i].ToString();
                                    }
                                }
                            }
                        }

                        arlValores.Add(valor);  // Field Value
                        arlValores.Add(iName);  //Field Internal Name
                        arlValores.Add(iType);  //Field Type
                        arlValores.Add(row[0]); //Etiqueta
                        arlMetadatos.Add(arlValores);
                    }
                }
            }

            if (ignoreFieldsNotFound == false)
            {
                string msgError = "";
                foreach (ArrayList valores in arlMetadatos)
                {
                    if (valores[3].ToString() == string.Empty)
                    {
                        msgError += valores[0] + ", ";
                    }
                }

                if (msgError.Length > 0)
                {
                    throw new System.Exception("No se encontró coincidencia para los campos: " + msgError.Substring(0, msgError.Length - 2));
                }
            }

            #endregion

            #region Validate document metadata

            foreach (ArrayList valores in arlMetadatos)
            {
                if (valores[2].ToString() != string.Empty)
                {
                    switch (valores[3].ToString())
                    {
                    case "Text":
                        string tmpText = valores[1].ToString();
                        if (tmpText.Length > 250)
                        {
                            tmpText = tmpText.Substring(0, 250);
                        }
                        break;

                    case "Number":
                        if (!Util.IsNumeric(valores[1].ToString()) || valores[1].ToString().Length == 0)
                        {
                            throw new System.Exception("El valor númerico en el campo: " + valores[4].ToString() + " del documento" + documentFileName + " es inválido. valor actual: " + valores[1].ToString());
                        }
                        break;

                    case "DateTime":
                        DateTime valorFecha = DateTime.MinValue;
                        try
                        {
                            valorFecha = DateTime.Parse(valores[1].ToString());
                        }
                        catch
                        {
                            throw new System.Exception("El valor de fecha en el campo: " + valores[0].ToString() + " es inválido. valor actual :" + valores[1].ToString());
                        }

                        break;
                    }
                }
            }

            #endregion
        }