Ejemplo n.º 1
0
        //--------------------------------------------------------------------
        #endregion
        //--------------------------------------------------------------------

        //--------------------------------------------------------------------
        #region Procedimientos y funciones varios (PRIVATE)
        /// <summary>
        /// Conecta con un site SHP (crea el objeto)
        /// </summary>
        /// <returns></returns>
        private bool ConnectToSite()
        {
            bool testConnection = false;

            site = null;
            try
            {
                site = new ClientContext(SiteSHP);
                if (!string.IsNullOrWhiteSpace(SiteUSER) && !string.IsNullOrWhiteSpace(SitePWD))
                {
                    testConnection = true;
                    if (SiteUSER.Contains(@"\"))
                    {
                        SiteDOMAIN       = SiteUSER.Substring(0, SiteUSER.IndexOf(@"\"));
                        SiteUSER         = SiteUSER.Substring(SiteUSER.IndexOf(@"\") + 1);
                        site.Credentials = new NetworkCredential(SiteUSER, SitePWD, SiteDOMAIN);
                    }
                    else
                    {
                        if (IsSHPOnline)
                        {
                            site.Credentials = new SharePointOnlineCredentials(SiteUSER, GetPasswordSecurity(SitePWD));
                        }
                        else
                        {
                            site.Credentials = new NetworkCredential(SiteUSER, SitePWD);
                        }
                    }
                }
                else
                {
                    site.Credentials = new NetworkCredential();
                }

                if (testConnection)
                {
                    Web webTest = site.Web;
                    try
                    {
                        site.Load(webTest);
                        site.ExecuteQuery();
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        SHPExecOperationsErrors.Add(GetInfoError(ex, "Conexion a site - credenciales", Clases.TypeError.Error));
                        return(false);
                    }
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                SHPExecOperationsErrors.Add(GetInfoError(ex, "Conexion a site", Clases.TypeError.Error));
                return(false);
            }
        }
Ejemplo n.º 2
0
        //--------------------------------------------------------------------

        //--------------------------------------------------------------------
        #region Procedimientos y funciones varios (PUBLIC)
        /// <summary>
        /// Crea una nueva lista en el site.
        /// </summary>
        /// <param name="Title">Título / Nombre de la lista</param>
        /// <param name="TemplateType">Tipo de lista (Genérica (100) por defecto)</param>
        /// <param name="Description">Descripcion para la lista a crear</param>
        /// <param name="SetListName">Indica si se ha de establecer el nombre de la lista a una propiedad de la clase para poder realizar, a posteriori, mas operaciones como añadir campos</param>
        /// <returns></returns>
        public bool CreateList(string title, int templateType = 100, string description = "", bool setListName = false)
        {
            bool result;

            SPFields.Clear();

            if (ConectionSiteOK)
            {
                ListCreationInformation CreationInfo = new ListCreationInformation
                {
                    Title        = title.Trim(),
                    Description  = description.Trim(),
                    TemplateType = templateType
                };

                try
                {
                    List newList = site.Web.Lists.Add(CreationInfo);
                    site.Load(newList);
                    site.ExecuteQuery();

                    result = true;
                }
                catch (Exception ex)
                {
                    SHPExecOperationsErrors.Add(GetInfoError(ex, string.Format("Alta de lista {0}", title), TypeError.Error));
                    result = false;
                }
            }
            else
            {
                SHPExecOperationsErrors.Add(GetInfoError("Alta de nueva lista (CreateList)", "No se ha establecido conexión con el site", string.Format("Alta de lista {0}", title), TypeError.Warning));
                result = false;
            }

            if ((result) && (setListName))
            {
                ListName = title.Trim();
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Lanza el proceso de creación de nuevas columnas
        /// </summary>
        /// <returns></returns>
        public bool CreateColumns()
        {
            bool result          = true;
            List list            = null;
            bool continueProcess = true;

            if (ConectionSiteOK)
            {
                if (SPFields.Count > 0)
                {
                    foreach (SPFields infoField in SPFields)
                    {
                        continueProcess = true;
                        try
                        {
                            list = site.Web.Lists.GetByTitle(infoField.ListName);
                        }
                        catch (Exception ex)
                        {
                            SHPExecOperationsErrors.Add(GetInfoError(ex, string.Format("Alta de nuevo campo {0}", infoField.ColumnName), TypeError.Error));
                            continueProcess = false;
                        }

                        if (continueProcess)
                        {
                            //Configuramos...
                            switch (infoField.Column)
                            {
                            case TypeColumn.LineOfText:
                                //Linea de texto
                                Field newField1 = list.Fields.AddFieldAsXml(string.Format(schemaXML, infoField.Type, infoField.ColumnName), true, AddFieldOptions.AddToDefaultContentType);
                                newField1.StaticName  = infoField.ColumnName;
                                newField1.Description = infoField.Description;

                                if (infoField.FieldText != null)
                                {
                                    FieldText fldText = site.CastTo <FieldText>(newField1);
                                    fldText.Required            = infoField.FieldText.ContainInformation;
                                    fldText.EnforceUniqueValues = infoField.FieldText.UniqueValues;
                                    fldText.MaxLength           = infoField.FieldText.MaxLength;
                                    fldText.DefaultValue        = infoField.FieldText.DefaultValue;
                                    if (infoField.FieldText.UniqueValues)
                                    {
                                        fldText.Indexed = true;
                                    }
                                    fldText.Update();
                                }
                                else
                                {
                                    continueProcess = false;
                                }
                                break;

                            case TypeColumn.Multiline:
                                //Varias líneas de texto
                                Field newField2 = list.Fields.AddFieldAsXml(string.Format(schemaXML, infoField.Type, infoField.ColumnName), true, AddFieldOptions.AddToDefaultContentType);
                                newField2.StaticName  = infoField.ColumnName;
                                newField2.Description = infoField.Description;

                                if (infoField.FieldMultiText != null)
                                {
                                    FieldMultiLineText fldMultiLine = site.CastTo <FieldMultiLineText>(newField2);
                                    fldMultiLine.Required      = infoField.FieldMultiText.ContainInformation;
                                    fldMultiLine.NumberOfLines = infoField.FieldMultiText.NumLines;
                                    fldMultiLine.RichText      = infoField.FieldMultiText.RichText;
                                    fldMultiLine.Update();
                                }
                                else
                                {
                                    continueProcess = false;
                                }
                                break;

                            case TypeColumn.Choice:
                                //Eleccion
                                Field newField3 = list.Fields.AddFieldAsXml(string.Format(schemaXML, infoField.Type, infoField.ColumnName), true, AddFieldOptions.AddToDefaultContentType);
                                newField3.StaticName  = infoField.ColumnName;
                                newField3.Description = infoField.Description;

                                if (infoField.FieldChoice != null)
                                {
                                    FieldMultiChoice fldChoice = site.CastTo <FieldMultiChoice>(newField3);
                                    fldChoice.Required            = infoField.FieldChoice.ContainInformation;
                                    fldChoice.EnforceUniqueValues = infoField.FieldChoice.UniqueValues;
                                    fldChoice.DefaultValue        = infoField.FieldChoice.DefaultValue;
                                    if (infoField.FieldChoice.UniqueValues)
                                    {
                                        fldChoice.Indexed = true;
                                    }
                                    fldChoice.Choices = infoField.FieldChoice.Options;
                                    fldChoice.Update();
                                }
                                else
                                {
                                    continueProcess = false;
                                }
                                break;

                            case TypeColumn.Number:
                                //Numero
                                Field newField4 = list.Fields.AddFieldAsXml(string.Format(schemaXML, infoField.Type, infoField.ColumnName), true, AddFieldOptions.AddToDefaultContentType);
                                newField4.StaticName  = infoField.ColumnName;
                                newField4.Description = infoField.Description;

                                if (infoField.FieldNumber != null)
                                {
                                    FieldNumber fldNumber = site.CastTo <FieldNumber>(newField4);
                                    fldNumber.ShowAsPercentage = infoField.FieldNumber.ShowAsPercentaje;
                                    if (infoField.FieldNumber.MinValue != 0)
                                    {
                                        fldNumber.MinimumValue = infoField.FieldNumber.MinValue;
                                    }
                                    if (infoField.FieldNumber.MaxValue != 0)
                                    {
                                        fldNumber.MaximumValue = infoField.FieldNumber.MaxValue;
                                    }

                                    fldNumber.Update();
                                }
                                else
                                {
                                    continueProcess = false;
                                }
                                break;

                            case TypeColumn.Lookup:
                                if (infoField.FieldLookup != null)
                                {
                                    string required = "FALSE";
                                    if (infoField.FieldLookup.ContainInformation)
                                    {
                                        required = "TRUE";
                                    }

                                    try
                                    {
                                        List sourceList = site.Web.Lists.GetByTitle(infoField.FieldLookup.LookupList);
                                        site.Load(sourceList);

                                        List destinationList = site.Web.Lists.GetByTitle(infoField.ListName);
                                        site.Load(destinationList);

                                        site.ExecuteQuery();

                                        Field LookUpField = destinationList.Fields.AddFieldAsXml(string.Format(schemaXMLLookUp, infoField.Type, infoField.ColumnName, required, sourceList.Id, infoField.FieldLookup.LookupField), true, AddFieldOptions.DefaultValue);
                                        LookUpField.Description = infoField.Description;
                                        LookUpField.Update();
                                    }
                                    catch (Exception ex)
                                    {
                                        SHPExecOperationsErrors.Add(GetInfoError(ex, string.Format("Alta de nuevo campo {0}", infoField.ColumnName), TypeError.Error));
                                        continueProcess = false;
                                    }
                                }
                                else
                                {
                                    continueProcess = false;
                                }
                                break;

                            case TypeColumn.UsersAndGroups:
                                Field newField5 = list.Fields.AddFieldAsXml(string.Format(schemaXML, infoField.Type, infoField.ColumnName), true, AddFieldOptions.AddToDefaultContentType);
                                newField5.StaticName  = infoField.ColumnName;
                                newField5.Description = infoField.Description;

                                if (infoField.FieldUserGroup != null)
                                {
                                    FieldUser fldUser = site.CastTo <FieldUser>(newField5);
                                    fldUser.Required = infoField.FieldUserGroup.ContainInformation;
                                    switch (infoField.FieldUserGroup.UserSelectionMode)
                                    {
                                    case TypeUserSelectionMode.PeopleOnly:
                                        fldUser.SelectionMode = FieldUserSelectionMode.PeopleOnly;
                                        break;

                                    case TypeUserSelectionMode.PeopleAndGroups:
                                        fldUser.SelectionMode = FieldUserSelectionMode.PeopleAndGroups;
                                        break;
                                    }

                                    if (infoField.FieldUserGroup.MultiUser)
                                    {
                                        fldUser.AllowMultipleValues = true;
                                    }

                                    if (!string.IsNullOrEmpty(infoField.FieldUserGroup.UsersFromGroup))
                                    {
                                        Group group = site.Web.SiteGroups.GetByName(infoField.FieldUserGroup.UsersFromGroup);

                                        try
                                        {
                                            site.Load(group);
                                            site.ExecuteQuery();
                                        }
                                        catch (Exception ex)
                                        {
                                            SHPExecOperationsErrors.Add(GetInfoError(ex, string.Format("Alta de nuevo campo {0}", infoField.ColumnName), TypeError.Warning));
                                            group = null;
                                        }

                                        if (group != null)
                                        {
                                            fldUser.SelectionGroup = group.Id;
                                        }
                                    }

                                    fldUser.Update();
                                }
                                else
                                {
                                    continueProcess = false;
                                }

                                break;
                            }

                            //Añadimos...
                            if (continueProcess)
                            {
                                try
                                {
                                    site.ExecuteQuery();
                                }
                                catch (Exception ex)
                                {
                                    SHPExecOperationsErrors.Add(GetInfoError(ex, string.Format("Alta de nuevo campo {0}", infoField.ColumnName), TypeError.Error));
                                }
                            }
                        }

                        list = null;
                    }
                }
                else
                {
                    SHPExecOperationsErrors.Add(GetInfoError("Creación de columnas (CreateColumns)", "No se han indicado campos a crear", "", TypeError.Warning));
                    result = false;
                }
            }
            else
            {
                SHPExecOperationsErrors.Add(GetInfoError("Creación de columnas (CreateColumns)", "No se ha establecido conexión con el site", "", TypeError.Warning));
                result = false;
            }

            SPFields.Clear();

            return(result);
        }